# Calculate Points for a Parallel Line

Given a line running through p1:(x1,y1) and p2:(x2,y2),

I need to calculate two points such that a new parallel line 20 pixels away from the given line runs through the two new points.

Edit: The newline can be 20 pixels in either direction (i.e., pick a direction; it does not matter).

-
20 pixels away - in which direction? – Gerry Myerson Nov 20 '11 at 23:30
Gerry, it can be 20pixels in either direction. – Mark P. Nov 20 '11 at 23:35
There are infinitely many directions. Which two do you have in mind? – Gerry Myerson Nov 21 '11 at 0:56
It's a 2D plane. If for example the given line was vertical,it could be 20 pixels to the left or 20 pixels to the right. – Mark P. Nov 21 '11 at 4:01
Suppose the line makes a 30 degree angle with the horizontal. Do you want a line 20 pixels to the left? or a line 20 pixels above? or a line 20 pixels away, measured along a perpendicular to the first line? They are all different, and that's just three of the infinitely many different ways to measure the distance between two lines. – Gerry Myerson Nov 21 '11 at 4:55

The slope of your line is $m=\frac{y_2-y_1}{x_2-x_1}$, and the slope of the perpendicular is $\frac{-1}{m}=\frac{-(x_2-x_1)}{y_2-y_1}$. You want a segement along the perpendicular of length $20$. So if the $x$ offset is $\Delta x$ and the $y$ offset is $\Delta y$, we have $\Delta y=\frac{-(x_2-x_1)}{y_2-y_1}\Delta x$ and using the length $20=\Delta x \sqrt{1+(\frac{-(x_2-x_1)}{y_2-y_1})^2}$ where you choose the sign of the square root to get the correct side of the line you can find $\Delta x$.
The interpretation that you mean a perpendicular distance of 20 fits with there being only two possible locations for the translated line segment. The vector $\langle x_2-x_1,y_2-y_1\rangle$ is along the given line; the vector $\langle y_2-y_1,-x_2+x_1\rangle$ is orthogonal (perpendicular) to the given line (in one particular direction—$\langle -y_2+y_1,x_2-x_1\rangle$ would be in the other direction); and the vector \begin{align} \frac{20\langle y_2-y_1,-x_2+x_1\rangle}{||\langle y_2-y_1,-x_2+x_1\rangle||} &=\frac{20}{\sqrt{(y_2-y_1)^2+(-x_2+x_1)^2}}\langle y_2-y_1,-x_2+x_1\rangle\\ &=\left\langle\frac{20(y_2-y_1)}{\sqrt{(y_2-y_1)^2+(-x_2+x_1)^2}},\frac{20(-x_2+x_1)}{\sqrt{(y_2-y_1)^2+(-x_2+x_1)^2}}\right\rangle \end{align} is orthogonal to the given line and has length 20. Translate your two given point by this vector and you'll have two points on the line you want: $$(x_1,y_1)\to\left(x_1+\frac{20(y_2-y_1)}{\sqrt{(y_2-y_1)^2+(-x_2+x_1)^2}},y_1+\frac{20(-x_2+x_1)}{\sqrt{(y_2-y_1)^2+(-x_2+x_1)^2}}\right)$$ $$(x_2,y_2)\to\left(x_2+\frac{20(y_2-y_1)}{\sqrt{(y_2-y_1)^2+(-x_2+x_1)^2}},y_2+\frac{20(-x_2+x_1)}{\sqrt{(y_2-y_1)^2+(-x_2+x_1)^2}}\right)$$