views:

324

answers:

2

If I have a polyline that describes a road and I know the road width at all parts, is there an algorithm I can use to determine if a point is on the road? I'm not entirely sure how to do this since the line itself has a width of 1px.

thanks,

Jeff

+2  A: 

Find the minimum distance of the point to the line (it will be a vector perpendicular to the line). Actual calculation where P0 is the first point of the road segment, v is the road segment vector and w is the vector from P0 to the point in question. You will have to iterate over each edge in the polyline. If the distance is less than the width of that segment, then it is "on" the road.

d = |v x w| / |v|

The corners might be tricky depending on if you treat them as rounded (constant radius) or angular.

p00ya
+2  A: 

Perhaps you could take each line segment, build the rectangle of the line segment + its width, and use rectangle/point collision algorithms to determine if the rectangle contains the point. A good algorithm will account for the width = 1 scenario, which should simply attempt to build the inverse function of the line segment and determine if y-1(point.y) is an x between line_segment.x1 and line_segment.x2

Stefan Kendall