Guest User

Fiveagon MCCC#1_1

a guest
Nov 15th, 2015
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.95 KB | None | 0 0
  1. import java.util.*;
  2.  
  3. color[] palette = { #2537b5, #ff3804, #ffffff };
  4. color backgroundColor = #000000;
  5.  
  6. Grid grid;
  7. int margin = 50;
  8. int dotSize = 8;
  9. int strokeSize = 4;
  10. boolean isRestarting = true;
  11.  
  12. int gridsPerGif = 1;
  13. boolean savingFrames = false;
  14. boolean savingImages = false;
  15. int frameCounter = 0;
  16. int gridCounter = 0;
  17.  
  18. List<PImage> frameList = new ArrayList<PImage>();
  19.  
  20. void createGrid() {
  21.   background(backgroundColor);
  22.   addFrame();
  23.   int m = margin * 2;
  24.   int numRows = 41;
  25.   grid = new Grid(numRows, width-m, height-m);
  26.   addFrame();
  27. }
  28.  
  29. void setup() {
  30.   size(540, 540);
  31.   noFill();
  32.   strokeWeight(strokeSize);
  33. }
  34.  
  35. void draw() {
  36.   translate(margin, margin);
  37.  
  38.   if (isRestarting) {  
  39.     createGrid();
  40.     isRestarting = false;
  41.   }
  42.  
  43.   grid.step();
  44.   addFrame();
  45.  
  46.   if (grid.isDone()) {
  47.     saveFrames();
  48.     gridCounter++;
  49.     if (gridCounter == gridsPerGif) {
  50.       exit();
  51.     } else {
  52.       isRestarting = true;
  53.     }
  54.   }
  55. }  
  56.  
  57. void addFrame() {
  58.   if (savingFrames) {
  59.     frameList.add(get());
  60.   }
  61. }
  62.  
  63. void saveFrames() {
  64.   if (savingFrames) {
  65.     for (PImage frame : frameList) {
  66.       frame.save(frameName());
  67.     }
  68.     Collections.reverse(frameList);
  69.     for (PImage frame : frameList) {
  70.       frame.save(frameName());
  71.     }
  72.     frameList.clear();
  73.   }
  74.  
  75.   if (savingImages) {
  76.     save(fileName());
  77.   }
  78. }
  79.  
  80. String frameName() {
  81.   String frameNumber = nf(frameCounter, 4);
  82.   frameCounter++;
  83.   return "frames3\\dots-" + frameNumber + ".tif";
  84. }  
  85.  
  86. String fileName() {
  87.   return "print\\" + UUID.randomUUID() + ".png";
  88. }
  89.  
  90. class Grid {
  91.   GridPoint[][] points;
  92.   int dimension;
  93.   List<Traveler> travelers = new LinkedList<Traveler>();
  94.   Random random = new Random();
  95.  
  96.   Grid(int dimension, float w, float h) {
  97.     this.dimension = dimension;
  98.     points = new GridPoint[dimension][dimension];
  99.     for (int i = 0; i < dimension; i++) {
  100.       for (int j = 0; j < dimension; j++) {
  101.         float x = w / (dimension-1) * j;
  102.         float y = h / (dimension-1) * i;
  103.         points[i][j] = new GridPoint(j, i, new PVector(x, y));
  104.       }
  105.     }
  106.  
  107.     travelers.add(new Traveler(points[0][0]));
  108.     travelers.add(new Traveler(points[0][dimension-1]));
  109.     travelers.add(new Traveler(points[dimension-1][0]));
  110.     travelers.add(new Traveler(points[dimension-1][dimension-1]));
  111.  
  112.     for (Traveler t : travelers) {
  113.       t.render();
  114.     }
  115.   }
  116.  
  117.   void step() {
  118.     for (ListIterator<Traveler> i = travelers.listIterator(); i.hasNext(); ) {
  119.       Traveler t = i.next();
  120.       List<GridPoint> neighbors = getNeighbors(t);
  121.  
  122.       if (neighbors.isEmpty()) {
  123.         i.remove();  // Dead end
  124.       } else {
  125.         int randomIndex = random.nextInt(neighbors.size());
  126.         GridPoint neighbor = neighbors.get(randomIndex);
  127.  
  128.         // Option 1 (Good default)
  129.         i.set(t.clone());
  130.         t.setPoint(neighbor);
  131.         i.add(t);
  132.         t.render();
  133.       }
  134.     }
  135.   }
  136.  
  137.   List<GridPoint> getNeighbors(Traveler t) {
  138.     List<GridPoint> neighbors = new ArrayList<GridPoint>();
  139.     int x = t.x();
  140.     int y = t.y();
  141.  
  142.     if (x - 1 >= 0) {
  143.       GridPoint left = points[y][x-1];
  144.       if (left.notVisited()) {
  145.         neighbors.add(left);
  146.       }
  147.     }
  148.  
  149.     if (x + 1 < dimension) {
  150.       GridPoint right = points[y][x+1];
  151.       if (right.notVisited()) {
  152.         neighbors.add(right);
  153.       }
  154.     }
  155.  
  156.     if (y - 1 >= 0) {
  157.       GridPoint up = points[y-1][x];
  158.       if (up.notVisited()) {
  159.         neighbors.add(up);
  160.       }
  161.     }
  162.  
  163.     if (y + 1 < dimension) {
  164.       GridPoint down = points[y+1][x];
  165.       if (down.notVisited()) {
  166.         neighbors.add(down);
  167.       }
  168.     }
  169.  
  170.     return neighbors;
  171.   }
  172.  
  173.   boolean isDone() {
  174.     return travelers.isEmpty();
  175.   }
  176. }
  177.  
  178. Random colorPicker = new Random();
  179.  
  180. color randomColor(color[] c) {
  181.   int i = colorPicker.nextInt(c.length);
  182.   return c[i];
  183. }
  184.  
  185. class Traveler {
  186.   GridPoint point;
  187.   color strokeColor;
  188.  
  189.   Traveler(GridPoint point) {
  190.     setPoint(point);
  191.     this.strokeColor = randomColor(palette);
  192.   }  
  193.  
  194.   void setPoint(GridPoint point) {
  195.     this.point = point;
  196.     this.point.visit();
  197.   }
  198.  
  199.   Traveler clone() {
  200.     return new Traveler(point);
  201.   }
  202.  
  203.   void render() {
  204.     stroke(strokeColor);
  205.     ellipse(screenX(), screenY(), dotSize, dotSize);
  206.   }
  207.  
  208.   int x() {
  209.     return point.x();
  210.   }
  211.  
  212.   int y() {
  213.     return point.y();
  214.   }
  215.  
  216.   float screenX() {
  217.     return point.screenX();
  218.   }
  219.  
  220.   float screenY() {
  221.     return point.screenY();
  222.   }
  223. }
  224.  
  225. class GridPoint {
  226.   final int x;
  227.   final int y;
  228.   final PVector point;
  229.   boolean visited = false;
  230.  
  231.   GridPoint(int x, int y, PVector point) {
  232.     this.x = x;
  233.     this.y = y;
  234.     this.point = point;
  235.   }
  236.  
  237.   boolean notVisited() {
  238.     return !visited;
  239.   }
  240.  
  241.   void visit() {
  242.     visited = true;
  243.   }
  244.  
  245.   int x() {
  246.     return x;
  247.   }
  248.  
  249.   int y() {
  250.     return y;
  251.   }
  252.  
  253.   float screenX() {
  254.     return point.x;
  255.   }
  256.  
  257.   float screenY() {
  258.     return point.y;
  259.   }
  260. }
Advertisement
Add Comment
Please, Sign In to add comment