Advertisement

Computing the intersection of two lines????

Started by November 08, 2000 10:40 PM
7 comments, last by Coconut 24 years, 2 months ago
I was wondering... How do you find the point where two lines intersect? This is assuming you have the coordinates (x1,y1,x2,y2) for the lines? If someone would help me with this I would be very grateful, as I am working on a Physics engine for my game that involves a bouncing ball (Hasbro PLEASE don''t sue me!!!!!). Thanks. Brent Robinson "What if this is as good as it gets?"
"The computer programmer is a creator of universes for which he alone is the lawgiver...No playwright, no stage director, no emperor, however powerful, has ever exercised such absolute athority to arrange a stage or a field of battle and to command such unswervingly dutiful actors or troops." - Joseph Weizenbaum-Brent Robinson
Any maths textbook will explain this. Anyway from the top of my head, a line is made up of 2 points. So (x1,y1) & (x2,y2) will only make up one line.

Anyway, lets assume you have two lines:
y1 = m1x1 + b1 and
y2 = m2x2 + b2

Now, assuming the 2 lines cross:
solve the y''s and the x''s by using the simultaneous equation.

Right?
Yes! There are kangaroos in Australia but I haven't seen them...yet
Advertisement
Thanks! I think I have it now... Wait. How would you find the y-intercept for the lines? I know this is probably basic linear algebra, but I decided to go straight to Geometry my freshman year of High School. That said I am still a freshman if anyone is still confused.

Brent Robinson
"What if this is as good as it gets?"
"The computer programmer is a creator of universes for which he alone is the lawgiver...No playwright, no stage director, no emperor, however powerful, has ever exercised such absolute athority to arrange a stage or a field of battle and to command such unswervingly dutiful actors or troops." - Joseph Weizenbaum-Brent Robinson
to find the y-intercept you will have to make x=0;

so if you have this line equation:

y = 3x+7 then the y-intercept would be y= 7; (since x=0)
Yes! There are kangaroos in Australia but I haven't seen them...yet
Though it technically falls under the domain of linear algebra, this is often referred to simply as 'Algebra'.

y= m*x + b



m1 = y1 / x1; //rise over run
b1 = y1 - m1 * x1; //substitute a point, any point will do

m2 = y2 / x2;
b2 = y2 - m2 * x2;

so we have:

y = m1*x + b1
y = m2*x + b2

so long as (m1<>m2 xor b1<>b2) a solution exist (if m1==m2 && b1==b2 the are the same line)

m2*x + b2 = m1*x + b1
m2*x - m1*x = b1 - b2
(m2 - m1)*x = b1 - b2
x = (b1 - b2) / (m2 - m1)

substitute x to yield
y = m1 * ((b1 - b2)/(m2 - m1)) + b1

the point defined by the equations for x & y above is the point of intersection.

...
Did I just do your homework for you?


Edited by - Magmai Kai Holmlor on November 8, 2000 12:28:29 AM
- The trade-off between price and quality does not exist in Japan. Rather, the idea that high quality brings on cost reduction is widely accepted.-- Tajima & Matsubara
Ok, above have explained it pretty well, but I''ll give it try as well

We all know the basic equaion of a line i 2D:

y = k*x + m;

where[k] = y/x deltay(xsteps per step in y-direction)
and [m] = the value of y when x=0

ok, say we have two lines.

1: y = 2x + 2;
2: y = -x + 5;

Ok, substitute away Y : -x + 5 = 2x + 2;
to find the x coordinate then there should be
a soulution for this equation, lets try...

3 = 3x;
x = 1;

Now, when you know on which x they intersect, it''s easy
to find the y. Just plug it in to any of the two equations.
y = 2*1 +2;
y = 4;

yep, they intersected at 1,4.

Lets take two paralell lines and do the same task.

y = 2x + 1;
y = 2x + 5;

These two should not intersect (hopefully )

lets give it a try:
2x + 1 = 2x + 5;
0 = 4

Nop, just as we wanted, now it is just up to you implementing this in C/C++ or whatever you want, its 00:41, so I cant really think clearly anymore

/ Tooon
Advertisement
Hey, Thank you sooo much guys! That really helped in my game''s physics clipping, where I am trying to find the point where the ball''s path intersects a leg of a rectangle. I then just check to make sure the point is contained within those segments. In case your wondering, I am pretty new to programming and just learning the basics by making a similar to BREAKOUT. Thanks again!

Brent Robinson
"What if this is as good as it gets?"
"The computer programmer is a creator of universes for which he alone is the lawgiver...No playwright, no stage director, no emperor, however powerful, has ever exercised such absolute athority to arrange a stage or a field of battle and to command such unswervingly dutiful actors or troops." - Joseph Weizenbaum-Brent Robinson
Sorry guys, but one more thing. How do I compute the intersection of 2 lines where one is either Horizontal or vertical. I''m sorry, but I can''t seem to figure anything out today. Thanks.

Brent Robinson
"What if this is as good as it gets?"
"The computer programmer is a creator of universes for which he alone is the lawgiver...No playwright, no stage director, no emperor, however powerful, has ever exercised such absolute athority to arrange a stage or a field of battle and to command such unswervingly dutiful actors or troops." - Joseph Weizenbaum-Brent Robinson
horizontal is easy, the same as above

verticle is a problem

special case i guess...
if one line is verticle, and the other isn''t they intersect, and the x value is the value of the verticle line, feed into the other line''s equation to get y value.

if both are verticle, and b1=b2, then they are the same line...


...
- The trade-off between price and quality does not exist in Japan. Rather, the idea that high quality brings on cost reduction is widely accepted.-- Tajima & Matsubara

This topic is closed to new replies.

Advertisement