[Math] How to find the X/Y coordinates of a parallel line

geometry

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.

enter image description here

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.

Best Answer

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.

Related Question