How to find distance between a known point and midway along a hypotenuse?

Viewed 108

Forgive my lack of math knowledge, its been a while since I've needed any of this. I'm attempting to programmatically find the distance between a point and the intersection directly below the point along the hypotenuse of a triangle.

Here is my scenario. All points have known x/y coordinates, I'm just trying to solve for the distance "A". The intersection point is directly vertical. I'll eventually have to do this horizontal as well, but I imagine if I can make it work vertical it shouldn't be too difficult to flip it horizontal.

1 Answers

Supposing your 3 points are P1=(x1,y1), P2=(x2,y2), and P3=(x3,y3) where the line is defined by P1 and P2:

When you project P3 straight down vertically onto the line, the projection point has X coordinate of x3, and Y coordinate (you can derive this from the equation of the line):

y1 + (y2 - y1)*(x3 - x1)/(x2 - x1)

The (signed) distance between P3 and the projection point would then be:

y3 - y1 - (y2 - y1)*(x3 - x1)/(x2 - x1)

The sign of the value tells you on which side of the line P3 is. If you're not interested in that, just take the absolute value (typically abs function in math libraries)

Related