# How to get the slope of the line perpendicular to another ?

Published: February 19, 2019

Let's consider the line of equation:

y = a * x + b

with the slope a = -2.5 and the intercept b = 5.0 (blue line in the figure). The goal is to find the slope $a'$ of the line perpendicular to it (in orange in the figure).

All the perpendicular lines have the same slope $a'$, so to make the calculation more easier let's take the line with the same intercept that the blue line, i.e:

y' = a' * x + b

Let's consider the points $A=(0,b)$, $B=(x,y)$ and $C=(x,y')$. Then, the two lines are perpendicular if the following scalar product is equal to 0:

\vec{AB}*\vec{AC} = 0

So we have one unknown $a'$ and one equation:

\left( \begin{array}{c}
x \\
y - b
\end{array}\right) * \left( \begin{array}{c}
x \\
y' - b
\end{array}\right)
=0

using equation 1 and 2:

\left( \begin{array}{c}
x \\
ax
\end{array}\right) * \left( \begin{array}{c}
x \\
a'x
\end{array}\right)
=0

calculating the scalar product

x^2 + a * a' * x^2=0

or

a * a' * x^2= - x^2

Finally, we found that

\boxed{a' = \frac{- 1}{a}}

In our example $a'= -1 / (- 2.5) = 0.4$

Code python to plot the figures using matplotlib:

import numpy as np
import matplotlib.pyplot as plt

x_min,x_max = -10.0, 10.0
y_min,y_max = -10.0, 10.0

slope = - 2.5
intercept = 5.0

x = np.linspace(x_min, x_max, 100)
y = slope * x + intercept

fig = plt.figure()

ax = plt.gca()

ax.plot(x, y,linestyle='--')

slope_p = (-1.0 / slope)

y = slope_p * x + intercept
ax.plot(x, y,linestyle='--')

ax.set_aspect('equal')

ax.grid(True)
ax.spines['left'].set_position('zero')
ax.spines['right'].set_color('none')
ax.spines['bottom'].set_position('zero')
ax.spines['top'].set_color('none')

plt.title('How to get the slope of \n the line perpendicular to another ?',fontsize=10)

plt.xlim(x_min,x_max)
plt.ylim(y_min,y_max)

plt.axvline(x=4.0,color='gray',linestyle='--')
plt.quiver([0.0,0.0],[intercept,intercept],[4.0,4.0],[slope * 4.0, slope_p * 4.0], angles='xy', scale_units='xy', scale=1)

plt.text(0.5,4.25, 'A', fontsize=14)
plt.text(3.0,-5.5, 'B', fontsize=14)
plt.text(4.5,7.55, 'C', fontsize=14)

plt.savefig("perpendicular_line_02.png",bbox_inches='tight')