ArrayList<Mover> movers = new ArrayList<Mover>();
boolean[][] grids;
float gridWidth = 5;
void setup(){
size(500, 500);
smooth();
frameRate(24);
background(0);
stroke(0, 255, 0);
strokeWeight(1);
grids = new boolean[100][100];
for(int x = 0; x < 100; x++){
for(int y = 0; y < 100; y++){
grids[x][y] = false;
}
}
for(int i = 0; i < 500; i++){
int x = 0;
int y = 0;
while(true){
x = int(random(100));
y = int(random(100));
if(grids[x][y] == false) break;
}
movers.add(new Mover(x, y));
}
}
void draw(){
for(Mover mover: movers){
mover.drawLine();
}
}
class Mover{
int x, y;
int direction;
Mover(int x, int y){
this.x = x;
this.y = y;
grids[x][y] = true;
direction = changeDirection();
}
void drawLine(){
if((direction == 0 && (x - 1 == -1 || grids[x - 1][y] == true)) ||
(direction == 1 && (x + 1 == 100 || grids[x + 1][y] == true)) ||
(direction == 2 && (y - 1 == -1 || grids[x][y - 1] == true)) ||
(direction == 3 && (y + 1 == 100 || grids[x][y + 1] == true))) {
direction = changeDirection();
}
if(direction == -1){
return;
}
int nextX = 0;
int nextY = 0;
if(direction == 0){
nextX = x - 1;
nextY = y;
} else if(direction == 1) {
nextX = x + 1;
nextY = y;
} else if(direction == 2){
nextX = x;
nextY = y - 1;
} else if(direction == 3) {
nextX = x;
nextY = y + 1;
}
line(x * gridWidth + gridWidth/2, y * gridWidth + gridWidth/2,
nextX * gridWidth + gridWidth/2, nextY * gridWidth + gridWidth/2);
x = nextX;
y = nextY;
grids[x][y] = true;
}
int changeDirection(){
ArrayList<Integer> possibleDir = new ArrayList<Integer>();
if(x - 1 >= 0 && grids[x - 1][y] == false){
possibleDir.add(0);
}
if(x + 1 <= 100 - 1 && grids[x + 1][y] == false){
possibleDir.add(1);
}
if(y - 1 >= 0 && grids[x][y - 1] == false){
possibleDir.add(2);
}
if(y + 1 <= 100 - 1 && grids[x][y + 1] == false){
possibleDir.add(3);
}
if(possibleDir.size() == 0){
return -1;
} else {
return possibleDir.get(int(random(possibleDir.size())));
}
}
}