Search code examples
mathgeometry2dcomputational-geometry

Perpendicular on a line from a given point


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 meets to line on point (x4,y4). I want to find out this (x4,y4).


Solution

  • I solved the equations for you:

    k = ((y2-y1) * (x3-x1) - (x2-x1) * (y3-y1)) / ((y2-y1)^2 + (x2-x1)^2)
    x4 = x3 - k * (y2-y1)
    y4 = y3 + k * (x2-x1)
    

    Where ^2 means squared