# LineLine Intersection

Thanks to all the help regarding Maths.

I was having trouble getting this method to work. I am pretty sure I understand what it is meant to do and that is detect non-segmented line intersection.

I was wondering if changes regarding my ‘LineShape’ class have gave in the wrong input to the method.

I have spent quite a bit of time researching geometry including vectors, magnitude, direction and line to line intersection.

Here is the project file (detecting parallel lines or any kind of intersection does not seem to work at all):
https://www.dropbox.com/s/crh295jhjwiaafz/DEBUG.xojo_binary_project?dl=0

Here is the modified method:

``````Function Intersects(other as LineShape, intersectionPoint as Xojo.Core.Point) As boolean
dim A1, A2, B1, B2 as Xojo.Core.Point
A1 = FromPoint
A2 = ToPoint
B1 = other.FromPoint
B2 = other.ToPoint

dim Aslope as Double = (A2.Y - A1.Y) / (A2.X - A1.X)
dim Bslope as Double = (B2.Y - B1.Y) / (B2.X - B1.X)

dim AIntercept as Double = A1.Y - Aslope*A1.X
dim BIntercept as Double = B1.Y - Bslope*B1.X

if Aslope = Bslope then //parallel lines
if AIntercept <> BIntercept then
Return False
else
Return True
end if
end if

dim XIntersection as Double = (BIntercept-AIntercept)/(Aslope-Bslope)
dim YIntersection as Double = Aslope*XIntersection + AIntercept

intersectionPoint = new xojo.Core.Point(XIntersection,YIntersection)
Return True
End Function``````

Thanks

and… at the end of the day… you learned something… am I right?

I don’t remember that far back… but it may be that the math involved in this won’t come up in you school work for another year or two… But this is proof that “yes you DO need to learn math, because the computer can’t design it for you”

Are your y-intercepts correct? The slope you calculated is not the slope using standard Cartesian coordinates because 0,0 for Xojo is the upper left hand corner, not the lower right hand corner. This has caused me so much grief that I transform all my x,y data before any mathematical calculations.

Is there a method which I can use to switch the point system?

Thanks

Wouldn’t 0, 0 be the centre? I suppose as it is just a point and not a rectangle then there is no centre. Am I right? Do I just flip the coordinates to be the opposite.

Thanks