distance from point to line

Darkmaster (311)
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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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.
ne555 (4034)
1
2
3
4
5
6
7
8
9
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
mik2718 (295)
I make it something like this (not tested)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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;
}

Darkmaster (311)
finally got it to work

1
2
3
4
5
6
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
ne555 (4034)
double distance = -line_direction.x * diff.x + line_direction.y * diff.y; ¿what are you doing here?

If you intend to use my code
1
2
3
double cross_product( point a, point b ){
   return a.x*b.y - a.y*b.x;
}
Darkmaster (311)
think that was the dot product and the minus is neccessary to create the normal vector from the direction vector
Registered users can post here. Sign in or register to post.