【问题标题】:Displaying the cosine function with specified periods (turtle)显示具有指定周期的余弦函数(海龟)
【发布时间】:2022-01-19 20:31:02
【问题描述】:

我正在使用 turtle api 来完成 [这个任务] (http://sites.asmsa.org/java-turtle/exer/5-function-graphing)。我坚持绘制余弦函数以显示用户指定的周期数。 for 循环是我用来为 x 值的间隔生成图形的 y 坐标的循环。不是 y = (x/2pi*period) 吗?但是,这显示了太多的句点。 (amp 也代表振幅,我将我的乌龟 don 命名为 donatello)

Scanner sc = new Scanner(System.in);
System.out.println("Enter the following values.");
System.out.print("Amplitude: ");
int amp = sc.nextInt();
System.out.print("Number of Periods:");
int per = sc.nextInt();
don.setPosition(-320, amp * Math.cos( -320 / (4 * Math.PI * per)));
don.down();
for(int x = -320; x < 320; x+=5) {
    double y = amp * Math.cos(x/(2 * Math.PI * per));
    don.setPosition(x, y);
}

【问题讨论】:

  • 您想将范围 (-320,320) 映射到范围 (0,2pi*per)。所以,你需要(x+320) * 2 * Math.PI * per / 640。

标签: java trigonometry turtle-graphics


【解决方案1】:

作为一般规则,任何时候您想将 M 的范围映射到 N 的范围,公式为x * N / M。因此,如果 x 为 0,则为 0;如果 x 在 M 中,则得到 N。

您希望将范围 (-320,320) 映射到范围 (0,2pi*per)。所以,你需要(x+320) * 2 * Math.PI * per / 640。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多