• We should now be fully online following an overnight outage. Apologies for any inconvenience, we do not expect there to be any further issues.

What's the distance formula for this problem?

watdahel

Golden Member
Jun 22, 2001
1,661
12
81
www.youtube.com
Let's say I have 2 vertices with known coordinates, that form a line segment. I have another vertex; what's the best way to find out the distance of this vertex from the line segment? I guess I need the length of the line perpendicular to the line segment from this vertex.
 

Firus

Senior member
Nov 16, 2001
525
0
0
You need to find some sort of relation between the other vertices and the one you are trying to find the distance to
 

cjr22

Member
Mar 21, 2003
65
0
0
If your two original vertices are a and b, then the vector of the line running between them is a+n(b- a). IF your third vertex is c then the distance from c to a point on the line is the length of a+n(b- a) - c. IF you minimise this formula wrt n, then you'll get the minimum distance.
 

notfred

Lifer
Feb 12, 2001
38,241
4
0
Assume A and B are your endpoints and C is your other point.

convert the coordinates to polar coordiantes, make point A the origin, and rotate B and C around the origin until B lies alogn the X axis of the previous rectangular coordinate system. Convert back to rectangular coordinates.

Your line segment is now along the X axis. If the X value of C is between the X value of A and B, then the distance is the Y coordinate of C. Otherwise, it's the distance to the nearest endpoint.

That's how I'd do it.
 

Bulldog13

Golden Member
Jul 18, 2002
1,655
1
81
Reminds me of the day of my Calc 3 final (assuming this isn t geometry or something :D )

"Screw this, I ll wake up at 5 in the morning and study all the different types of coord system and how to convert them. Midterm is at 9, will give me plenty of time"

:: hits the hay ::

:: Sleeping blissfully ::

Roommate : "Bulldog, bulldog...."

Me : "SSSHhhhhh, sleeping"

Roommate : "Ain t you got a final today ?"

Me : ::glances at clock :: 8:57 it reads

Me: OH SH*T, HOLY F*CK

Roommate : "Hows that studying going ?"

Me : "Die"

I ran straight to class in flip flops, shorts, t shirt and leather jacket. Nevermind it was poring.

Still swung a B+ too.
 

watdahel

Golden Member
Jun 22, 2001
1,661
12
81
www.youtube.com
Thanks for that code. Did you wrote that up just for me? What's the order of input? Is it x2,y2,x1,y1,x3,y3.

Below is an equation I found that seems to show the same results as your algorithm when I enter the input in the order above.

d= |(x2-x1)(y1-y0) - (x1-x0)(y2-y1)| / sqrt( (x2-x1)² + (y2-y1)² )
 

notfred

Lifer
Feb 12, 2001
38,241
4
0
Yeah, I wrote that in repsonse to this question. The order of input is in the comments above the loop that takes input.

Your algorithm is shorter, but I came up with mine myself :p