How to rotate coordinate system?

北慕城南 提交于 2019-12-07 04:03:57

问题


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.


回答1:


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... ;)



来源:https://stackoverflow.com/questions/9190149/how-to-rotate-coordinate-system

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!