2D line intersection. Beat this?

Miscellaneous Forums/General Discussion/2D line intersection. Beat this?

Hello

I've converted the following line intersection code from C source. It seems to work fine (the first line is dodgy because it's checking for equality between floats) but I'm not convinced it's the fastest method possible. The rotation of the coordinate system smacks as a bit of a kludge, to me. Maybe I'm wrong, though. :)

Anyone able to improve on this?

I've tried sswift's in the archives but it doesn't work.

Global intersection_x#
Global intersection_y#

Function lines_intersect(ax#,ay#,bx#,by#, cx#,cy#,dx#,dy#)

  ; Fail if either line segment is zero-length.
  If (ax=bx And ay=by) Or (cx=dx And cy=dy) Then Return False

  ; Translate the system so that point A is on the origin.
  bx = bx - ax : by = by - ay
  cx = cx - ax : cy = cy - ay
  dx = dx - ax : dy = dy - ay

  ; Discover the length of segment A-B.	
  distAB# = Sqr(bx*bx + by*by)

  ; Rotate the system so that point B is on the positive X axis.
  theCos# = bx / distAB
  theSin# = by / distAB
  newX# = cx * theCos + cy * theSin
  cy = cy * theCos - cx * theSin
  cx = newX
  newX = dx * theCos + dy * theSin
  dy = dy * theCos - dx * theSin
  dx = newX

  ; Fail if segment C-D doesn't cross line A-B.
  If (cy<0 And dy<0) Or (cy>=0 And dy>=0) Return False

  ; Discover the position of the intersection point along Line A-B.
  ABpos# = dx + (cx-dx) * dy / (dy-cy)

  ; Fail if segment C-D crosses line A-B outside of segment A-B.
  If (ABpos<0 Or ABpos>distAB) Return False

  ; Apply the discovered position to line A-B in the original coordinate system.
  intersection_x = ax + ABpos * theCos
  intersection_y = ay + ABpos * theSin

 ; Success.
 Return True
	
End Function


Function RayIntersect(ax#,ay#,bx#,by#,cx#,cy#,dx#,dy#,rx# Var, ry# Var)
	
	Local u_b#  = (dy - cy) * (bx - ax) - (dx - cx) * (by - ay)

	If ( u_b <> 0 ) 
		Local ua_t# = (dx - cx) * (ay - cy) - (dy - cy) * (ax - cx)
		Local ub_t# = (bx - ax) * (ay - cy) - (by - ay) * (ax - cx)
		Local ua# = ua_t / u_b
		Local ub# = ub_t / u_b

		If ( 0 <= ua And ua <= 1 And 0 <= ub And ub <= 1 ) 
			rx# = ax + ua * (bx - ax)
			ry# = ay + ua * (by - ay)
			Return True
		EndIf 
	EndIf
EndFunction

rx# and ry# are the points of intersection

Very nice. Thanks Indie! ;)

I forget the "Return False" :)

The intersect funcion in the archives is pretty much the same as the one Indiepath posted.

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

Hmm, not sure how I missed that.

The intersect funcion in the archives is pretty much the same as the one Indiepath posted.

Maths is maths after all.

Not a prob

Just wanted to let you know that there's a function that A) returns a result code and B) provided the intersect coordinates. :)

Maths is maths after all.
And then there's bad maths, as demonstrated in the function I posted. Oh, and sswift's which doesn't even work. :P

For my current needs I just needed to know if an intersection occured, and if so, the coords of that intersection.