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 ;)

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

4 Answers4

292

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.

funie200
  • 3,688
  • 5
  • 21
  • 34
Oren Trutner
  • 23,752
  • 8
  • 54
  • 55
  • 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 07 '09 at 13:10
  • @OrenTrutner I still don't understand this; `(x', y') = (-y, x)` and `(x', y') = (y, -x)` seems to be right, but why would one use `dx` and `dy` here. Moreover, based on slopes, `m1 * m2 = -1` for right angle lines, hence `dy' = dx' * (-dx/dy)` and `dx' = dy' * (-dy/dx)`, how come in your equation `normal.x = x' = -dy`? – legends2k Jan 25 '13 at 08:53
  • 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 '13 at 08: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: http://en.wikipedia.org/wiki/Rotation_matrix#Basic_rotations – geon Feb 24 '13 at 17:24
  • @geon: Aah! Got it, I was confusing delta with slope while in affine geometry the difference between two points is a vector, the tanget here :) – legends2k Feb 25 '13 at 10:52
  • 1
    @Oren Trutner does the result have a length of one? – Martin Meeser Apr 24 '14 at 11:42
  • @Martin Meeser: This technique would produce a vector with a magnitude equal to the distance between the two points, so the answer to your question is no. – Syndog Dec 15 '14 at 04:16
  • This answer is incorrect, if dx=1 and dy=1,(a 45deg angle) then the norm would be (0.707,-0.707) not (1,-1). The length of a vector normal=1; – Andy k Jan 11 '17 at 22:43
  • 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 '17 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.

duffymo
  • 305,152
  • 44
  • 369
  • 561
  • 2
    Thanks a ton, was breaking my head on how it was getting derived. – legends2k Jan 25 '13 at 08: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 '13 at 08:26
  • @duffymo does the result have a length of one? – Martin Meeser Apr 24 '14 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 '14 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)

Tu Bui
  • 1,660
  • 5
  • 26
  • 39
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

ufukgun
  • 6,889
  • 8
  • 33
  • 55