1

Say I have a rectangle (R) and inside said rectangle, I have two points, A and B. I need to rotate the rectangle around the center point (C) to the point A and B have equal y coordinates. Since the solution to this could produce two separate answers (where Ax is < Bx, and where Ax is > Bx), I would like to constrain this to only allow the solution where Ax < Bx.

My solution is to solve for theta when rotating around M (the midpoint between A and B) and then to rotate around C by theta (below).

Will this work in all cases/is this optimal? Thanks in advance!

CGPoint m = CGPointMake(a.x / 2 + b.x / 2, a.y / 2 + b.y / 2);
float dx = m.x - a.x;
float dy = m.y - a.y;
float radians = -atan2f(dy, dx)

1 Answers1

1

You can perform the rotation around C, but determine the angle of rotation by examining the relationship between points A and B. The angle of rotation will be -atan2(dy, dx), where dy = B.y-A.y and dx = B.x-A.x.

The range of atan2 is -M_PI to M_PI, so the expression will always provide the smallest rotation to make the line AB parallel to the x axis. To produce a result where A.x < B.x, examine the sign of dx. A negative dx means that A.x > B.x. In that case, your rotation should be adjusted by pi. To sum up:

CGPoint A = // some point
CGPoint B = // some point

CGFloat dx = B.x - A.x;
CGFloat dy = B.y - A.y;
CGFloat rotation = (dx < 0)? M_PI+atan2(dy,dx) : -atan2(dy,dx);

Apply rotation to any point you wish in the rectangle's coordinate system. The rectangle will be rotated about that point to make the line AB parallel to the x-axis leaving A.x < B.x.

danh
  • 62,181
  • 10
  • 95
  • 136
  • Can you explain why the equation is not: CGFloat rotation = (dx < 0)? M_PI+atan2(powf(dy,2),powf(dx,2)) : -atan2(powf(dy,2),powf(dx,2)); – eurekabeacon Jul 27 '15 at 04:52
  • We need a simple slope to compute arctan, that's the change in x divided by the change in y. – danh Jul 27 '15 at 04:57
  • I believe the method above also works, using the midpoint, as it yields the same result. – eurekabeacon Jul 27 '15 at 05:46
  • the midpoint would be dx/2 and dy/2, not squared. squaring is a non-linear change in those differences. – danh Jul 27 '15 at 05:48
  • Sorry. Tough to track this because of the edits. Any two points on the line is sufficient to compute its slope. The midpoint is a point on the line of course, but so are the input points. There's no need to compute other points to get the slope. Your recent edit just needs to solve the x ordering problem and it will be equivalent to mine. – danh Jul 27 '15 at 06:03