如何旋转坐标系?

xco*_*der 5 transform objective-c rotation cocos2d-iphone coordinate

如果我围绕原点旋转,我试图找到坐标的新值.

例如,假设我有点(1,1).如果我将坐标轴围绕原点旋转45度,则变换后的坐标将为(0,1.414)

有没有办法在cocos2d或objective-c中有效地做到这一点?即使是解释数学的答案也会有所帮助.

Ric*_*sen 10

见本页:http: //www.siggraph.org/education/materials/HyperGraph/modeling/mod_tran/2drota.htm

这是公式:

x'= x cos f - y sin f

y'= y cos f + x sin f

请记住,sin和cos需要弧度,所以你必须这样做:

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);
Run Code Online (Sandbox Code Playgroud)

我没有测试这个,所以可能有拼写错误...;)