Shift line given horizontal distance and bearing

148 Views Asked by At

I have a line defined by two 2D grid points x1,y1 and x2,y2. I need to shift the line by a perpindicular distan to form a new parallel line that is offset by N (plus or minus).

How can I can calculate the shift in x/y directions to translate my line given N and the bearing (0 to 360) of my original line? If I can calculate the shift I can translate any point on the original line.

1

There are 1 best solutions below

0
On

Honestly, there are so many ways to do this.

For example, you can form the vector in the direction from $(x_1,y_1)$ to $(x_2,y_2)$, namely $(x_2-x_1,y_2-y_1)$.

Since we want to shift by a perpendicular distance, we should find the vector that is in a perpendicular direction to the original one. This is easily found to be $(-(y_2-y_1),x_2-x_1)$.

The shift is to be of a distance $N$, so we find the vector in the direction $(-(y_2-y_1),x_2-x_1)$ with magnitude $N$, namely

$$\pm \frac{N}{\sqrt{(x_1-x_2)^2+(y_1-y_2)^2}}(-(y_2-y_1),x_2-x_1)$$

Hence $(x_1,x_2)$ is shifted to

$$(x_1,x_2)\pm \frac{N}{\sqrt{(x_1-x_2)^2+(y_1-y_2)^2}}(-(y_2-y_1),x_2-x_1)$$

and $(y_1,y_2)$ is shifted to

$$(y_1,y_2)\pm \frac{N}{\sqrt{(x_1-x_2)^2+(y_1-y_2)^2}}(-(y_2-y_1),x_2-x_1)$$