# Understanding math used to determine if vector is clockwise / counterclockwise from your vector

I'm reading Programming Game AI by Example by Mat Buckland. In the Math & Physics primer chapter there's a listing of the declaration of a class used to represent 2D vectors.

This class contains a method called Sign. It's implementation is as follows

//------------------------ Sign ------------------------------------------
//
//  returns positive if v2 is clockwise of this vector,
//  minus if anticlockwise (Y axis pointing down, X axis to right)
//------------------------------------------------------------------------
enum {clockwise = 1, anticlockwise = -1};

inline int Vector2D::Sign(const Vector2D& v2)const
{
if (y*v2.x > x*v2.y)
{
return anticlockwise;
}
else
{
return clockwise;
}
}


Can someone explain the vector rules that make this hold true?

What do the values of y*v2.x and x*v2.y that are being compared actually represent?

I'd like to have a solid understanding of why this works rather than just accepting that it does without figuring it out. I feel like it's something really obvious that I'm just not catching on to.

• It looks like it's looking at the sign of the cross product, which would be x * v2.y - y * v2.x. When the cross product is negative, y * v2.x > x * v2.y. – amitp Dec 12 '12 at 2:10
• I think I've found a page that describes what's being done: oocities.org/pcgpe/math2d.html The section entitled 'Clockwise/Anticlockwise'. At the bottom of that section they have the following: if ((E1x * E2y - E1y * E2x) >= 0) clockwise = TRUE; else clockwise = FALSE; If the E1Y * 2x is brought over to the other side of the equation you have a very similar comparison. And once you account for the fact that they are using a coordinate system in which +Y goes up, rather than down as stated in the code snippet, I think it's pretty much the same thing. – MTLPhil Dec 12 '12 at 2:22

• I could be wrong but I think (-y,x) yields a counter clockwise rotation and (y,-x) yields a clockwise rotation by 90 degrees. (-y,x) = (1,1) == (-1,1) (y,-x) = (1,1) == (1,-1) – Alex Brown Dec 27 '12 at 18:52