Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I calculate the normal vector of a line segment?

People also ask

How do you find the normal vector between two points?

We use the general form for a straight line, ax + by + c = 0. Find two points on the line, first by choosing x = 0 and finding y and then by choosing y = 0 and finding x. The points (0, –c/b) and (–c/a, 0) lie on the line. The direction vector is therefore and the normal vector is .

How do you find the normal vector perpendicular to a line?

A normal vector to a line in the plane is a non-zero vector perpendicular to the line, or equivalently perpendicular to v. If N is a normal vector, the line can be described as all points which satisfy N • 〈x, y〉 = d and a point P = 〈x0,y0〉 is on the line if and only if N • P = d.


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.


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.


This question has been posted long time ago, but I found an alternative way to answer it. So I decided to share it here.
Firstly, one must 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)


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