public class SimpleHarmonic {
public static void main(String[] args) {
StdDraw.setXscale(0,900);
StdDraw.setYscale(0,700);
while (true) {
StdDraw.setPenColor(StdDraw.BLACK);
StdDraw.line(0,350,900,350); // x-axis
StdDraw.line(450,0,450,900); // y-axis
StdDraw.setPenColor(StdDraw.RED);
for (double x = -450; x <= 450; x += 0.5) {
double y = 50 * Math.sin(x * (Math.PI / 180));
int Y = (int) y;
int X = (int) x;
StdDraw.line(450 + X, 350 - Y, 450 + X, 350 - Y);
}
StdDraw.clear();
}
}
}
在这段代码中,我试图模拟简谐运动。但是,我只能绘制静态图形,但我需要它不断移动。
我相信我需要使用一个循环来逐渐重绘点数,但我不知道该怎么做。
如何使我当前的正弦曲线图一致地移动?
编辑:投票结束非编程? 是什么?的
答案 0 :(得分:2)
我看了一下你正在使用的StdDraw
课程,它看起来像你想要的是
StdDRaw.show(int)
方法,此方法注释说明:
/**
* Display on screen, pause for t milliseconds, and turn on
* <em>animation mode</em>: subsequent calls to
* drawing methods such as {@code line()}, {@code circle()}, and {@code square()}
* will not be displayed on screen until the next call to {@code show()}.
* This is useful for producing animations (clear the screen, draw a bunch of shapes,
* display on screen for a fixed amount of time, and repeat). It also speeds up
* drawing a huge number of shapes (call {@code show(0)} to defer drawing
* on screen, draw the shapes, and call {@code show(0)} to display them all
* on screen at once).
* @param t number of milliseconds
*/
在此库中,只要您调用绘图方法(例如line
或circle
),它就会有条件地重新绘制框架。通过将int
参数传递给draw
方法,它会将所有绘制方法转换为“动画模式”,并推迟重新绘制框架,直到您调用draw()
(无参数)。
要使其成为动画,您必须对while
循环1动画帧进行每次迭代,每个帧都需要与前一帧不同。您可以通过在循环外使用变量来将每个帧偏移一个小的ammount来完成此操作。我称之为offset
使用此信息,您可以将循环更改为:
double offset = 0;
while (true) {
offset+=1; // move the frame slightly
StdDraw.show(10); // defer repainting for 10 milisecoinds
StdDraw.clear(); // clear before painting
StdDraw.setPenColor(StdDraw.BLACK);
StdDraw.line(0,350,900,350); // x-axis
StdDraw.line(450,0,450,900); // y-axis
StdDraw.setPenColor(StdDraw.RED);
for (double x = -450; x <= 450; x += 0.5) {
// apply the offset inside of calculation of Y only such that it
// slowly "moves" the sin wave
double y = 50 * Math.sin((offset+x) * (Math.PI / 180));
int Y = (int) y;
int X = (int) x;
StdDraw.line(450 + X, 350 - Y, 450 + X, 350 - Y);
}
StdDraw.show(); // end animation frame. force a repaint
}
<小时/> 代码中的一些改进
1 在您绘制每个“点”的循环中,您将增加.5
。因为X值实际上是1个像素,所以你不能通过.5
而不是1
获得任何东西。 1是你在这个环境中可以看到的最小的字面。我建议至少将其x+=1
for (double x = -450; x <= 450; x += 1)
2 您正在使用.line
方法,但绘制到同一点。通过仅计算每个第3像素Y值并连接点,可以显着加快程序的速度。例如
double prevX = -450;
double prevY = 50 * Math.sin((prevX+offset) * (Math.PI / 180)); // seed the previous Y to start
for (double x = 0; x <= 450; x += 3) {
double y = 50 * Math.sin((x+offset) * (Math.PI / 180));
StdDraw.line(450 + (int)prevX, 350 - (int)prevY, 450 + (int)x, 350 - (int)y);
prevX = x;
prevY = y;
}
3 这不是您的代码,但在StdDraw.init
方法中,您可以设置一些渲染提示以允许更清晰的线条。这应该让它看起来更好
offscreen.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL,
RenderingHints.VALUE_STROKE_PURE);
结合所有这些事情,我写的是
public static void main(String[] args) {
StdDraw.setXscale(0,900);
StdDraw.setYscale(0,700);
double offset = 0;
while (true) {
StdDraw.show(10);
StdDraw.clear();
offset-=1;
StdDraw.setPenColor(StdDraw.BLACK);
StdDraw.line(0,350,900,350); // x-axis
StdDraw.line(450,0,450,900); // y-axis
StdDraw.setPenColor(StdDraw.RED);
double prevX = 0;
double prevY = 50 * Math.sin((prevX+offset) * (Math.PI / 180)); // seed the previous Y to start
StdDraw.filledCircle(450 + prevX, 350 - prevY, 5);
for (double x = 0; x <= 450; x += 3) {
double y = 50 * Math.sin((x+offset) * (Math.PI / 180));
StdDraw.line(450 + (int)prevX, 350 - (int)prevY, 450 + (int)x, 350 - (int)y);
prevX = x;
prevY = y;
}
StdDraw.show();
}
}