TITLE: Given two points, find another point a perpendicular distance away from the midpoint
QUESTION [8 upvotes]: I am a computer programmer and need to find the x and y coordinate of a point that is a defined perpendicular distance from a midpoint.  For reference, I have tried to attached an image for reference.
X1, Y1, D, X2, Y2 will be given.  I need to find X3, Y3.  The shorter line, will always be perpendicular from the larger line and will always intersect the midpoint between (x1, x2) and (x2, y2).  I have been struggling with this a while and cannot figure it out.  Any help would be greatly appreciated.

REPLY [3 votes]: Given $(x_1, y_1), (x_2, y_2)$ the midpoint $P$ is
$$P = (X, Y) = \left(\frac{x_1 + x_2}{2}, \frac{y_1 + y_2}{2}\right).$$
The slope of the perpendicular is the negative reciprocal of the slope of the line segment:
$$m = -\frac{x_2 - x_1}{y_2 - y_1}.$$
Then, the line going through the midpoint perpendicular to your line segment is defined by
$$y - Y = m(x - X),$$
which allows you to solve for $y$ in terms of $x$ for all points on the perpendicular.
Finally, find the values of $x_3,y_3$ such that
$$(X-x_3)^2 + (Y-y_3)^2 = d^2.$$
You should get two choices (or one, if $d=0$ -- in that case, it's just $P$!)