Code archives/Algorithms/Lines Intersect

This code has been declared by its author to be Public Domain code.

Download source code

Lines Intersect by GfK2009
.
.

Comments

AndrewT2009
Nice job. I just made a line intersection function for the same reason you did, and it turned out near-identical; great minds think alike. ;)

Also, you can check for intersection between line segments as opposed to lines as well by checking if the intersection point is between the endpoints of both lines by adding this to the end of your function

	If Between(X, X1, X2) And Between(Y, Y1, Y2) And Between(X, X3, X4) And Between(Y, Y3, Y4)
                'Line segments intersect, return 1
		Return 1
	EndIf

        Return 0


Here's the Between function:

Function Between:Int(X:Float, B:Float, T:Float)
	If T > B
		If X > B And X < T
			Return 1
		EndIf
	Else
		If X > T And X < B
			Return 1
		EndIf
	EndIf
	Return 0
EndFunction


Might slow it down a bit, which is why I made a separate function to check for segment intersection.


GfK2009
Thanks for that. I was going to add that later but you just saved me the trouble. ;)


Difference2011
Thanks both.

Small error in the code above (that goes unnoticed if you don't need to check for colinear lines )

b2 = v1 - (m2 * u2)

should be

b2 = v1 - (m2 * u1)


RemiD2016
.


Code Archives Forum