Perpendicular on a line segment from a given point

2019-01-13 11:07发布

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 calculate a point D on AB such that CD is perpendicular to AB.

Find point D

I have to find point D.

It quite similar to this, but I want to consider to Z coordinate also as it does not show up correctly in 3D space.

标签: math 3d geometry
9条回答
祖国的老花朵
2楼-- · 2019-01-13 11:55
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

查看更多
3楼-- · 2019-01-13 12:04

Here i have converted answered code from "cuixiping" to matlab code.

function Pr=getSpPoint(Line,Point)
% getSpPoint(): find Perpendicular on a line segment from a given point
x1=Line(1,1);
y1=Line(1,2);
x2=Line(2,1);
y2=Line(2,1);
x3=Point(1,1);
y3=Point(1,2);

px = x2-x1;
py = y2-y1;
dAB = px*px + py*py;

u = ((x3 - x1) * px + (y3 - y1) * py) / dAB;
x = x1 + u * px;
y = y1 + u * py;

Pr=[x,y];

end
查看更多
劳资没心,怎么记你
4楼-- · 2019-01-13 12:07

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)
查看更多
登录 后发表回答