ChatGPT解决这个技术问题 Extra ChatGPT

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

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers. This question does not appear to be about programming within the scope defined in the help center. Closed 6 months ago. The community reviewed whether to reopen this question 6 months ago and left it closed: Original close reason(s) were not resolved Improve this question

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

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.
This question is about math, not programming.
I'm voting to close this question as off-topic because it is about mathematics, not programming.

f
funie200

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.


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...
Could you please brief more on how the delta plays a role here? I'm sure I'm missing something here.
@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
@Oren Trutner does the result have a length of one?
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.
d
duffymo

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.


Thanks a ton, was breaking my head on how it was getting derived.
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.
@duffymo does the result have a length of one?
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.
T
Tu Bui

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)


u
ufukgun
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