distance from point to line

I'm having problems with the calculation of the distance from a point to a line in a two dimensional space. It's something wrong with the math in this code, but i just can't find the problem. Maybe you can help me with this or have some working functions.

 ``123456789101112131415`` ``````double Detection::distance_to_Line(cv::Point line_start, cv::Point line_end, cv::Point point) { cv::Point line; //direction of the line line.x = line_end.x - line_start.x; line.y = line_end.y - line_start.y; cv::Point point_to_start; //vector from the point to the start of the line point_to_start.x = line_start.x - point.x; point_to_start.y = line_start.y - point.y; int dot = point_to_start.x * line.x + point_to_start.y * line.y; //dot product of point_to_start * line cv::Point distance; //shortest distance vector from point to line distance.x = point_to_start.x - dot * line.x; distance.y = point_to_start.y - dot * line.y; double output = sqrt((double)(distance.x * distance.x + distance.y * distance.y)); return output; }``````

cv:Point is from the OpenCV library. You can just use a 2d instead and post your code, if you don't have the library.
 ``123456789`` ``````double distance_to_line( point begin, point end, point x ){ //translate the begin to the origin end -= begin; x -= begin; //¿do you see the triangle? double area = cross_product(x, end); return area / norm(end); }``````
As a bonus it would tell you if the point is at the right or the left of the line.
Last edited on
I make it something like this (not tested)

 ``123456789101112131415161718`` ``````double Detection::distance_to_Line(cv::Point line_start, cv::Point line_end, cv::Point point) { cv::Point line; //direction of the line line.x = line_end.x - line_start.x; line.y = line_end.y - line_start.y; cv::Point point_to_start; //vector from the point to the start of the line point_to_start.x = line_start.x - point.x; point_to_start.y = line_start.y - point.y; int dot = point_to_start.x * line.x + point_to_start.y * line.y; //dot product of point_to_start * line cv::Point distance; //shortest distance vector from point to line int linelength2 = line.x*line.x+line.y*line.y;//length of line squared distance.x = double(dot * line.x) / linelength2 + line_start.x; distance.y = double(dot * line.y) / linelength2 + line_start.y double output = sqrt((double)(distance.x * distance.x + distance.y * distance.y)); return output; }``````

finally got it to work

 ``123456`` ``````double Detection::distance_to_Line(cv::Point line_start, cv::Point line_end, cv::Point point) { double normalLength = _hypot(line_end.x - line_start.x, line_end.y - line_start.y); double distance = (double)((point.x - line_start.x) * (line_end.y - line_start.y) - (point.y - line_start.y) * (line_end.x - line_start.x)) / normalLength; return distance; }``````

return abs(distance), if you don't want a vector.
Last edited on
`double distance = -line_direction.x * diff.x + line_direction.y * diff.y;` ¿what are you doing here?

If you intend to use my code
 ``123`` ``````double cross_product( point a, point b ){ return a.x*b.y - a.y*b.x; }``````
think that was the dot product and the minus is neccessary to create the normal vector from the direction vector
Topic archived. No new replies allowed.