_TITLE "Two Triangles Overlap is Outlined in White dots" 'b+ 2020-03-18 ' Just worked Rosetta Code for Line Intersect Line
' but what if we want to know if two line segments intersect?
'2020-03-14 "Two Line Segments Intersect" 'b+ 2020-03-14 start
'2020-03-15 rework this code so we identify points all on same line and
' if there is overlap of line segments say the two x endpoints of the segments
' otherwise, if there is an intersect of 2 line segments say the point x, y.
' Return 0 no intersect or overlap
' Return 1 if intersect and ix, iy point of intersect
' Return -1 if segments are on same and there is overlap: ix = overlap start x, iy overlap end x
'2020-03-16 "Segments Intersect mod tester" >>> just post testing code
'mod tester for 2 segments of vertical line and found I need to add more parameters to
' FUNCTION twoLineSegmentsIntersect% (ax1, ay1, ax2, ay2, bx1, by1, bx2, by2, ix, iy)
' mod that name and parameters to:
' FUNCTION twoSegmentsIntersect% (ax1, ay1, ax2, ay2, bx1, by1, bx2, by2, ix1, iy1, ix2, iy2)
'2020-03-16 Segments Intersect revised 2020-03-16
' OK now get the new FUNCTION working
' ah! I had to tighten down D from >.2 to >.05 but loosen y-axis intersect
'2020-03-18 apply routines to two triangles
' modified FUNCTION twoSegmentsIntersect% (ax1, ay1, ax2, ay2, bx1, by1, bx2, by2, ix1, iy1)
' to do only intersect. This code proved, Segments Intersect revised 2020-03-16, was faulty.
CONST xmax
= 800, ymax
= 600 ax1
= (xmax
- 20) * RND + 10: ay1
= (ymax
- 20) * RND + 10 ax2
= (xmax
- 20) * RND + 10: ay2
= (ymax
- 20) * RND + 10 ax3
= (xmax
- 20) * RND + 10: ay3
= (ymax
- 20) * RND + 10 bx1
= (xmax
- 20) * RND + 10: by1
= (ymax
- 20) * RND + 10 bx2
= (xmax
- 20) * RND + 10: by2
= (ymax
- 20) * RND + 10 bx3
= (xmax
- 20) * RND + 10: by3
= (ymax
- 20) * RND + 10 'tri a
LINE (ax1
, ay1
)-(ax2
, ay2
), &HFFFF0000 LINE (ax2
, ay2
)-(ax3
, ay3
), &HFFFF0000 LINE (ax3
, ay3
)-(ax1
, ay1
), &HFFFF0000 'tri b
LINE (bx1
, by1
)-(bx2
, by2
), &HFF0000FF LINE (bx2
, by2
)-(bx3
, by3
), &HFF0000FF LINE (bx3
, by3
)-(bx1
, by1
), &HFF0000FF
dista2a3
= _HYPOT(ax2
- ax3
, ay2
- ay3
) adx = (ax3 - ax2) / dista2a3: ady = (ay3 - ay2) / dista2a3
distb2b3
= _HYPOT(bx2
- bx3
, by2
- by3
) bdx = (bx3 - bx2) / distb2b3: bdy = (by3 - by2) / distb2b3
x1 = ax2 + adx * i: y1 = ay2 + ady * i
sect = twoSegmentsIntersect%(ax1, ay1, x1, y1, bx1, by1, bx2, by2, ix1, iy1)
sect = twoSegmentsIntersect%(ax1, ay1, x1, y1, bx3, by3, bx2, by2, ix1, iy1)
sect = twoSegmentsIntersect%(ax1, ay1, x1, y1, bx1, by1, bx3, by3, ix1, iy1)
x1 = bx2 + bdx * i: y1 = by2 + bdy * i
sect = twoSegmentsIntersect%(bx1, by1, x1, y1, ax1, ay1, ax2, ay2, ix1, iy1)
sect = twoSegmentsIntersect%(bx1, by1, x1, y1, ax3, ay3, ax2, ay2, ix1, iy1)
sect = twoSegmentsIntersect%(bx1, by1, x1, y1, ax1, ay1, ax3, ay3, ix1, iy1)
INPUT "Press enter for another demo, any + enter to quit...", again$
'Slope and Y-intersect for non vertical lines,
' if x1 = x2 the line is vertical don't call this sub
' because slope calculation would cause division by 0 error.
SUB slopeYintersect
(X1
, Y1
, X2
, Y2
, slope
, Yintercept
) 'check x1 <> x2 first! slope = (Y2 - Y1) / (X2 - X1): Yintercept = slope * (0 - X1) + Y1
' ======================================== end tester code functions ======================================
'This function needs: FUNCTION lineIntersectLine% (ax1, ay1, ax2, ay2, bx1, by1, bx2, by2, ix, iy)
' which in turn needs: SUB slopeYintersect (X1, Y1, X2, Y2, slope, Yintercept) 'check x1 <> x2 first!
FUNCTION twoSegmentsIntersect%
(ax1
, ay1
, ax2
, ay2
, bx1
, by1
, bx2
, by2
, ix1
, iy1
) intersect = lineIntersectLine%(ax1, ay1, ax2, ay2, bx1, by1, bx2, by2, ix, iy)
IF ax1
< ax2
THEN aMinX
= ax1: aMaxX
= ax2
ELSE aMinX
= ax2: aMaxX
= ax1
IF ay1
< ay2
THEN aMinY
= ay1: aMaxY
= ay2
ELSE aMinY
= ay2: aMaxY
= ay1
IF bx1
< bx2
THEN bMinX
= bx1: bMaxX
= bx2
ELSE bMinX
= bx2: bMaxX
= bx1
IF by1
< by2
THEN bMinY
= by1: bMaxY
= by2
ELSE bMinY
= by2: bMaxY
= by1
IF intersect
= 0 THEN 'no intersect twoSegmentsIntersect% = 0
ELSEIF intersect
= 1 THEN 'segments intersect at one point IF ax1
= ax2
THEN 'is iy between IF iy
< aMinY
OR iy
> aMaxY
OR ix
< bMinX
OR ix
> bMaxX
THEN twoSegmentsIntersect% = 0
ix1 = ix: iy1 = iy: twoSegmentsIntersect% = 1
IF iy
< bMinY
OR iy
> bMaxY
OR ix
< aMinX
OR ix
> aMaxX
THEN twoSegmentsIntersect% = 0
ix1 = ix: iy1 = iy: twoSegmentsIntersect% = 1
ix1 = ix: iy1 = iy: twoSegmentsIntersect% = 1
' this function needs: SUB slopeYintersect (X1, Y1, X2, Y2, slope, Yintercept)
FUNCTION lineIntersectLine%
(ax1
, ay1
, ax2
, ay2
, bx1
, by1
, bx2
, by2
, ix
, iy
) IF ax1
= ax2
THEN 'line a is vertical IF bx1
= bx2
THEN ' b is vertical IF ax1
= bx1
THEN lineIntersectLine%
= -1 ' if x's are same it is same vertical line ix = ax1
slopeYintersect bx1, by1, bx2, by2, m2, y02
iy = m2 * ix + y02
lineIntersectLine% = 1 'signal a point was found
slopeYintersect ax1, ay1, ax2, ay2, m1, y01 ' -m = a, 1 = b, y0 = c std form
IF bx1
= bx2
THEN 'b is vertical ix = bx1: iy = m1 * ix + y01: lineIntersectLine% = 1 'signal a point was found
slopeYintersect bx1, by1, bx2, by2, m2, y02 ' -m = a, 1 = b, y0 = c std form
d = -m1 - -m2 ' if = 0 then parallel or equal because slopes are same
IF d
THEN 'otherwise about 0 <<< tighten down from .2 to .05 ix = (y01 - y02) / d: iy = (-m1 * y02 - -m2 * y01) / d
lineIntersectLine% = 1 'signal one intersect point was found