**1**

# Finding the normal to a 2d line

###
#1
Members - Reputation: **110**

Posted 02 January 2006 - 05:08 PM

###
#2
Moderators - Reputation: **9615**

Posted 02 January 2006 - 05:14 PM

If the lines are ordered, and you want the counter-clockwise normal, then it's simple to see what to do.

First, generate the rotation matrix that will rotate a point by 90 degrees counterclockwise in 2D. I'm assuming row vertices on the left:

0 1

[x y] * = [Xn Yn]

-1 0

Then, generate the tangent for the line:

[Xt Yt] = [x2 y2]-[x1 y1] = [x2-x1 y2-y1]

Last, rotate through the matrix and simplify:

0 1

[Xn Yn] = [x2-x1 y2-y1] * = [x1-x2 y2-y1]

-1 0

(Watch the order!)

Normalize and you have the actual normal. The clockwise normal would be the 90 degree rotation the other way.

###
#3
Members - Reputation: **110**

Posted 02 January 2006 - 05:33 PM

[x1-x2, y2-y1]

[4 - 0, 4 - 0]

**[4, 4]**

which is clockwise.

###
#4
Members - Reputation: **1372**

Posted 02 January 2006 - 06:01 PM

Quote:

Original post by hplus0603

0 1

[Xn Yn] = [x2-x1 y2-y1] * = [x1-x2 y2-y1]

-1 0

Clockwise would be

0 1

[Xn Yn] = [x2-x1 y2-y1] * =[y1-y2 x2-x1]

-1 0

0-1

[Xn Yn] = [x2-x1 y2-y1] * = [y2-y1 x1-x2]

10

John BoltonLocomotive Games (THQ)Current Project: Destroy All Humans (Wii). IN STORES NOW!

###
#5
Members - Reputation: **463**

Posted 02 January 2006 - 06:02 PM

where m = dy/dx = (y2-y1)/(x2-x1)

Then the perpendicular line is defined as:

y = m'*x

where m' = -1/m = -(dx/dy) = -dx/dy = dx/-dy

So you have two solutiond for m'

m' = -dx/dy

so m' = x1-x2/y2-y1 (1)

or m' = dx/-dy

so m' = x2-x1/y1-y2 (2)

Which are the solutions HPPlus presented

Luck!

Guimo

###
#6
Members - Reputation: **140**

Posted 02 January 2006 - 07:18 PM

v is the direction of the line

n1 is the first "normal"

n2 is the second

n1<-v.y, v.x>

n2<v.y, -v.x>

normalize them if necessary.

of course i'm assuming you're defineing the line as a segment with two endpoints or in terms of a vector and a point.

###
#7
Members - Reputation: **136**

Posted 22 January 2012 - 12:21 AM

1) Find the direction of the line by subtracting one point from the other

2) Convert the direction into a 3D vector, leave z as 0.

3) do a cross product with (0,0,1)

4) normalize the result

Voila, you have the normal to the line!

###
#8
Crossbones+ - Reputation: **8840**

Posted 22 January 2012 - 01:42 AM

Careful with this approach, as you need to handle the case whenSo you have two solutiond for m'

m' = -dx/dy

so m' = x1-x2/y2-y1 (1)

or m' = dx/-dy

so m' = x2-x1/y1-y2 (2)

Which are the solutions HPPlus presented

`y1-y2`is zero.

Not everyone working with 2D space has a 3D math library readily available. Hplus' approach makes the most sense (with an thorough explanation even!) and avoids 6 multiplications, 3 subtractions, and storage overhead for z component and the extra vector.Much easier way:

1) Find the direction of the line by subtracting one point from the other

2) Convert the direction into a 3D vector, leave z as 0.

3) do a cross product with (0,0,1)

4) normalize the result

Voila, you have the normal to the line!

`gzip: H4sIAAAAAAAEAG1QTUvEMBC991e8nvaiFfYoS7yo sLCo6MnjtJ1ugmkiyWRL/72z3T1YEQIJ8z4zA2Xp yPvt1qBpGrRFIJZkk9FyRyUzHCbKIHgn4hnZOrm1 TD0mG0HCCs+QGDGWziKXI6Wm2n++GYwUVH2mrGEE PnGCVQ8K8+JYfXA6URDEQfMZh5h6g5eoAlWJdeEI bbH2qYZf7XMUfw8f/Q0oMeZYNL9/WHF0uFEshvMr XYujd9SycFb+F18QcSOvlJauZ8ejqevdnV7/d550 e0t6prmunh73Bu+vz4c/XUeOQXfJgvKNkhf95U8/ Dtgmy5IBAAA=`

###
#9
Members - Reputation: **136**

Posted 22 January 2012 - 11:07 AM

Not everyone working with 2D space has a 3D math library readily available. Hplus' approach makes the most sense (with an thorough explanation even!) and avoids 6 multiplications, 3 subtractions, and storage overhead for z component and the extra vector.

It's super cheap and easy to make your own cross product function that takes one 2d vector, assumes the z is 0 and crosses it with (0,0,1).

Here, let me give you the exact formula:

Given a 2d vector (a,b), the normal is (x,y):

x = b

y = -a

So basically, flip a and b, and negate the y. Two assignments and one negation. Can't be cheaper!