Perpendicular on a line from a given point

前端 未结 14 1632
名媛妹妹
名媛妹妹 2020-11-29 18:34

How can I draw a perpendicular on a line segment from a given point? My line segment is defined as (x1, y1), (x2, y2), If I draw a perpendicular from a point (x3,y3) and it

14条回答
  •  抹茶落季
    2020-11-29 19:08

    Matlab function code for the following problem

    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
    

提交回复
热议问题