Question

here's a snipped of my code - it is supposed to rotate point around origin and then translate it back

    angle = angle * M_PI / 180;
point1.x = (point1.x) * cos(angle) - (point1.y) * sin(angle);
point1.y = (point1.x) * sin(angle) + (point1.y) * cos(angle);

and after that, for translating point where it should "move", condition will be stated based upon in which quadrant is point after "rotation" - for eg, if it is in 1, x += 2*x and y += 2*y. The problem here is rotation : for example, for angle of 130 degrees, for point (100,100), here are coordinates of new point x:CGFloat-3.09086e-06,y:CGFloat100. What am I doing wrong?

Was it helpful?

Solution

When you calculate point1.y you use already translated point1.x. Fix your code like code below:

angle = angle * M_PI / 180;
CGPoint result = CGPointZero;
result.x = (point1.x) * cos(angle) - (point1.y) * sin(angle);
result.y = (point1.x) * sin(angle) + (point1.y) * cos(angle);

And use result point in your future calculations.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top