public static void main(String[] args) {

    Random random = new Random();
    Point start = new Point(random.nextInt(3), random.nextInt(3));
    System.out.print("How many steps: ");
    int n = new In().readInt();
    RandomWalker walker = new RandomWalker(start.x, start.y);

    StdDraw.setScale(-100, 100);
    StdDraw.show();

    System.out.println("Start: x=" + walker.getInitX() + " y=" + walker.getInitY());
    int oldX, oldY;
    for (int i = 0; i < n; i++) {
      oldX = walker.getX();
      oldY = walker.getY();

      walker.step();

      StdDraw.line(oldX, oldY, walker.getX(), walker.getY());
    }

    System.out.println("End: x=" + walker.getX() + " y=" + walker.getY());
  }
Ejemplo n.º 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));
   }
 }
  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]);
    }
  }
 public static void main(String[] args) throws Exception {
   skel = loadRawInteger("data/skeleton.dat");
   vec = loadRawDouble("data/vectorsCorrected.dat");
   pos = new int[2000];
   int seek = 0;
   int frame = 0;
   while (frame < pos.length) {
     pos[frame++] = seek;
     seek += (skel[seek] * 2) + 1;
   }
   StdDraw.setCanvasSize(512, 512);
   StdDraw.setXscale(-128, 128);
   StdDraw.setYscale(-128, 128);
   StdDraw.show(100);
   StdDraw.setPenRadius(0.005);
   int f = 0;
   while (true) {
     if (StdDraw.isKeyPressed(KeyEvent.VK_Q)) {
       break;
     }
     if (StdDraw.isKeyPressed(KeyEvent.VK_RIGHT)) {
       if (StdDraw.isKeyPressed(KeyEvent.VK_SHIFT)) {
         f += 10;
         draw(f);
       } else draw(f++);
     }
     if (StdDraw.isKeyPressed(KeyEvent.VK_LEFT)) {
       if (StdDraw.isKeyPressed(KeyEvent.VK_SHIFT)) {
         f -= 10;
         draw(f);
       } else draw(f--);
     }
     StdDraw.show(100);
   }
 }