# How can I find the X/Y coordinates of a parallel line

Disclaimer - I am in no way a mathematician so please cater to my ignorance and explain everything as clearly as you can, if you are able to explain using code rather than notation, please do - If this question would be better suited to another StackExchange site please do tell me and I will move it

I currently have two sets of X/Y coordinates which describe the beginning and end of a line, what I hope to achieve is to plot two parallel lines of equal length either side of the line I have. I would need to be able to vary the distance between the lines. Please see my crude diagram below. In the above image, I have X/Y 1 and 2, but need 3,4,5,6 where i is variable. This is to solve a programming problem, so if you have Python code to hand, that would be ideal, however just an explanation will do the trick.

• This can be done with translation vectors. A vector can be expressed with an $x$ component and a $y$ component. Say you have a line between the points ($X_1$, $Y_1$) and ($X_2$, $Y_2$). Now, if you have a vector $(u,v)$, one of the lines is a line between the points $(X_1+u, Y_1+v)$ and $(X_2+u, Y_2+v)$. The other line will be between the points $(X_1-u, Y_1-v)$ and $(X_2-u, Y_2-v)$. If the two "red" lines need to form a rectangle, not just a generic parallelogram, a restriction is needed on the vector, it must be perpendicular to the line. – Cristhian Grundmann Aug 2 '18 at 21:37
• Thank you for your time Cristhian, I will read up on this and hopefully it will lead me to an answer. Apologies this must seem like "Maths for Kids" :) – Aphire Aug 2 '18 at 21:48

There are many possible approaches but I would probably use an algorithm like this:

Calculate the change in $x$ and the change in $y$ for the original line. Call these $\Delta x$ and $\Delta y$. (For clarity - these are single values and in code you could call them something like "delta_x" and "delta_y".)

$$\Delta x = x_2 - x_1$$

$$\Delta y = y_2 - y_1$$

Now consider the parallel line that is below the original line. To find the coordinates of the ends of this line, you would need to add certain values to the old $x$ and $y$ coordinates. Let's call these $\delta x$ and $\delta y$. (Make up some new names for them in your code!) Now suppose the distance $i$ between the lines was the same as the length of the original line. Then we would be creating a square and the relevant values would be:

$$\delta x = \Delta y$$

$$\delta y = -\Delta x$$

(Note the switch in $x$ and $y$ and the negative sign for $\delta y$. To convince yourself of this, draw a diagram of the square.)

But since $i$ is not in general the same as the length of the original line we need to adjust. First let's calculate the length of the original line, call it $L$. By Pythagoras:

$$L=\sqrt {\Delta x^2 + \Delta y^2}$$ And the relevant adjustment factor is $i / L$, giving:

$$\delta x = \Delta y \times i / L$$

$$\delta y = -\Delta x \times i / L$$

Now you can simply add $\delta x$ and $\delta y$ to each of your original pairs of coordinates for the lower line, and subtract them for the upper line.

Suppose your reference (green) line $L_1$ is described by the equation $ax + by + c = 0$. (We'll see how to find $a, b$ and $c$ later.) Note that there are many tuples $(a,b,c)$ that describe the same line; they're all multiples of each other.

Let $P_1 = (x_1,y_1)$ and $P_2=(x_2,y_2)$ be the two given points on this line.

You need to translate $L_1$ along with $P_1$ and $P_2$ along the direction of a vector normal/orthogonal/perpendicular to $L_1$. One such vector is $(a, b)$. Normalizing this vector to have length one, we have the vector

$$(u, v) = \frac{1}{\sqrt{a^2 + b^2}}(a, b)$$

So $(u, v)$ is a unit vector orthogonal to $L_1$.

Translate $P_1$ and $P_2$ by distance $d$: $$P_3 = P_1 + d(u, v) = (x_1 + du, y_1+dv)\\ P_4 = P_2 + d(u, v) = (x_2 + du, y_2+dv)\\ P_5 = P_1 - d(u, v) = (x_1 - du, y_1-dv)\\ P_6 = P_2 - d(u, v) = (x_2 - du, y_2-dv)$$ Note that $(P_3, P_4)$ and $(P_5, P_6)$ are interchangeable depending on which direction $(u,v)$ points.

Here's how to find $a, b$ and $c$. The defining equation for a line through 2 distinct points $(x_1,y_1)$ and $(x_2, y_2)$ is $$(y_1-y_2)(x-x_1) - (x_1-x_2)(y - y_1) = 0$$ i.e. $$(y_1-y_2)x - (x_1-x_2)y - (y_1-y_2)x_1 + (x_1-x_2)y_1 = 0$$ So you can take $$a = y_1-y_2\\ b = x_2 - x_1\\ c = - (y_1-y_2)x_1 + (x_1-x_2)y_1$$

• Myath, thank you very much for taking the time to write this up, it's appreciated. I will be picking this up in the morning after a large cup of coffee. – Aphire Aug 2 '18 at 22:19