SUB intersect2Circles
(x1
, y1
, r1
, x2
, y2
, r2
, ix1
, iy1
, ix2
, iy2
) 'x1, y1 origin of circle 1 with radius r1
'x2, y2 origin of circle 2 with radius r2
'ix1, iy1 is the first point of intersect
'ix2, iy2 is the 2nd point of intersect
'if ix1 = ix2 = iy1 = iy2 = 0 then no points returned
d = distance(x1, y1, x2, y2) 'distance between two origins
'PRINT "The circles are too far apart to intersect.": END
'some signal ??? if ix1 = ix2 = iy1 = iy2 = 0 then no points returned
ix1 = 0: ix2 = 0: iy1 = 0: iy2 = 0
IF (d
< r1
AND r2
+ d
< r1
) OR (d
< r2
AND r1
+ d
< r2
) THEN 'one circle is inside the other = no intersect ix1 = 0: ix2 = 0: iy1 = 0: iy2 = 0
'IF ABS(r1 - r2) > 3 THEN
' PRINT "No intersect, same center (or nearly so) and different radii (or seemingly so).": END
'ELSE
' PRINT "Infinite intersect, the circles are the same (or nearly so).": END
'END IF
'results
a = (r1 ^ 2 - r2 ^ 2 + d ^ 2) / (2 * d)
Px = x1 + a * (x2 - x1) / d
pY = y1 + a * (y2 - y1) / d
h = (r1 ^ 2 - a ^ 2) ^ .5
ix1
= INT(Px
- h
* (y2
- y1
) / d
) iy1
= INT(pY
+ h
* (x2
- x1
) / d
) 'circle x1,y1,2,1 filled
'PRINT: PRINT "Intersect pt1: "; x1; ", "; y1
ix2
= INT(Px
+ h
* (y2
- y1
) / d
) iy2
= INT(pY
- h
* (x2
- x1
) / d
) 'circle x2,y2,2,1 filled
'PRINT: PRINT "Intersect pt2: "; x2; ", "; y2
'line x1,y1,x2,y2