tags:

views:

526

answers:

3

HI All,

How can I calculate direction vector of a line segment, defined by start point (x1, y1) and end point (x2, y2)?

Cheers.

+5  A: 
(x2 - x1, y2 - y1)

If you want the unit direction vector, divide each component by sqrt((x2 - x1)² + (y2 - y1)²).

Mark Byers
Might want to add something about normalizing.
GMan
Do I have to do something to deal with -ve and +ve coordinates???
Zinx
No, it works for negative coordinates too.
Mark Byers
cool, thanx for help. Cheer
Zinx
+2  A: 

Hi

The direction vector can be represented as (x2 - x1)i + (y2 - y1)j where i and j are unit vectors along x and y axis respectively.

cheers

Andriyev
+2  A: 

If you want the vector from the end of vector (x1,y1) to the end of vector (x2,y2), the answer is

(x2-x1, y2-y1) + (x1,y1)

If you want the (unit-length) direction vector, then the answer is

((x2-x1)/L, (y2-y1)/L)

where L=√((x2-x1)² + (y2-y1)²) (thats $L=\sqrt{(x2-x1)^2 + (y2-y1)^2}$ in LaTeX).

Barry Wark
Hey, does that means ((x2-x1)/L, (y2-y1)/L)????
Zinx
@Zinx, Yes a * (x,y) is multiplying a vector by a scalar, which corresponds to dividing each component of the vector by a.
Barry Wark
thanks for the help, cheers.
Zinx