Lines intersect
Blitz3D Forums/Blitz3D Beginners Area/Lines intersect
| ||
I was in the middle of a 2d game and I realized that I needed a linesintersect function. I thought it would be simple so I tried to make one and soon realized just how hard it was. I could not find anything in the code archives either. I guess what I am asking is does anyone have a lines intersect function that is efficient and fast? Any help is greatly appreciated :) |
| ||
http://blitzbasic.com/codearcs/codearcs.php?code=471 This one by sswift in the code archives should be fine. |
| ||
That one of sswifts doesn't work, as someone has pointed out in the comments. There are several other one's in the archives that do work, though. [edit] Actually, here's the functions I have for line intersection: ; ; Determines whether two lines intersect without calculating the actual point of intersection. ; ; 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 lines_intersect(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) Then Return True EndIf Return False End Function Global intersect_x#, intersect_y# ; ; Determines whether two lines intersect, or not. If the lines do intersect, the ; point of intersection is returned in the global variables intersect_x and 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 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) intersect_x = x1 + (ua * dx1) intersect_y = y1 + (ua * dy1) Return True EndIf EndIf Return False End Function |
| ||
Here is a chunk of code using a modified version of sswifts. |
| ||
Thank you so much pongo for the modified version. I like it the most :) thanks to everyone else who helped. P.S. Is there a way to do a search on the code archives? I can't seem to do a search on them. I just have to look through them manually. I guess that is why I have to ask for things like this |