0
votes

I tryed to write code for calculating point-line distance. I found a lot of calculations on the internet but I am not sure if I understand right. I found some equations for lines and it seems like equations for calculating point-plane distance in 3D. I think it is the same but in 2 dimensional.

I have some experiences with 3D point-plane distance calculations. I calculated parameters A,B,C,D from 3 points vec3 (plane definition) and to get distance just apply this equation with vec3 point (x,y,z).

Plane equation: Ax + By + Cz = D

Line equation should be: Ax + By = C

Line equation should work similary by applying equation with vec2 point (x,y).

My problem is how to calculate parameters A,B,C from this equation with 2 points vec2 (line definition)?

Any simply programming/mathematic explanation?

Plane with 3 points:

Plane(vec3 p0, vec3 p1, vec3 p2) {
    vec3 v = p1 - p0;
    vec3 u = p2 - p0;
    vec3 n = cross(v, u);

    normalize(n);

    //Result A,B,C,D
    A = n.x;
    B = n.y;
    C = n.z;
    D = dot(vec3(-n.x, -n.y, -n.z), p0);
}

Plane with 2 points:

Line(vec2 p0, vec2 p1) {

    //...

    //Result A,B,C
    A = (?);
    B = (?);
    C = (?);
}

Thanks.

PS: Sorry for my english. :/

Update:

Solved!

I found answer after hour of calculations and it's similar to plane equation.

Programming:

Line(vec2 p0, vec2 p1) {
    vec2 l = p1 - p0;

    vec2 n = l.cross();
    n.normalize();

    //Result a,b,c 
    a = n.x;
    b = n.y;
    c = vec2::dot(vec2(-n.x, -n.y), p0);
}

Difference is in cross product of vec2. It's someting like this:

vec2 cross(vec2 p) { //Only one parameter
    return vec2(y, -x);
}

Mathematic:

P1 - start point of line

P2 - end point of line

1) N = x(P2 - P1)

Where "x" is cross product of vector (swap elemets and negate element x) ---> x(V) = [Vy, -Vx]

(I'm not sure is this operation is official but result of that should be vector which is perpendicular to that parameter)

2) N = N / |N| (normalize vector N)

Where |N| is length of vector N

3) Result A: a = Nx

4) Result B: b = Ny

5) N' = -N

6) Result C: c = (N').(P1)

Where "(N').(P1)" is dot product of vectors N' and P1

PS:

This formula d = (|(x_2-x_1)x(x_1-x_0)|)/(|x_2-x_1|) is right. It works. But I need to use equation ax + by + c = 0 because I need to know if it's on left or right side from line (positive of negaive distance) and it's better for programming. Thanks for answers.

Still sorry for my english. :D

1

1 Answers

6
votes

It's not clear from your question, but it sounds like you have a line defined by two points, and you want to find the distance between that line and a third point? As you've seen, you can't uniquely determine A,B,C and D from just two points -- there are an infinite number of planes that contain that line.

If x_1 and x_2 are the 3-d vectors representing the points defining the line, and x_0 is the point you're trying to find the distance to, the formula is:

d   =   (|(x_2-x_1)x(x_1-x_0)|)/(|x_2-x_1|)

(In this formula, 'x' denotes the 3-d cross product, '-' denotes vector subtraction, and |v| represents the length of vector v.)

Point-Line Distance--3-dimensional