Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- color[] palette = { #2537b5, #ff3804, #ffffff };
- color backgroundColor = #000000;
- Grid grid;
- int margin = 50;
- int dotSize = 8;
- int strokeSize = 4;
- boolean isRestarting = true;
- int gridsPerGif = 1;
- boolean savingFrames = false;
- boolean savingImages = false;
- int frameCounter = 0;
- int gridCounter = 0;
- List<PImage> frameList = new ArrayList<PImage>();
- void createGrid() {
- background(backgroundColor);
- addFrame();
- int m = margin * 2;
- int numRows = 41;
- grid = new Grid(numRows, width-m, height-m);
- addFrame();
- }
- void setup() {
- size(540, 540);
- noFill();
- strokeWeight(strokeSize);
- }
- void draw() {
- translate(margin, margin);
- if (isRestarting) {
- createGrid();
- isRestarting = false;
- }
- grid.step();
- addFrame();
- if (grid.isDone()) {
- saveFrames();
- gridCounter++;
- if (gridCounter == gridsPerGif) {
- exit();
- } else {
- isRestarting = true;
- }
- }
- }
- void addFrame() {
- if (savingFrames) {
- frameList.add(get());
- }
- }
- void saveFrames() {
- if (savingFrames) {
- for (PImage frame : frameList) {
- frame.save(frameName());
- }
- Collections.reverse(frameList);
- for (PImage frame : frameList) {
- frame.save(frameName());
- }
- frameList.clear();
- }
- if (savingImages) {
- save(fileName());
- }
- }
- String frameName() {
- String frameNumber = nf(frameCounter, 4);
- frameCounter++;
- return "frames3\\dots-" + frameNumber + ".tif";
- }
- String fileName() {
- return "print\\" + UUID.randomUUID() + ".png";
- }
- class Grid {
- GridPoint[][] points;
- int dimension;
- List<Traveler> travelers = new LinkedList<Traveler>();
- Random random = new Random();
- Grid(int dimension, float w, float h) {
- this.dimension = dimension;
- points = new GridPoint[dimension][dimension];
- for (int i = 0; i < dimension; i++) {
- for (int j = 0; j < dimension; j++) {
- float x = w / (dimension-1) * j;
- float y = h / (dimension-1) * i;
- points[i][j] = new GridPoint(j, i, new PVector(x, y));
- }
- }
- travelers.add(new Traveler(points[0][0]));
- travelers.add(new Traveler(points[0][dimension-1]));
- travelers.add(new Traveler(points[dimension-1][0]));
- travelers.add(new Traveler(points[dimension-1][dimension-1]));
- for (Traveler t : travelers) {
- t.render();
- }
- }
- void step() {
- for (ListIterator<Traveler> i = travelers.listIterator(); i.hasNext(); ) {
- Traveler t = i.next();
- List<GridPoint> neighbors = getNeighbors(t);
- if (neighbors.isEmpty()) {
- i.remove(); // Dead end
- } else {
- int randomIndex = random.nextInt(neighbors.size());
- GridPoint neighbor = neighbors.get(randomIndex);
- // Option 1 (Good default)
- i.set(t.clone());
- t.setPoint(neighbor);
- i.add(t);
- t.render();
- }
- }
- }
- List<GridPoint> getNeighbors(Traveler t) {
- List<GridPoint> neighbors = new ArrayList<GridPoint>();
- int x = t.x();
- int y = t.y();
- if (x - 1 >= 0) {
- GridPoint left = points[y][x-1];
- if (left.notVisited()) {
- neighbors.add(left);
- }
- }
- if (x + 1 < dimension) {
- GridPoint right = points[y][x+1];
- if (right.notVisited()) {
- neighbors.add(right);
- }
- }
- if (y - 1 >= 0) {
- GridPoint up = points[y-1][x];
- if (up.notVisited()) {
- neighbors.add(up);
- }
- }
- if (y + 1 < dimension) {
- GridPoint down = points[y+1][x];
- if (down.notVisited()) {
- neighbors.add(down);
- }
- }
- return neighbors;
- }
- boolean isDone() {
- return travelers.isEmpty();
- }
- }
- Random colorPicker = new Random();
- color randomColor(color[] c) {
- int i = colorPicker.nextInt(c.length);
- return c[i];
- }
- class Traveler {
- GridPoint point;
- color strokeColor;
- Traveler(GridPoint point) {
- setPoint(point);
- this.strokeColor = randomColor(palette);
- }
- void setPoint(GridPoint point) {
- this.point = point;
- this.point.visit();
- }
- Traveler clone() {
- return new Traveler(point);
- }
- void render() {
- stroke(strokeColor);
- ellipse(screenX(), screenY(), dotSize, dotSize);
- }
- int x() {
- return point.x();
- }
- int y() {
- return point.y();
- }
- float screenX() {
- return point.screenX();
- }
- float screenY() {
- return point.screenY();
- }
- }
- class GridPoint {
- final int x;
- final int y;
- final PVector point;
- boolean visited = false;
- GridPoint(int x, int y, PVector point) {
- this.x = x;
- this.y = y;
- this.point = point;
- }
- boolean notVisited() {
- return !visited;
- }
- void visit() {
- visited = true;
- }
- int x() {
- return x;
- }
- int y() {
- return y;
- }
- float screenX() {
- return point.x;
- }
- float screenY() {
- return point.y;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment