SRL Archive - Help and Support

Getting the Intersection Point of Two Lines.


weibs

2006-11-24 12:45:23

Hey, I've been trying to figure this out for a while, but I can't get it.

I have the starting coords of the first line, and the ending coords of the second line, the staring coords of the second line, and the ending coords of the second line. I need to find the point of intersection of the two lines.

Here's a little picture for a better explanation:


The red line is the first line, and the black line is the second line. I need to find the intersection coordinates, (5, 5) using a formula or something. In this situation, it wouldn't be hard considering the red line is (y = 5) and the black line is (x = 5) so you'd just put the x and y together to get the intersection point of 5, 5. But, the lines I'll be dealing with are more like this:


I know there's some way to do it, I just can't get it.

Thanks in advance,

-Weibs.

masquerader

2006-11-24 13:49:23

m=(x1-x2)/(y1-y2)

y=mx+b
so, b=y-mx

enter a point and solve for y=mx+b, then set the two lines equal

weibs

2006-11-24 13:57:52

I appreciate the fast reply, but…I didn't understand that at all :redface:

Would you mind explaining a little more?

Thanks again.

masquerader

2006-11-24 14:14:36

umm…ok
i'll try to explain

do you know how to get the equation of a line from two points?
that seems like algebra 1 stuff : p
you find the slope by change in y over change in x (rise over run)

you can get the slope in a couple different ways
using the slope intercept form y=mx+b, entering in one of the pairs of the coordinates, you can solve for it

above, i changed it to b=y-mx

so, now you have two different equations in y=mx+b format

to find where the have the same value, you set them equal to each other, and solve for x
so

slope1*x+intercept1=clope2*x+intercept2
factoring and algebra ensue, and you get x:=(b2-b1)/(m1-m2)

i was really bored, so i made you a program!
i think i even figured out how to do vertical lines ;)
(im always doing stuff like this in my calculator for math class)

program WEIBS;
var
line1x1,line1y1,line1y2,line1x2:extended;
line2x1,line2y1,line2y2,line2x2:extended;
change1x,change2x:extended;
m1,b1,m2,b2:extended;
x,y:extended;
begin
//setup
line1x1:=
line1y1:=
line1x2:=
line1y2:=

line2x1:=
line2y1:=
line2x2:=
line2y2:=
//end setup
change1x:=line1x1-line1x2;
change2x:=line2x1-line2x2;
if((change1x=0)and(change2x<>0))then
begin
x:=line1x1;
m2:=(line2y1-line2y2)/change2x;
b2:=line2y1-m2*line2x1;
y:=m2*x+b2;
end else
if((change2x=0)and(change1x<>0))then
begin
x:=line2x1;
m1:=(line1y1-line1y2)/change1x;
b1:=line1y1-m2*line1x1;
y:=m1*x+b1;
end else
if((change2x=0)and(change1x=0))then
begin
writeln('error: two verical lines');
terminatescript;
end else
begin
m1:=(line1y1-line1y2)/change1x;
b1:=line1y1-m1*line1x1;
m2:=(line2y1-line2y2)/change2x;
b2:=line2y1-m2*line2x1;
x:=(b2-b1)/(m1-m2)
y:=m1*x+b1
end;
writeln(floattostr(x)+','+floattostr(y));
end.

weibs

2006-11-24 14:36:29

Thank you so much, I understood a lot of that ( I'm 1/3 way through Algebra 1, we're just starting coordinates and things like that, so we've barely touched the subject of slope, although he's mentioned it ) and I tried it in place of my old procedure and it worked great!

I can't thank you enough!

Boreas

2006-11-24 14:39:32

Is this for the same thing as that other question you asked?

weibs

2006-11-24 14:46:18

Yes :tongue:

Boreas

2006-11-24 14:50:07

Did my answer work?

weibs

2006-11-24 15:17:09

No, but BenLand came up with a formula.

BTW, for some reason, the intersection formula keeps on throwing 0.somethings or numbers that don't fit in with the rest of the numbers, but I have no idea why.. :\