我们如何写出模拟时钟的角度表达式?

Ale*_*lex 4 java math trigonometry

这是我参考的书中作为解决方案给出的表达,但似乎超出了我的理解。请任何人帮助理解我们究竟是如何写这个角度的?为什么要减去pi/2?我真的不知道

xs=(int)(Math.cos(s*3.14f/30-3.14f/2)*45+xcentre);
ys=(int)(Math.sin(s*3.14f/30-3.14f/2)*45+ycentre);
xm=(int)(Math.cos(m*3.14f/30-3.14f/2)*40+xcentre);
ym=(int)(Math.sin(m*3.14f/30-3.14f/2)*40+ycentre);
xh=(int)(Math.cos((h*30+m/2)*3.14f/180-3.14f/2)*30+xcentre);
yh=(int)(Math.sin((h*30+m/2)*3.14f/180-3.14f/2)*30+ycentre);
Run Code Online (Sandbox Code Playgroud)

Ste*_*eve 5

似乎它给出了以原点为中心的时钟三个指针末端的笛卡尔坐标,向上是负 y,右边是正 x。我可以通过尝试一些价值观来说明这一点。

我像这样包装了你的基本代码:

static class ClockCoords {
    int xs, ys;
    int xm, ym;
    int xh, yh;

    public String toString() {
        return String.format("h: %3d,%3d m: %3d,%3d s: %3d,%3d", xh, yh, xm, ym, xs, ys);
    }
}

public static ClockCoords coords(int h, int m, int s) {
    ClockCoords r = new ClockCoords();
    int xcentre = 0;
    int ycentre = 0;
    r.xs = (int) (Math.cos(s * 3.14f / 30 - 3.14f / 2) * 45 + xcentre);
    r.ys = (int) (Math.sin(s * 3.14f / 30 - 3.14f / 2) * 45 + ycentre);
    r.xm = (int) (Math.cos(m * 3.14f / 30 - 3.14f / 2) * 40 + xcentre);
    r.ym = (int) (Math.sin(m * 3.14f / 30 - 3.14f / 2) * 40 + ycentre);
    r.xh = (int) (Math.cos((h * 30 + m / 2) * 3.14f / 180 - 3.14f / 2) * 30 + xcentre);
    r.yh = (int) (Math.sin((h * 30 + m / 2) * 3.14f / 180 - 3.14f / 2) * 30 + ycentre);
    return r;
}


public static void main(String[] args) throws IOException {
    System.out.println(coords(0, 0, 0));
    System.out.println(coords(0, 0, 45));
    System.out.println(coords(6, 0, 0));
    System.out.println(coords(6, 30, 0));
}
Run Code Online (Sandbox Code Playgroud)

这给了我这个:

h:   0,-29 m:   0,-39 s:   0,-44
h:   0,-29 m:   0,-39 s: -44,  0
h:   0, 29 m:   0,-39 s:   0,-44
h:  -7, 28 m:   0, 39 s:   0,-44
Run Code Online (Sandbox Code Playgroud)

所以,如果我没有搞砸的话,时针长 29 个单位,分针长 39 个单位,秒针长 44 个单位。在一天开始时,第一个条目,它们都指向正上方。一天 45 秒后,第二次进入,看到秒针指向左侧,但其他两只手仍然指向正上方,给定整数粒度和不太长的指针。第三个条目,6:00,只是翻转时针指向下方。第四个条目很有趣,6:30,因为它将分针指向下方,但也将时针向左和向上移动了一点……我们的第一只手的位置与中心不成 90 度角。有趣的东西。至于所有这些的原因......我认为这都是基本的三角和笛卡尔数学。

等一下...有一件奇怪的事情我不明白...我在公式中看到每只手的长度不同。为什么我看到的手长与公式中的相差 1?我的猜测是它与所使用的 PI 的非常粗略的近似值有关。我认为那些手没有指向正上方。我能想到的就这些。也许明天我会尝试为 PI 插入一个更好的值。