2d line intersection code

Blitz3D Forums/Blitz3D Programming/2d line intersection code

Craig H. Nisbet(Posted 2010) [#1]
Hey guys,

Anyone know where I can get code to test if 2 2d lines are intersectiong each other? Also, I need the coords for that point at the intersection.


BlitzSupport(Posted 2010) [#2]
See the Code Archives:

http://www.blitzbasic.com/codearcs/codearcs.php?code=2597
http://www.blitzbasic.com/codearcs/codearcs.php?code=1855


big10p(Posted 2010) [#3]
Just as well throw mine into the mix, too:
Global vl_intersect_x, vl_intersect_y

;
; Determines whether two lines intersect, or not. If the lines do intersect, the
; point of intersection is returned in the global variables vl_intersect_x and vl_intersect_y.
;
; Params:
; x1,y1   - Start coords of first line.
; dx1,dy1 - Delta coords of first line.
; x2,y2   - Start coords of second line.
; dx2,dy2 - Delta coords of second line.
;
; Returns:
; True if the lines intersect, false otherwise.
;
Function vl__lines_intersect_xy(x1#, y1#, dx1#, dy1#,  x2#, y2#, dx2#, dy2#)
	
	u_b# = dy2 * dx1 - dx2 * dy1

	If (u_b <> 0) 

		ua_t# = (dx2 * (y1 - y2)) - (dy2 * (x1 - x2))
		ub_t# = (dx1 * (y1 - y2)) - (dy1 * (x1 - x2))
		
		ua# = ua_t / u_b
		ub# = ub_t / u_b
	
		If (ua >= 0 And ua <= 1 And ub >= 0 And ub <= 1) 

			vl_intersect_x = x1 + (ua * dx1)
			vl_intersect_y = y1 + (ua * dy1)

			Return True

		EndIf 

	EndIf

	Return False
	
End Function