TYPE POINTTYPE
' X,Y point with integer precision
'**********************************************************************************************************************
'******************************************************************************************************************
'* Returns TRUE if line segments p1q1 and p2q2 intersect. *
'* *
'* p1x,p1y - starting X,Y coordinates of segment 1 *
'* q1x,q1y - ending X,Y coordinates of segment 1 *
'* p2x,p2y - starting X,Y coordinates of segment 2 *
'* q2x,q2y - ending X,Y coordinates of segment 2 *
'* *
'* This function was created from example code found at: *
'* https://www.geeksforgeeks.org/check-if-two-given-line-segments-intersect/ *
'*****************************************************************************
DIM p1
AS POINTTYPE
' line 1 coordinate X,Y pairs DIM p2
AS POINTTYPE
' line 2 coordinate X,Y pairs
p1.x = p1x: p1.y = p1y ' line 1 start X,Y
q1.x = q1x: q1.y = q1y ' line 1 end X,Y
p2.x = p2x: p2.y = p2y ' line 2 start X,Y
q2.x = q2x: q2.y = q2y ' line 2 end X,Y
o1 = Orientation(p1, q1, p2) ' get the four orientations needed for general and special cases
o2 = Orientation(p1, q1, q2)
o3 = Orientation(p2, q2, p1)
o4 = Orientation(p2, q2, q1)
IF o3
<> o4
THEN ' general case ObjIntersect = -1
IF onSegment
(p1
, p2
, q1
) THEN ' p1, q1, and p2 are colinear and p2 lies on segment p1q1 ObjIntersect = -1
IF onSegment
(p1
, q2
, q1
) THEN ' p1, q1, and q2 are colinear and q2 lies on segment p1q1 ObjIntersect = -1
IF onSegment
(p2
, p1
, q2
) THEN ' p2, q2, and p1 are colinear and p1 lies on segment p2q2 ObjIntersect = -1
IF onSegment
(p2
, q1
, q2
) THEN ' p2, q2, and q1 are colinear and q1 lies on segment p2q2 ObjIntersect = -1
ObjIntersect = 0 ' doesn't fall into any of the above cases
'**********************************************************************************************************************
FUNCTION Orientation
(p
AS POINTTYPE
, q
AS POINTTYPE
, r
AS POINTTYPE
) ' ORIENTATION '******************************************************************************************************************
'* Returns the orientation of ordered triplet p, q, r. *
'* 0 = p, q, r are colinear *
'* 1 = clockwise orientation *
'* 2 = counter clockwise orientation *
'* *
'* This function was created from example code found at: *
'* https://www.geeksforgeeks.org/check-if-two-given-line-segments-intersect/ *
'* (FOR INTERNAL USE ONLY) *
'*****************************************************************************
Value = (q.y - p.y) * (r.x - q.x) - (q.x - p.x) * (r.y - q.y) ' calculate orientation
Orientation = 0
Orientation = 1
Orientation = 2
'**********************************************************************************************************************
FUNCTION onSegment
(p
AS POINTTYPE
, q
AS POINTTYPE
, r
AS POINTTYPE
) ' ONSEGMENT '******************************************************************************************************************
'* Given 3 colinear points p, q, r, the function checks if point q lies on line segment pr. *
'* *
'* p, q, r - three colinear X,Y points *
'* *
'* This function was created from example code found at: *
'* https://www.geeksforgeeks.org/check-if-two-given-line-segments-intersect/ *
'* (FOR INTERNAL USE ONLY) *
'********************************************************************************************
IF q.x
<= ObjMax
(p.x
, r.x
) THEN IF q.x
>= ObjMin
(p.x
, r.x
) THEN IF q.y
<= ObjMax
(p.y
, r.y
) THEN IF q.y
>= ObjMin
(p.y
, r.y
) THEN onSegment = -1
'**********************************************************************************************************************
'******************************************************************************************************************
'* Returns the maximum of two numbers provided. *
'* *
'* n1, n2 - the numbers to be compared *
'************************************************
IF n1
> n2
THEN ObjMax
= n1
ELSE ObjMax
= n2
' return largest number
'**********************************************************************************************************************
'******************************************************************************************************************
'* Returns the minimum of two numbers provided. *
'* *
'* n1, n2 - the numbers to be compared *
'************************************************
IF n1
< n2
THEN ObjMin
= n1
ELSE ObjMin
= n2
' return smallest number