private static void draw(int frame) {
    // Draw Worm
    int offset = pos[frame] + 1;
    int len = skel[offset - 1];
    double[] x = new double[len];
    double[] y = new double[len];
    double _x = 0;
    double _y = 0;
    for (int i = 0; i < len; i++) {
      int s = i * 2;
      x[i] = skel[offset + s];
      y[i] = skel[offset + s + 1];
      _x += x[i];
      _y += y[i];
    }
    _x /= len;
    _y /= len;
    StdDraw.clear();
    StdDraw.setPenColor(StdDraw.BLACK);
    StdDraw.text(0, 120, "" + frame);
    StdDraw.setPenRadius(0.003);
    StdDraw.setXscale(-128, 128);
    StdDraw.setYscale(-128, 128);
    for (int i = 0; i < len; i++) {
      x[i] -= _x;
      y[i] -= _y;
      StdDraw.point(x[i], y[i]);
    }
    StdDraw.setPenColor(StdDraw.GREEN);
    StdDraw.setPenRadius(0.01);
    StdDraw.point(x[0], y[0]);

    StdDraw.setXscale(-128, 128);
    StdDraw.setYscale(128, -128);
    StdDraw.setPenColor(StdDraw.MAGENTA);
    StdDraw.setPenRadius(0.005);
    // Recreate from vectors
    offset = frame * n;
    x = new double[n + 1];
    y = new double[n + 1];
    double dist = 3.0;
    for (int i = 0; i < n; i++) {
      x[i + 1] = x[i] + (dist * Math.sin(vec[offset + i]));
      y[i + 1] = y[i] + (dist * Math.cos(vec[offset + i]));
    }
    _x = 0;
    _y = 0;
    for (int i = 0; i < n + 1; i++) {
      _x += x[i];
      _y += y[i];
    }
    _x /= n + 1;
    _y /= n + 1;
    for (int i = 0; i < n + 1; i++) {
      x[i] -= _x;
      y[i] -= _y;
      StdDraw.point(x[i], y[i]);
    }
  }
예제 #2
0
 public static void main(String[] args) {
   final int N = 100;
   StdDraw.setXscale(0, N);
   StdDraw.setYscale(0, N * N);
   StdDraw.setPenRadius(.01);
   for (int i = 1; i <= N; i++) {
     StdDraw.point(i, i);
     StdDraw.point(i, i * i);
     StdDraw.point(i, i * Math.log(i));
   }
 }