这是一个更与数学相关的问题。在我的 android 应用程序中,我有一个从起点到终点绘制一条线的画布。
我也有一个angle属性来旋转线。
我试图将下面的代码应用于我的概念,但它奇怪地跳来跳去,没有指向它应该指向的方向。(0deg = 水平线)
//Coordinates for P1 and P2
int startx = 0;
int starty = 66;
int endx = 420;
int endy = 66;
//Alpha
float angle = 0.000F;
final float radius = 209.500F;
final float extra_radius = 20.000F; //required later - don't mind it
private void reCal(float[] vals) {
    float xAcc = vals[0]*(-1);
    float yAcc = vals[1]*(-1);
    angle = yAcc / 10.000F * 90.000F;
    final float rRadius = radius + extra_radius;
    startx = (int) (radius - Math.cos(angle) * rRadius); //left
    endx = (int) (radius + Math.cos(angle) * rRadius); //right
    starty = 66 + (int) (Math.sin(angle) * rRadius); //top
    endy = 66 - (int) (Math.sin(angle) * rRadius); //bot
}
他们是否有任何可能导致这种情况的类型转换,还是我的数学错误?
更新:
我看着Math.cos(angle)和Math.sin(angle)在运行时。
它们在 0 和 1 之间跳跃,即使angle只是一点点改变。
现在我想知道:这些方法是否以 RAD 或 DEG 作为参数?(我需要 deg,显然)
如果要旋转从[cx, cy]和长度为 的行dx,请使用该代码片段:
canvas.save();
canvas.rotate(angle, cx, cy);
canvas.drawLine(cx, cy, cx + dx, cy, paint);
canvas.restore();
| 归档时间: | 
 | 
| 查看次数: | 2210 次 | 
| 最近记录: |