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

Piku picture Piku · Aug 7, 2009 · Viewed 206.3k times · Source

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

Answer

Oren Trutner picture Oren Trutner · Aug 7, 2009

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.