Post by (X) on Feb 11, 2023 18:48:15 GMT 1
I found a nifty solution to finding the intersection coordinates of 2 lines from the following website:
I adapted the code to GFA:
'#################################################################################
'
' DISCLAIMER: THIS CODE IS OFFERED AS IS FOR EDUCATIONAL PURPOSES.
' YOU ARE FREE TO USE, COPY AND DISTRIBUTE.
' THE USE OF THIS CODE IS AT YOUR OWN RISK.
' I ACCEPT NO LIABILITY NOR MAKE ANY CLAIM OF
' CORRECTNESS OR SUITABILITY IN PART OR IN WHOLE.
'
'#################################################################################
'
' FILENAME Demo Line Line Intersect.G32
'
' DESCRIPTION Demonstration of how to obtain the intersection coordinates of
' 2 lines, given the start and end coordinates of both lines.
'
' AUTHOR (X)
' EMAIL xman.gb32@gmail.com
' WEBSITE https://gfabasic32.blogspot.com/
' FORUM https://gb32.proboards.com/
' STARTED 2023-02-11
' UPDATED <ISO date>
'
'
$ManifestOff
$Library "GfaWinX"
$Library "UpdateRT"
UpdateRuntime
Mode Date "-"
P_Main
Proc P_Main
OpenW 1 : FontSize = 16
Local x#, y#
Local x1# = 0, y1# = 0, x2# = 100, y2# = 100
'Local x1# = 0, y1# = 100, x2# = 100, y2# = 0
Local x3# = 0, y3# = 100, x4# = 100, y4# = 0
Print
Print " This is a demonstration of finding the intersection coordinates of 2 lines, given"
Print " the start and end points of each line."
Print " The first line coordinates are:", "("; x1; ","; y1; ","; x2; ","; y2; ")"
Print " The second line coordinates are:", "("; x3; ","; y3; ","; x4; ","; y4; ")"
If F_LineLineIntersect(x1#, y1#, x2#, y2#, x3#, y3#, x4#, y4#, x#, y#)
Print "The (x, y) intersection is:("; x; ","; y; ")"
Else
Print "These lines are parallel, so, do not intersect."
EndIf
Do
Sleep
Loop Until Win_1 Is Nothing
CloseW 1
EndProc
Proc P_Notes
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' From: https://www.topcoder.com/thrive/articles/Geometry%20Concepts%20part%202:%20%20Line%20Intersection%20and%20its%20Applications
'
' LINE-LINE, INTERSECTION
'
' One of the most common tasks you will find in geometry problems is line
' intersection. Despite the fact that it is so common, a lot of coders still
' have trouble with it. The first question is, what form are we given our
' lines in, and what form would we like them in? Ideally, each of our lines
' will be in the form Ax+By=C, where A, B and C are the numbers which define
' the line. However, we are rarely given lines in this format, but we can
' easily generate such an equation from two points. Say we are given two
' different points, (x1, y1) and (x2, y2), and want to find A, B and C for
' the equation above. We can do so by setting:
'
' A = y2 - y1
' B = x1 - x2
' C = Ax1 + By1
'
' Regardless of how the lines are specified, you should be able to generate
' two different points along the line, and then generate A, B and C. Now,
' lets say that you have lines, given by the equations:
'
' A1x + B1y = C1
' A2x + B2y = C2
'
' To find the point at which the two lines intersect, we simply need to solve
' the two equations for the two unknowns, x and y.
'
' double det = A1 * B2 - A2 * B1
' if (det == 0) {
' //Lines are parallel
' } else {
' double x = (B2 * C1 - B1 * C2) / det
' double y = (A1 * C2 - A2 * C1) / det
' }
'
EndProc
Function A(y1#, y2#) As Double
Return (y2 - y1)
EndFunc
Function B(x1#, x2#) As Double
Return (x1 - x2)
EndFunc
Function C(_A#, _B#, x1#, y1#) As Double
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' C = Ax1 + By1
'
Return ((_A * x1) + (_B * y1))
EndFunc
Function F_LineLineIntersect(x1#, y1#, x2#, y2#, x3#, y3#, x4#, y4#, ByRef x#, ByRef y#) As Bool
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Regardless of how the lines are specified, you should be able to generate
' two different points along the line, and then generate A, B and C. Now,
' lets say that you have lines, given by the equations:
'
' A1x + B1y = C1
' A2x + B2y = C2
'
' To find the point at which the two lines intersect, we simply need to solve
' the two equations for the two unknowns, x and y.
'
' double det = A1 * B2 - A2 * B1
'
Local A1# = A(y1, y2)
Local A2# = A(y3, y4)
Local B1# = B(x1, x2)
Local B2# = B(x3, x4)
Local C1# = C(A1, B1, x1, y1)
Local C2# = C(A2, B2, x3, y3)
Local det# = (A1 * B2) - (A2 * B1)
If (det == 0)
// Lines are parallel
x = 0 : y = 0
Return False
Else
x = ((B2 * C1) - (B1 * C2)) / det
y = ((A1 * C2) - (A2 * C1)) / det
Return True
EndIf
EndFunc
Don't ask me to explain the reasoning. It just seems to work.