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.
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