Josema
Josema

Reputation: 4738

How can I find the direction of a Vector2 with only the X and Y coordinates?

I would like to know 2 things about the struct Vector2 in XNA:

  1. Why does this struct only have X and Y instead of X,Y (origin) and X',Y' (destination)?
  2. How can I calculate the direction of a vector with only the X,Y?

Thanks a lot in advance.
Kind Regards.
Josema.

Upvotes: 1

Views: 5751

Answers (5)

starblue
starblue

Reputation: 56822

To calculate the direction (angle) of a vector most languages have an atan2(y,x) function.

Upvotes: 3

mattnewport
mattnewport

Reputation: 14077

Mathematically a vector has orientation (direction) and magnitude (length). It does not have position. When vectors are used in graphics programming to represent positions they are implicitly representing a point as an offset from the origin.

If you want to convert from a vector to an angle you can use simple trigonometry - the x and y components form two sides of a triangle and you can calculate the angle the vector makes with any axis. If you want to find the angle between two arbitrary vectors a and b it's acos(dot(a, b) / (length(a) * length(b)).

Upvotes: 3

Kenan E. K.
Kenan E. K.

Reputation: 14111

The X and Y are not actually the coordinates of a point.

They are X-axis and Y-axis components of the vector. A vector by definition has no origin, it represents only direction and length, not position.

Upvotes: 3

Aaron
Aaron

Reputation: 7541

1) A vector does not need length.

2) The numbers themselves determine the direction of the vector. Think of the cartesian plane. If you have a negative x and a positive y, then you are going top left...positive x, positive y, top right...etc. etc.

Upvotes: 2

Amok
Amok

Reputation: 1269

The origin is usually assumed to be (0,0).

Upvotes: 15

Related Questions