I struggle to find the language to express what I am trying to do. So I made a diagram.


So my original line is the red line. From (2.5,2.5) to (7.5,7.5).

I want to shift the line away from itself a certain distance but maintaining the original angle of the line(so move it a certain distance away at a 90 degree angle). So after the shifting the line by the distance of +1 it would become the blue line or by -1 it would be come the yellow line.

I don't know a lot of the maths terminology so if anybody could manage an explanation in layman terms it would be appreciated.

Thanks, C

  • 1
    $\begingroup$ The technical term is parallel translation. Note that the distance between the red line and the blue line is not $1$ but $\sqrt2\approx1.414$ — $\endgroup$ – Lubin Jan 5 '18 at 20:48
  • $\begingroup$ And what exactly do you want to see? I mean, the equations? Or is this related to programming? $\endgroup$ – user316769 Jan 5 '18 at 20:53
  • $\begingroup$ And is your line just an example, and do you want to know it for any general line? Or are you just interested in this particular (45 degree) case? $\endgroup$ – user316769 Jan 5 '18 at 20:54
  • 1
    $\begingroup$ How do you define the line, Code? Or is it a line segment? Using two points? As an equation (like $y = x + 5$ or similar)? Using one point and a direction vector? A starting point, angle, and length? Mathematically they are all equivalent (except only a line segment has length; lines are technically infinite length), but knowing your starting point will let others to try and write their answers in a way most likely familiar/easy to comprehend to you. $\endgroup$ – Nominal Animal Jan 5 '18 at 23:11
  • 1
    $\begingroup$ @NominalAnimal The line is defined by have a start and end point. So (2.5,2,5) and (7.5,7.5). So what I want to be able to achieve is to move do a parallel translation (thanks Lubin) a certain distance. Is there a formula to use when you know the start and endpoints of the original line and you know the distance you want to do the translation? $\endgroup$ – Code Jan 6 '18 at 11:49

Let's use basic vector algebra. (This is extremely useful for any kind of graphics programmers, so if you are not familiar with the basics yet, I warmly recommend you look up some tutorials on the net first. Basics of linear algebra, namely matrices, and vector-matrix and matrix-matrix multiplication, is of tremendous help with transformations (rotations et cetera). If you add unit quaternions (also known as versors) describing rotations, and descriptive geometry basics (how to do projections), you've got a very powerful mathematical toolbox to work with all kinds of 2D and 3D graphics.)

Let's say you have a line segment between points $\vec{p}_1 = (x_1 , y_1)$ and $\vec{p}_2 = (x_2 , y_2)$, and you wish to do a parallel translation by $d$. (If $d$ is positive, when we look from $\vec{p}_1$ towards $\vec{p}_2$, the translation is to the left, if $x$ axis increases right and $y$ axis up; if $d$ is negative, to the right. This choice is arbitrary, but you'll see this choice made often in vector algebra tutorials, so it'll feel and look familiar to many.)

Step 1.

Let's define $\hat{n}$ as the unit direction vector from $\vec{p}_1$ towards $\vec{p}_2$. (Unit vector means its length is 1, i.e. $\left\lVert\hat{n}\right\rVert = 1$. The hat mark, $\hat{\cdot}$, is often (but not always) used to distinguish unit vectors from all other vectors, $\vec{\cdot}$; I use that convention here just to make that point, to convey the math clearer.)

We calculate $\hat{n}$ by $$\hat{n} = \frac{\vec{p}_2 - \vec{p}_1}{\left\lVert \vec{p}_2 - \vec{p}_1 \right\rVert} \tag{1a}\label{NA1a}$$ If we use $\hat{n} = ( x_n , y_n )$, then in coordinate form, $$\begin{cases} x_n = \frac{x_2 - x_1}{\sqrt{ (x_2 - x_1)^2 + (y_2 - y_1)^2 }} \\ y_n = \frac{y_2 - y_1}{\sqrt{ (x_2 - x_1)^2 + (y_2 - y_1)^2 }} \end{cases} \tag{1b}\label{NA1b}$$

Step 2.

Rotating $\hat{n} = ( x_n , y_n )$ 90° counterclockwise yields $\hat{v} = ( -y_n , x_n )$.


In two dimensions, counterclockwise rotation (this is the choice I made above) by angle $\varphi$ is described by matrix $\mathbf{R}$, $$\mathbf{R} = \left [ \begin{array}{cc} \cos\varphi & -\sin\varphi \\ \sin\varphi & \cos\varphi \end{array} \right ]$$ so that the rotation of a vector $\vec{p} = ( x , y )$ by $\varphi$ yields $\vec{p}^{,}$: $$\vec{p}^{,} = \mathbf{R}\vec{p} \iff \begin{cases} x^{,} = x \cos(\varphi) - y \sin(\varphi) \\ y^{,} = x \sin(\varphi) + y \cos(\varphi) \end{cases}$$ Note that a common "bug" in first implementations is to not realize that you must use the old coordinates for the entire calculation; you cannot do say $x$ first, and use the new $x^{,}$ when calculating $y^{,}$.

In our current particular situation, $\varphi = 90°$, and therefore $$\mathbf{R} = \left [ \begin{array}{cc} 0 & -1 \\ 1 & 0 \end{array} \right ]$$ or, in coordinate form, $$\begin{cases} x^{,} = x 0 - y 1 = - y\\ y^{,} = x 1 + y 0 = x \end{cases}$$

Step 3.

Move the line segment along the rotated unit vector $\hat{v} = ( -y_n , x_n )$ by the desired distance $d$.

If we use $\vec{p}_3 = ( x_3 , y_3 )$ and $\vec{p}_4 = ( x_4 , y_4 )$ for the translated line segment endpoints $\vec{p}_1$ and $\vec{p}_2$, respectively, then

$$\begin{cases} \vec{p}_3 = \vec{p}_1 + d \hat{v} \\ \vec{p}_4 = \vec{p}_2 + d \hat{v} \end{cases} \tag{2}\label{NA2}$$


In coordinate form, and summarizing all you need to calculate to do this programmatically: $$\begin{array}{l} r = \sqrt{ (x_2 - x_1)^2 + (y_2 - y_1)^2 } \\ x_\Delta = \frac{d}{r} ( y_1 - y_2 ) \\ y_\Delta = \frac{d}{r} ( x_2 - x_1 ) \\ x_3 = x_1 + x_\Delta \\ y_3 = y_1 + y_\Delta \\ x_4 = x_2 + x_\Delta \\ y_4 = y_2 + y_\Delta \end{array}\tag{3}\label{NA3}$$ with the translated line segment being from $( x_3 , y_3 )$ to $( x_4 , y_4 )$.

  • $\begingroup$ nice and clean solution. $\endgroup$ – Peter Pohlmann Jul 18 '19 at 17:45

The line equation between $(2.5,2.5)$ to $(7.5,7.5)$ is:


For a vertical shifting of $\pm1$, the equation for blu line and yellow line are



For a parallel shifting of $\pm1$, the equation for blu line and yellow line are

$$y=x+\frac{\sqrt 2}{2}$$

$$y=x-\frac{\sqrt 2}{2}$$

Thus if you want a (orthogonal/parallel) distance $\pm d$ between the lines you ave to move vertically by $\pm\frac{d\sqrt 2}{2}$.

  • $\begingroup$ Hi @gimusi All I have at the start are the star(2.5,2.5)t and end point(7.5,7.5) of the line and the distance I want to translate. How do I plug those values in to your formula? Thank you, Code $\endgroup$ – Code Jan 6 '18 at 11:52
  • 1
    $\begingroup$ @ Code I have slightly changed your graph of the 3 lines. Hope OK. $\endgroup$ – Narasimham Jan 6 '18 at 17:57

You forgot marking the axes.

In the straight line $y=mx+c$, the $c$ or $y$ coordinate part to be added is $\dfrac{1}{\sqrt2}= \sin 45^{\circ}$ if you want shift up parallelly,

and it is $\dfrac{-1}{\sqrt2}$ if you want shift down parallelly. Here slope $m=1$;

So red line

$$ y= m x +0 $$

becomes blue line above

$$ y= mx+ \dfrac{1}{\sqrt2}$$

and the yellow line below

$$ y= mx - \dfrac{1}{\sqrt2}$$

as shown in the graph with axes marked. Also if you are looking for a variable distance between lines the polar form is convenient:

$$ x \cos\alpha + y \sin \alpha = p $$

$p$ is pedal (normal) length from origin. and $ \alpha $ is positive angle pedal makes to x-axis. In the graphs you respectively have

$$ \alpha = 3 \pi/4 ; p_{blue,red,yellow}= ( \sqrt 2, 0, -\sqrt 2) $$


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.