【发布时间】: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