I've read this one, but I need more info: rotating an object from sourceAngle to destAngle, both 0-359, clockwise or counter clockwise?

I have a ball. The user is able to drag the ball in any direction clockwise/anticlockwise around a circle. I need to know whether or not the ball was dragged clockwise or anticlockwise.

I'd like to get both vector/non-vector methods.

  • \$\begingroup\$ If the angle was 90 one frame, and 270 the next, which way is it rotating? i.e. you'd have to assume it moved in the direction of the minimum rotation, then have some default for 180 degree turns. \$\endgroup\$ Jan 9, 2012 at 11:52
  • \$\begingroup\$ please see edit \$\endgroup\$
    – Vishnu
    Jan 9, 2012 at 11:55
  • \$\begingroup\$ i also have a middle point \$\endgroup\$
    – Vishnu
    Jan 9, 2012 at 14:21
  • 1
    \$\begingroup\$ I'm a little confused here, what data do you have? You talk about both angles and points, one must be derived from the other. Don't tell us about derived data, just the data that you have and the result you need. Anyone can compute the middle point between two points, but that doesn't actually solve the problem @GeorgeDuckett states. In any case the answer to that is that this question probably calls for 4 different outputs, right rotation, left rotation, no rotation and exactly 180 degrees rotation. \$\endgroup\$ Jan 9, 2012 at 14:39
  • \$\begingroup\$ @eBusiness: Well, simple take this as I have user dragging a ball around a circle. I need to know in which direction clockwise/anticlockwise he did drag it \$\endgroup\$
    – Vishnu
    Jan 9, 2012 at 15:01

5 Answers 5


Keep track of the position of the ball on the previous frame. Then let's say we have:

|\                // C = Rotation Center
| \               // P1 = Previous Frame Position
|  P2             // P2 = Current Frame Position

You need to check using the 2D analog of a cross product whether P2 lies to the left or to the right of the C-P1 line segment. If it's to the right, then it's rotating clockwise. If it's to the left, then it's rotating counter-clockwise.

You can use this method to check:

bool isLeft(Vector2 c, Vector2 p1, Vector2 p2)
     return ((p1.x - c.x)*(p2.y - c.y) - (p1.y - c.y)*(p2.x - c.x)) > 0;
  • \$\begingroup\$ I find a problem arising sometimes though, now ! can you brief. Some times it shows 1 when I really require -1 \$\endgroup\$
    – Vishnu
    Jan 10, 2012 at 9:01
  • \$\begingroup\$ What if user drags the ball 270 degree clockwise? In this case, your solution will determinates that the ball rotate reverse clockwise 90 degree, Right? \$\endgroup\$ May 30, 2013 at 17:13
  • \$\begingroup\$ This also works if you don't know the center of the rotation, but track 2+ previous frames' positions. Subsequent steps around the arc will produce a cross product with different sign depending on the direction the arc curves. \$\endgroup\$
    – DMGregory
    Oct 12, 2022 at 16:15

Given the points Source, Destination and Centre, where a move has happened from Source to Destination first compute the vectors:

CentreSource = Source - Centre
CentreDestination = Destination - Centre

Compute the dot product of the tværvector of CentreSource and CentreDestination:

RorL = CentreSourceX * CentreDestinationY - CentreSourceY * CentreDestinationX

Now RorL should be positive if the point is moving counterclockwise around the centre, negative in case of clockwise, and 0 if the move was either 0 or 180 degrees.

You can further check if the move was above or below 90 degrees by computing the dot product of CentreSource and CentreDestination:

AorB = CentreSourceX * CentreSourceY + CentreDestinationX * CentreDestinationY

If AorB is positive then the move was below 90 degrees, if it is negative the move was above 90 degrees, and if it is 0 then the move was either exactly 90 degrees or it either started or ended in the point Centre.

  • \$\begingroup\$ What's a tværvector? Can't seem to find any reference to it online, besides your posts. \$\endgroup\$ Jan 9, 2012 at 15:43
  • \$\begingroup\$ There are no other references. First hit on Google gives you the definition (unless of course if this post should overtake it). No one by the way seems to be using the term 2D cross product either, the page you link to call it a determinant, then you can follow a link to a a page about determinants where it is written that "Determinants are defined only for square matrices.". That is not exactly coherent. Using the word tværvector we can at least talk about the maths without having to touch this linguistic mess. \$\endgroup\$ Jan 9, 2012 at 16:14

How I would achieve this is to have a stack of 3 positions and use these to determine rotation of the object.


double[] position = new double[2];

double[][] positions = new double[3][2];
void bool clockwise () {
     for (int i = 0; i < 2; i++) {
          positions[i + 1][0] = positions[i][0];
          positions[i + 1][1] = positions[i][1];
     positions[0][0] = position[0];
     positions[0][1] = position[1];
     double[] averageOfEnds = new double[]{ (positions[0][0] + position[2][0]) / 2, (positions[0][1] + position[2][1]) / 2 };
     if (positions[2][0] < averageOfEnds[0] || positions[2][1] > averageOfEnds[1]) {
          return true;
     return false;

this assumes you axes are like

- ------ +

Starting with the representation:

[x2, y2]' = R(th) * [x1 y1]';
R(th) = [cos(th) -sin(th)
         sin(th)  cos(th)]

Which is basically a rotation operation on a vector. x2 and y2 are the rotated position of x1 and y1. The idea is to get cos(th) and sin(th) in terms of x1,x2,y1 and y2 variables. In order not to fall in to wrong quadrant, atan2 is used with the values of sin(th) and cos(th) which boils down to:

  x1 = cos(th1); y1 = sin(th1);
  x2 = cos(th2); y2 = sin(th2);
  diff = atan2((x1*y2 - x2*y1), (y1*y2 + x1*x2)); # atan2(Yterm,Xterm) (Matlab notation 

Derivation: enter image description here


The easiest solution would be to keep a reference to the object's previous and current or previous, current and extrapolated next positions and do a simple compare.

  • \$\begingroup\$ Can you explain at least a little more. Thanks for the effort, though \$\endgroup\$
    – Vishnu
    Jan 9, 2012 at 11:42

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .