Created
March 29, 2016 03:15
-
-
Save carlynorama/de076d0a0907a7b902e7 to your computer and use it in GitHub Desktop.
uses vornoi sketch found on open processing to generate hexgons
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //Derivative of | |
| //http://www.openprocessing.org/sketch/61968 | |
| class Cell | |
| { | |
| PVector loc; | |
| color c; | |
| Cell(int _x, int _y, color _c) | |
| { | |
| loc = new PVector(_x, _y); | |
| c = _c; | |
| } | |
| } | |
| ArrayList<Cell> cells = new ArrayList<Cell>(); | |
| boolean use_manhattan = false; | |
| int xspace = 30; | |
| int yspace = xspace * 4 /5; | |
| int xlocation; | |
| int ylocation; | |
| void setup() | |
| { | |
| size(400, 400); | |
| for(int i = 0; i < 20; i++) { | |
| for(int j = 0; j < 20; j++) { | |
| xlocation = i*xspace; | |
| ylocation = j*yspace; | |
| if (j%2==0) { | |
| xlocation +=(xspace/2); | |
| }; | |
| cells.add(new Cell(xlocation, ylocation, color(random(0,255), random(0,255), random(0,255)))); | |
| } | |
| } | |
| } | |
| void draw() | |
| { | |
| cells.get(0).loc.x = mouseX; | |
| cells.get(0).loc.y = mouseY; | |
| background(0); | |
| // Draw the voronoi cells | |
| drawVoronoi(); | |
| // Draw the points | |
| for(int i = 0; i < cells.size(); ++i) | |
| ellipse(cells.get(i).loc.x, cells.get(i).loc.y, 5, 5); | |
| } | |
| void drawVoronoi() | |
| { | |
| loadPixels(); | |
| int offset = 0; | |
| // Iterate over every pixel | |
| for(int y = 0; y < height; y++) | |
| { | |
| for(int x = 0; x < width; x++) | |
| { | |
| float shortest = 1E12; | |
| int index = -1; | |
| // Find the closest cell | |
| for(int i = 0; i < cells.size(); ++i) | |
| { | |
| Cell cc = cells.get(i); | |
| float d; | |
| if(use_manhattan) | |
| { | |
| // Manhattan Distance | |
| d = abs(x - cc.loc.x) + abs(y - cc.loc.y); | |
| } | |
| else | |
| { | |
| // Euclidean distance, dont need to sqrt it since actual distance isnt important | |
| d = sq(x-cc.loc.x) + sq(y-cc.loc.y); | |
| } | |
| if(d < shortest) | |
| { | |
| shortest = d; | |
| index = i; | |
| } | |
| } | |
| // Set the pixel to the cells color | |
| pixels[offset++] = cells.get(index).c; | |
| } | |
| } | |
| updatePixels(); | |
| } | |
| void keyPressed() | |
| { | |
| if(key == 'a') | |
| cells.add(new Cell(mouseX, mouseY, color(random(0,255), random(0,255), random(0,255)))); | |
| if(key == ' ') | |
| cells.subList(5, cells.size()).clear(); | |
| if(key == 't') | |
| use_manhattan = !use_manhattan; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment