210

Suppose I have a line segment going from (x1,y1) to (x2,y2). How do I calculate the normal vector perpendicular to the line?

I can find lots of stuff about doing this for planes in 3D, but no 2D stuff.

Please go easy on the maths (links to worked examples, diagrams or algorithms are welcome), I'm a programmer more than I'm a mathematician ;)

3
  • 3
    And if you want to know on the "maths" behind this, you can look up my answer at stackoverflow.com/a/7470098/189767. It's basically the same, but more elaborate.
    – Andreas
    Jan 25, 2013 at 8:47
  • 4
    This question is about math, not programming.
    – Charlie
    Oct 6, 2015 at 2:41
  • 2
    I'm voting to close this question as off-topic because it is about mathematics, not programming.
    – Pang
    Oct 7, 2015 at 1:34

4 Answers 4

294

If we define dx = x2 - x1 and dy = y2 - y1, then the normals are (-dy, dx) and (dy, -dx).

Note that no division is required, and so you're not risking dividing by zero.

9
  • 19
    It's quite subtle and took me a while to realise normal.x = -dy and normal.y = dx. I had them the other way around because it looked like a typo assigning the x part to the y value...
    – Piku
    Aug 7, 2009 at 13:10
  • 1
    Could you please brief more on how the delta plays a role here? I'm sure I'm missing something here.
    – legends2k
    Jan 25, 2013 at 8:58
  • 8
    @legends2k: The delta is the tangent vector. The normal is the direction perpendicular to the tangent. Flipping the x/y values and negating one becomes obvious if you look at a 2D matrix for 90 deg rotation: en.wikipedia.org/wiki/Rotation_matrix#Basic_rotations
    – geon
    Feb 24, 2013 at 17:24
  • 1
    @Oren Trutner does the result have a length of one? Apr 24, 2014 at 11:42
  • 2
    The original answer was correct - a normal vector is not necessarily a unit vector. I've added / suggested a clarification sentence above though to make it a little clearer.
    – Andy Boura
    Feb 10, 2017 at 10:40
109

Another way to think of it is to calculate the unit vector for a given direction and then apply a 90 degree counterclockwise rotation to get the normal vector.

The matrix representation of the general 2D transformation looks like this:

x' = x cos(t) - y sin(t)
y' = x sin(t) + y cos(t)

where (x,y) are the components of the original vector and (x', y') are the transformed components.

If t = 90 degrees, then cos(90) = 0 and sin(90) = 1. Substituting and multiplying it out gives:

x' = -y
y' = +x

Same result as given earlier, but with a little more explanation as to where it comes from.

4
  • 2
    Thanks a ton, was breaking my head on how it was getting derived.
    – legends2k
    Jan 25, 2013 at 8:19
  • 1
    Although I knew the rotation formula earlier, the thing that clicked inside my head, by this answer, was that the angle is a constant (+/- 90), which simplicifies it to a simple negation and reversal of x and y.
    – legends2k
    Jan 25, 2013 at 8:26
  • @duffymo does the result have a length of one? Apr 24, 2014 at 11:41
  • If the vector is normalized before transformation it will remain so after. You have to normalize either before or after you do the rotational transformation.
    – duffymo
    Apr 24, 2014 at 11:54
16

We know that: if two vectors are perpendicular, their dot product equals zero.

The normal vector (x',y') is perpendicular to the line connecting (x1,y1) and (x2,y2). This line has direction (x2-x1,y2-y1), or (dx,dy).
So,

(x',y').(dx,dy) = 0
x'.dx + y'.dy = 0

The are plenty of pairs (x',y') that satisfy the above equation. But the best pair that ALWAYS satisfies is either (dy,-dx) or (-dy,dx)

9
m1 = (y2 - y1) / (x2 - x1)

if perpendicular two lines:

m1*m2 = -1

then

m2 = -1 / m1 //if (m1 == 0, then your line should have an equation like x = b)

y = m2*x + b //b is offset of new perpendicular line.. 

b is something if you want to pass it from a point you defined

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