Perpendicular on a line segment from a given point

后端 未结 9 1517
小鲜肉
小鲜肉 2020-12-02 11:52

I want to calculate a point on a given line that is perpendicular from a given point.

I have a line segment AB and have a point C outside line segment. I want to ca

相关标签:
9条回答
  • I didn't see this answer offered, but Ron Warholic had a great suggestion with the Vector Projection. ACD is merely a right triangle.

    1. Create the vector AC i.e (Cx - Ax, Cy - Ay)
    2. Create the Vector AB i.e (Bx - Ax, By - Ay)
    3. Dot product of AC and AB is equal to the cosine of the angle between the vectors. i.e cos(theta) = ACx*ABx + ACy*ABy.
    4. Length of a vector is sqrt(x*x + y*y)
    5. Length of AD = cos(theta)*length(AC)
    6. Normalize AB i.e (ABx/length(AB), ABy/length(AB))
    7. D = A + NAB*length(AD)
    0 讨论(0)
  • 2020-12-02 12:20

    A point on line AB can be parametrized by:

    M(x)=A+x*(B-A), for x real.

    You want D=M(x) such that DC and AB are orthogonal:

    dot(B-A,C-M(x))=0.

    That is: dot(B-A,C-A-x*(B-A))=0, or dot(B-A,C-A)=x*dot(B-A,B-A), giving:

    x=dot(B-A,C-A)/dot(B-A,B-A) which is defined unless A=B.

    0 讨论(0)
  • 2020-12-02 12:21
    function getSpPoint(A,B,C){
        var x1=A.x, y1=A.y, x2=B.x, y2=B.y, x3=C.x, y3=C.y;
        var px = x2-x1, py = y2-y1, dAB = px*px + py*py;
        var u = ((x3 - x1) * px + (y3 - y1) * py) / dAB;
        var x = x1 + u * px, y = y1 + u * py;
        return {x:x, y:y}; //this is D
    }
    

    question

    0 讨论(0)
提交回复
热议问题