如何旋转坐标系? [英] How to rotate coordinate system?
问题描述
如果我绕原点旋转,我正在尝试查找坐标的新值。
I am trying to find the new value of a coordinate if I rotate around the origin.
例如,说我有一点(1,1)。
如果我将坐标轴绕原点旋转45度,则变换后的坐标将为(0,1.414)
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)
有没有一种方法可以在cocos2d,还是在objective-c中?
甚至解释数学的答案也将有所帮助。
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.
推荐答案
请参见本页:
< a href = http://www.siggraph.org/education/materials/HyperGraph/modeling/mod_tran/2drota.htm rel = noreferrer> http://www.siggraph.org/education/materials/HyperGraph/模拟/mod_tran/2drota.htm
这是公式:
x'= x cos f-y sin f
x' = x cos f - y sin f
y'= y cos f + x sin f
y' = y cos f + x sin f
记住罪和cos采用弧度,因此您必须这样做:
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... ;)
这篇关于如何旋转坐标系?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!