Skip to content

Instantly share code, notes, and snippets.

@josephtaylor
Created January 21, 2013 02:10
Show Gist options
  • Save josephtaylor/4583136 to your computer and use it in GitHub Desktop.
Save josephtaylor/4583136 to your computer and use it in GitHub Desktop.
import java.util.ListIterator;
GridSpace[][] grid;
final int SIZE = 200;
ArrayList<GridSpace> points;
void setup() {
size(1000, 1000);
background(255);
stroke(0,0,0);
smooth();
strokeCap(SQUARE);
grid = new GridSpace[SIZE][SIZE];
int xIndex = 0;
for(int x = width / SIZE; x < width; x += (width / SIZE)) {
int yIndex = 0;
for(int y = height / SIZE; y < height; y += (height / SIZE)) {
GridSpace space = new GridSpace(new PVector(x,y), xIndex, yIndex);
grid[xIndex][yIndex] = space;
yIndex++;
}
xIndex++;
}
points = new ArrayList<GridSpace>();
this.initializeGrid();
}
void draw() {
if(points.isEmpty()) {
noLoop();
saveFrame("gridStuff" + (int) random(36500) + ".png");
}
ListIterator<GridSpace> it = points.listIterator();
while(it.hasNext()) {
GridSpace space = it.next();
GridSpace newSpace = findPlaceToMove(space);
if(newSpace == null) {
it.remove();
}
else {
line(space.loc.x, space.loc.y, newSpace.loc.x, newSpace.loc.y);
space.loc.set(newSpace.loc.x, newSpace.loc.y, 0);
space.xIndex = newSpace.xIndex;
space.yIndex = newSpace.yIndex;
}
}
}
public void initializeGrid() {
for(int i = 0; i < 800; i++) {
int x = (int) random(SIZE);
int y = (int) random(SIZE);
GridSpace space = grid[x][y];
if(space != null) {
PVector p = space.loc;
points.add(new GridSpace(new PVector(p.x,p.y), x, y));
}
}
}
public GridSpace findPlaceToMove(GridSpace space) {
int x = space.xIndex;
int y = space.yIndex;
GridSpace newPoint = null;
int[] searches = new int[8];
for(int i = 0; i < 8; i++) {
searches[i] = 0;
}
boolean found = false;
while(!found) {
boolean allChecked = true;
for(int i = 0; i < 8; i++) {
if(searches[i] == 0) {
allChecked = false;
}
}
if(allChecked) {
return null;
}
int rand = (int) random(8);
if(searches[rand] == 1) {
continue;
}
else {
searches[rand] = 1;
int newX = 0;
int newY = 0;
if(rand == 0) {
newX = x - 1;
newY = y - 1;
}
else if(rand == 1) {
newX = x - 1;
newY = y;
}
else if(rand == 2) {
newX = x - 1;
newY = y + 1;
}
else if(rand == 3) {
newX = x;
newY = y + 1;
}
else if(rand == 4) {
newX = x + 1;
newY = y + 1;
}
else if(rand == 5) {
newX = x + 1;
newY = y;
}
else if(rand == 6) {
newX = x + 1;
newY = y - 1;
}
else if(rand == 6) {
newX = x + 1;
newY = y - 1;
}
else if(rand == 7) {
newX = x;
newY = y - 1;
}
if((newX < 0 || newX > (SIZE - 1)) ||
(newY < 0 || newY > (SIZE - 1))) {
continue;
}
GridSpace newSpace = grid[newX][newY];
if(newSpace == null) {
continue;
}
if(!newSpace.isFilled()) {
newPoint = new GridSpace(new PVector(newSpace.loc.x, newSpace.loc.y), newX, newY);
found = true;
newSpace.setFilled(true);
}
}
}
return newPoint;
}
public class GridSpace
{
public PVector loc;
public int xIndex;
public int yIndex;
private boolean isFilled;
public GridSpace(PVector loc, int xIndex, int yIndex) {
this.loc = loc;
this.xIndex = xIndex;
this.yIndex = yIndex;
isFilled = false;
}
public void setFilled(boolean filled) {
isFilled = filled;
}
public boolean isFilled() {
return isFilled;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment