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();
}
}
}
在这段代码中,我试图模拟简单的谐波运动。但是,我只能绘制静态图形,但是我需要它连续移动。
我相信我需要使用循环来连续地重画点,但是我不确定该怎么做。
如何使当前的正弦图连续移动?
编辑:投票关闭为非编程?什么?
最佳答案
我看了看您正在使用的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动画帧进行每次迭代,每个帧都必须与前一个不同。您可以通过在循环外使用变量将每个帧偏移一小步来实现。叫这个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();
}
}
我没有动画录制器,所以这是一张照片