Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

DarkBASIC Professional Discussion / Getting intersection point of two lines?

Author
Message
TheComet
18
Years of Service
User Offline
Joined: 18th Oct 2007
Location: I`m under ur bridge eating ur goatz.
Posted: 19th Dec 2010 21:43
Hi!


So this is pretty straight forward:



How can I find the intersection point when given the start and end points of both lines? (ax,ay,bx,by,cx,cy,dx,dy)

Thanks, TheComet

Indicium
18
Years of Service
User Offline
Joined: 26th May 2008
Location:
Posted: 19th Dec 2010 22:26
Guido Italy
20
Years of Service
User Offline
Joined: 25th Dec 2005
Location:
Posted: 19th Dec 2010 22:51
can you translate in dbpro? ( ..... )
Sixty Squares
20
Years of Service
User Offline
Joined: 7th Jun 2006
Location: Somewhere in the world
Posted: 20th Dec 2010 00:05 Edited at: 20th Dec 2010 00:10
I just did the algebra. It was based on the POINT SLOPE form of a line (it was just solving a system of 2 equations, one for each line). Here's a function to find the intersection. The result is stored in the global variables XIntersect# and YIntersect#. Click the mouse w/ the LMB and RMB to move the endpoints around. A circle marks the intersection point.



There's probably a faster way to do it, but this gets the job done as precisely as floats will allow .


Guns, cinematics, stealth, items and more!
Madscientist
16
Years of Service
User Offline
Joined: 23rd Aug 2009
Location: Between a rock and a hard place
Posted: 20th Dec 2010 00:22 Edited at: 20th Dec 2010 00:24
Wow Sixty Squares, your fast. You beat me to it!
But anyways here's the actual math behind it.
am=(ay-by) <- Slope of line ab
````(ax-bx)

bm=(cy-dy) <- Slope of line cd
````(cx-dx)

ab=ay-am(ax) <- Gets the Y intercept of ab
bb=cy-bm(cx) <- Gets the Y intercept of cd

am(IntX)+ab=bm(IntX)+bb <- Set them equal since their intersection will have the same Y coordinate
IntX=(bb-ab) <- Simplifies to this
````(am-bm)

IntY=am(IntX)+ab <- Plugs the X intercept into the original equation since it will come out to the Y intersection.
Your intersection will be at IntX,IntY

Hope this explanation gives you an understanding of what Sixty Squares did.

My computer surpasses all the technologies of the day. What computer do I have?

Sixty Squares
20
Years of Service
User Offline
Joined: 7th Jun 2006
Location: Somewhere in the world
Posted: 20th Dec 2010 00:53 Edited at: 20th Dec 2010 00:59
@Madscientist: .
Thanks for explaining that, I guess I should have written the math out . Your way looks a little different from what I did as it uses slope-intercept form, but I hadn't thought about doing it that way. Maybe it's faster .

----------------------------
I did this:

Found the slopes:
mab = (by - ay)
``````(bx - ax)

mcd = (dy - cy)
``````(dx - cx)


Wrote the equations of the two lines in point-slope form (Y - y1) = m(x - x1):
IntY - ay = mab * (IntX - ax)
IntY - cy = mcd * (IntX - cx)


Solved for IntY in both cases:
IntY = mab * (IntX - ax) + ay
IntY = mcd * (IntX - cx) + cy


Set them equal to each other, since the intersection is the point at which the X and Y coordinates of both lines are the same. (Thus IntY on line ab must be equal to IntY on line cd):
mab * (IntX - ax) + ay = mcd * (IntX - cx) + cy


Solved for IntX (the X coordinate at which the intersection occurs, i.e. the X coordinate at which the two IntY's are the same):
IntX = mab * ax - ay - mcd + yc
```````(mab - mcd)


Then substituted that back into one of the equations for IntY (I arbitrarily chose the first one)
IntY = mab * (IntX - ax) + ay


And the intercepts are at IntX and IntY. Same answer as Madscientist but a slightly different method


Guns, cinematics, stealth, items and more!
29 games
20
Years of Service
User Offline
Joined: 23rd Nov 2005
Location: not entirely sure
Posted: 22nd Dec 2010 00:39
In addition to the answers above, you have to be careful with division by zero errors.

These will come about if one of the lines is verticle (ie ax# = bx# and or cx# = dx#) or if the lines are parallel (mab# = mcb#).

I played around with Sixty Squares' code, making vetical lines or parallel lines and although it didn't crash the program, it did come up with incorrect answers for the intersect point. I changed the coordinates to the following:

ax#=5
ay#=5
bx#=5
by#=425

cx#=10
cy#=450
dx#=10
dy#=0


and it gave an intersect point of 0,5, which is impossible as the lines are parallel.

Obviously if the lines are parellel then there can not be an intersection but if one of the lines is vertical the two lines still intersect so you need to account for this in your code.

Example: if ax# = bx# then the line ab is vertical which means the x intersect will be equal to ax# (or bx#)

Also, the maths apply to lines of infinite length and so will return an intersection even if the lines themselves do not actually cross. This may or may not be an issue depending on what you're doing.

I looked at this same problem earlier this year and decided that using a simillar method to determining point in triangle can also be used for two lines crossing:





The important bit in the code is the function "POINTS_ON_SAME_SIDE", which was originally written as part of some point in triangle code (the basic theory for how this works can be found here http://www.blackpawn.com/texts/pointinpoly/default.html).

Note that this doesn't actually determine the intersect.

However, using the exact same maths as everyone else, code for determining the intersect can be added:




You may note that I use X and Z coordinates as this code was originally for 3D maths collision and I have calculated the slopes to match how angles are measured on the XZ plane but it work perfectly well in 2D (Z just becomes Y).

Just to pre-empt any off topic debate, rather than have a single function and global variables to calculate the intersect point, I've used two functions which is just my style (I prefer to be able to copy and paste functions without having to worry about global variables) and I appreciate that this may not be the most effecient way of going about things
TheComet
18
Years of Service
User Offline
Joined: 18th Oct 2007
Location: I`m under ur bridge eating ur goatz.
Posted: 23rd Dec 2010 22:11
Thanks everyone! That's exactly what I needed to complete collision detection in my game for NaGaCreMo : http://forum.thegamecreators.com/?m=forum_view&t=179190&b=8


Cheers,

TheComet

TheComet
18
Years of Service
User Offline
Joined: 18th Oct 2007
Location: I`m under ur bridge eating ur goatz.
Posted: 23rd Dec 2010 23:46
Sorry guys, but another problem has arisen... It happens when I use your LINES_CROSS function. Here's how I set it up:



The function will not return any intersections with the blue line, even though dx=bx and dy=by. Is there a way to adapt the calculations so it does detect intersection? I'd do it myself, but this level of maths is still a little high for me

Thanks in advance!

TheComet

29 games
20
Years of Service
User Offline
Joined: 23rd Nov 2005
Location: not entirely sure
Posted: 24th Dec 2010 01:06 Edited at: 24th Dec 2010 01:07
I think I've solved this.

I've modified the SAME_SIDE function so if one of the points is on the line and the other isn't it treats them as being on different sides (ie the lines have crossed).



This should give you what you're after.
Phaelax
DBPro Master
23
Years of Service
User Offline
Joined: 16th Apr 2003
Location: Metropia
Posted: 24th Dec 2010 01:13
You can always checkout my website which has the equations of several types of intersection:
http://dbcodecorner.com/?page=eq

"Only the educated are free" ~Epictetus
"Imagination is more important than knowledge..." ~Einstein
Neuro Fuzzy
19
Years of Service
User Offline
Joined: 11th Jun 2007
Location:
Posted: 24th Dec 2010 08:35
._. big code scares me



Login to post a reply

Server time is: 2026-07-21 20:35:20
Your offset time is: 2026-07-21 20:35:20