# Distance betwwen line and point? 0

This topic is 4990 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Does anyone have a simple, quick and clean algorithm to find the shortest distance between a line (denoted by x1, z1, x2, z2) and a point (x0, z0)? Something to just cut ‘n’ paste?

##### Share on other sites
Quote:
 Does anyone have a simple, quick and clean algorithm to find the shortest distance between a line (denoted by x1, z1, x2, z2) and a point (x0, z0)?
Yes:

Vector2 O(x1, z1);
Vector2 D(x2-x1, z2-z1);
Vector2 diff(x0-x1, z0-z1);
float t = diff.Dot(D) / D.Dot(D);
if (t >= 1.0f) diff -= D;
else if (t >= 0.0f) diff -= t*D;
return diff.Dot(diff); // Squared length

##### Share on other sites
Distance between a line and a point is defined as:

d = (| b x (p-a) |) / |b|

Where p is a vector containing the point, a is the origin of the line, and b is the vector of the line. Ill let you put that into code...

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 11
• 15
• 11
• 11
• 9
• ### Forum Statistics

• Total Topics
634151
• Total Posts
3015826
×