Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to rotate coordinate system?

I am trying to find the new value of a coordinate if I rotate around the origin.

For example, say I have the point (1,1). If I rotate the coordinate axis 45 degrees around the origin, the transformed coordinate would be (0,1.414)

Is there a way to do this efficiently in cocos2d, or in objective-c ? Even answers explaining the math to do this would be helpful.

like image 787
xcoder Avatar asked Feb 08 '12 08:02

xcoder


1 Answers

See this page: http://www.siggraph.org/education/materials/HyperGraph/modeling/mod_tran/2drota.htm

This is the formula:

x' = x cos f - y sin f

y' = y cos f + x sin f

Remember that sin and cos takes radians, so you have to do like this:

double x,y;
double newX,newY;
double angle;

//Test values:
x=1;
y=1;
angle = 45;

double rad = angle*M_PI/180;

newX = x * cos(rad) - y * sin(rad);
newY = y * cos(rad) + x * sin(rad);

I didn't test this, so there might be typos... ;)

like image 193
Richard G. Nielsen Avatar answered Oct 28 '22 23:10

Richard G. Nielsen