按角度平移点ios

Nen*_*vic 4 translation angle points ios

这是我的代码片段 - 它应该围绕原点旋转点,然后将其平移回来

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

之后,对于应“移动”的平移点,将根据“旋转”后的象限所在的点来说明条件 - 例如,如果它在 1 中,则 x += 2*x 且 y += 2 *y。这里的问题是旋转:例如,对于 130 度的角度,对于点 (100,100),这里是新点 x:CGFloat-3.09086e-06,y:CGFloat100 的坐标。我究竟做错了什么?

Lor*_*rik 5

当您计算时,point1.y您使用已经翻译的point1.x。修复您的代码,如下所示:

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

result并在以后的计算中使用点。