Example #1
0
 /**
  * Write the given text string in the current font, centered on (x, y) and rotated by the
  * specified number of degrees
  *
  * @param x the center x-coordinate of the text
  * @param y the center y-coordinate of the text
  * @param s the text
  * @param degrees is the number of degrees to rotate counterclockwise
  */
 public static void text(double x, double y, String s, double degrees) {
   double xs = scaleX(x);
   double ys = scaleY(y);
   offscreen.rotate(Math.toRadians(-degrees), xs, ys);
   text(x, y, s);
   offscreen.rotate(Math.toRadians(+degrees), xs, ys);
 }
  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]);
    }
  }