Jump to content
  • Advertisement
Sign in to follow this  
chillah

Point on a line

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hey guys, I've got a problem here. I have a line segment from (x0, y0) to (x1, y1). For example, (3,3) to (7,7) in 2d coords. I want to have certain actions happen based on whether a user clicks within 2 units of this line. I get the point they click on, but how can I detect if the point they click on is within that distance (d) from the line? Thanks.

Share this post


Link to post
Share on other sites
Advertisement
But what if the line is diagonal? Will this still work? It seems like it would get anything within a rectangle with corners at (x0, y0) and (x1, y1).

Share this post


Link to post
Share on other sites
Basically what you do is..

- find the perpendicular vector to the line

- to calculate the four corners of the rectangle offset the end points of the line along the perp vector, first in one direction and then the other by 2 pixels.

- to detect if the mouse click is within this none-axis aligned rectangle you need to divide it into 2 triangles and use barycentric coords to determine if the mouse click was within either triangle.

I have the code for this somewhere at home but won't be able to get to it for about 6-7 hours.

Share this post


Link to post
Share on other sites
A slightly easier alternative might be to use a capsule. Basically, this just means finding the shortest distance from the point to the line segment and checking to see if it's within the desired range. Here's some pseudocode:
float DistancePointSegment(Vector3 p, Vector3 a, Vector3 b) {
Vector3 O = a;
Vector3 D = b-a;
Vector3 d = p-a;
float t = (d.Dot(D))/(D.Dot(D));
if (t < 0.0f) t = 0.0f;
else if (t > 1.0f) t = 1.0f;
Vector3 closest = O + t * D;
return (p - closest).Length();
}

bool SelectSegment(Vector3 p, Vector3 a, Vector3 b, float range) {
return DistancePointSegment(p, a, b) <= range;
}
No guarantee that I got that all right, but if you have problems try googling for 'point line distance'. Also, you can substitute squared length for length to eliminate the square root.

Share this post


Link to post
Share on other sites
LineVec = LineP2 - LineP1;
PointLineVec = MousePoint - LineP1;

/* This will give you the distance along the vector where the mouse point would be nearest too.*/
DotValue = DotProduct(LineVec, PointLineVec);


/* Note: The point is behind the first point in the line. */
if (DotValue < 0) {
/* Cap it to an end point. */
DotValue = 0;
}
/* The point is behind the second point in the line. */
else if (DotValue > LineDist) {
/* Cap it to the other end point.
DotValue = LineDist;
}

LineVecNormed = Normalize(LineVec);
PointOnLine = Scale(LineVecNormed, DotValue) + LineP1;

PointDistance = Distance(MousePoint, PointOnLine);

if (PointDistance < tolerance) {
/* Mouse point is reasonably close to line vector or it's end points.*/
}

Share this post


Link to post
Share on other sites
Just to save the OP some time, the above solution is equivalent to the example I posted (in spirit at least - I think it may have a couple of errors). Although performance is probably of little concern here, it is also less efficient due to the (unnecessary) square root. To be fair, there are a few small optimizations that could be made to my version as well, but for illustrative purposes I opted for clarity.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
It is easy to modify the above algorithms to calculate if the point is within the area perpendicular to the line segment rather than off the end of the line segment (making the clickable area a rectange [possibly rotated] rather than a capsule). The above algorithms project the point to the line, then cap the point to be within the line segment. In the cases where capping the value is necessary, the point is outside of the area to the sides of the line segment.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!