我正在编写一个类似精灵的模拟,我需要在圆形路径中移动带有图像的标签。我有4个起点;西、北、东和南。标签应以顺时针方向移动。
这是我写的,效果很好,除了南北位置逆时针旋转。原因是,我使用了单独的三角公式来处理方向 2 和 4。
int xstart = 450
int ystart = 325;
int h = 0;
double theta = -0.1047;
for (int p = 0;; p++)
{
int xocir = xstart;
int yocir = ystart;
int pocir = 0; // perpendicular and base of outer circle
int bocir = 0;
if (direction == 1)//west - clockwise
{
pocir = (int) (Math.cos(theta) * (h + 300));
bocir = (int) (Math.sin(theta) * (h + 300));
}
else if (direction == 2)//north - counter-clockwise
{
pocir = (int) (Math.sin(theta) * (h + 300));//reversed sin,cos
bocir = (int) (Math.cos(theta) * (h + 300));
}
else if (direction == 3)//east - clockwise
{
pocir = (int) (Math.cos(theta) * (h - 300));
bocir = (int) (Math.sin(theta) * (h - 300));
}
else if (direction == 4)//south - counter-clockwise
{
pocir = (int) (Math.sin(theta) * (h - 300));
bocir = (int) (Math.cos(theta) * (h - 300));
}
xocir = xocir - pocir;
yocir = yocir - bocir;
theta = theta - 0.005;
setBounds(xocir, yocir, 65, 65);
}
有没有更有效的方法来解决这个问题?也许是一个更简单的触发方法。我能做些什么来确保所有的运动都是顺时针的?