在看<Flash actionscript动画教程>(中文版)的第十章的第二节,它提到“高级坐标旋转”,书中只给出了一个基本公式:

x1 = cos(angle) * x – sin(angle) * y;

y1 = cos(angle) * y + sin(angle) * x;

书上并没有写如何来分析、证明这个公式,所以就自己琢磨了一下。

 

有如下的所示的图,如果有一个圆,它的圆心距离以某一点为参考点(原点)不变,而绕着它旋转某一角度后,希望得到新的坐标。

==> 已经圆心距离“原点”它的坐标为(x, y),现在希望求得在它绕“原点”旋转一定角度(angle)后,新的坐标(x1, y1)

关于坐标旋转

假设初始坐标(x, y)与X轴形成的一个角度为A,记圆心距离“原点”的长度为 d = Math.sqrt(x * x + y * y);

sin(A) = y / d;

cos(A) = x / d;

x1 = d * cos(A + angle);

=>

x1 = d * (cos(A)*cos(angle) – sina(A)*sin(angle));

x1 = d * cos(A) * cos(angle) – d * sin(A) * sin(angle);

x1 = d * (x / d) * cos(angle) – d * (y / d) * sin(angle);

x1 = x * cos(angle) – y * sina(angle);

 

y1 = d * sin(A + angle);

=>

y1 = d * (sin(A)*cos(angle)  + cos(A)*sin(angle));

y1 = d * sin(A) * cos(angle) + d * cos(A) * sin(angle);

y1 = d * (y / d) * cos(angle) + d * (x / d) + sin(angle);

y1 = y * cos(angle) + x * sin(angle);

 

主要用到了以下两个公式:

sin(A+B) = sin(A) * cos(B) + cos(A) * sin(B);

cos(A+B) = cos(A) * cos(B) – sin(A) * sin(B);

 

最后用这个公式写一个简单的例子

void {
graphics.beginFill(color);
graphics.drawCircle(0, 0, radius);
graphics.endFill();
}

}
}

用于测试的类

void
{
var x1:Number = ball.x - stage.stageWidth / 2;
var y1:Number = ball.y - stage.stageHeight / 2;
var x2:Number = cos * x1 - sin * y1;
var y2:Number = cos * y1 + sin * x1;

ball.x = stage.stageWidth / 2 + x2;
ball.y = stage.stageHeight / 2 + y2;
}
}

}

相关文章:

  • 2021-08-28
  • 2022-12-23
  • 2021-12-03
  • 2021-12-03
  • 2021-12-03
  • 2021-06-21
  • 2021-10-18
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-12-03
  • 2021-12-03
  • 2021-12-03
相关资源
相似解决方案