How do I calculate the normal vector of a line segment? [closed]
Asked Answered
B

4

214

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

Banc answered 7/8, 2009 at 8:35 Comment(3)
And if you want to know on the "maths" behind this, you can look up my answer at https://mcmap.net/q/128552/-given-2-points-how-do-i-draw-a-line-at-a-right-angle-to-the-line-formed-by-the-two-points. It's basically the same, but more elaborate.Worshipful
This question is about math, not programming.Vieva
I'm voting to close this question as off-topic because it is about mathematics, not programming.Avar
H
302

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.

Homesteader answered 7/8, 2009 at 8:49 Comment(9)
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...Banc
@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?Congregationalism
Could you please brief more on how the delta plays a role here? I'm sure I'm missing something here.Congregationalism
@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_rotationsGleeful
@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 :)Congregationalism
@Oren Trutner does the result have a length of one?Horsefly
@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.Kisser
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;Conceited
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.Longitude
T
113

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.

Tetartohedral answered 10/8, 2009 at 0:57 Comment(4)
Thanks a ton, was breaking my head on how it was getting derived.Congregationalism
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.Congregationalism
@Tetartohedral does the result have a length of one?Horsefly
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.Tetartohedral
U
17

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)

Ultramundane answered 26/4, 2014 at 23:21 Comment(0)
I
10
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

Inelastic answered 7/8, 2009 at 8:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.