Cellular Automata question
in
Programming Questions
•
3 months ago
Hi All,
I'm a real novice with Processing (or coding for that matter) so excuse my ignorance.
I'm trying to simulate some sort of urban growth using Cellular Automata and started by adapting the sketch for "Game of Life" by Daniel Shiffman on his book "Nature of Code".
What I'm hopping to do is to use an image (that in my case represents water on a map) to influence urban growth, by first preventing a black pixel (urban) to grow where water is (blue pixels)
But being a novice as I am :( , I'm unable to link the pixelated image map to the 2D array that is the CA to use the water pixels as a parameter for controlling growth.
Can anyone help please.
Thanks
- // Daniel Shiffman, Nature of Code
- // Cellular Automata defined by terrain map
// Modified by Omar Helmy July 2013 - GOL gol;
PImage img; - void setup() {
size(800, 800);
img = loadImage ("Map.jpg"); //load the terrain image
smooth();
frameRate (5);
gol = new GOL();
} - void draw() {
background(255); - //run the CA
gol.generate();
gol.display();
}
// reset board when mouse is pressed
void mousePressed() {
gol.init();
}- class GOL {
- int w = 4;
int columns, rows;
// Game of life board
int[][] board;
GOL() {
// Initialize rows, columns and set-up arrays
columns = width/w;
rows = height/w;
board = new int[columns][rows];
}- // The process of creating the new generation
void generate() {
int pixlSize = w; //controles the pixelation of the terrain map - //pixelation loops
for (int x= 0; x < img.width; x+= pixlSize) {
for (int y= 0; y < img.height; y+= pixlSize) {
int pixelColor = img.get(x, y); - fill(pixelColor);
stroke(0);
rect(x, y, pixlSize, pixlSize);
}
}
//Generating the CA
int[][] next = new int[columns][rows]; - // Loop through every spot in our 2D array and check spots neighbors
for (int x = 1; x < columns-1; x++) {
for (int y = 1; y < rows-1; y++) { - // Add up all the states in a 3x3 surrounding grid
int neighbors = 0;
for (int i = -1; i <= 1; i++) {
for (int j = -1; j <= 1; j++) {
neighbors += board[x+i][y+j];
}
} - // A little trick to subtract the current cell's state since
// we added it in the above loop
neighbors -= board[x][y]; - // Rules of Life
if ((board[x][y] == 1) && (neighbors < 1)) next[x][y] = 0; // Loneliness
else if ((board[x][y] == 1) && (neighbors > 5)) next[x][y] = 0; // Overpopulation
else if ((board[x][y] == 0) && (neighbors == 2)) next[x][y] = int(random(2)); // Reproduction (gave it a random probability)
else next[x][y] = board[x][y]; // Stasis
}
} - // Next is now our board
board = next;
}
void init() {
for (int i =2 ;i < columns-1 ;i++) {
for (int j =2 ;j < rows-1 ;j++) {
board[i][j] = 0;
}
}
board[100][80] = 1;
board[100+1][80] = 1;
} - // drawing the cells, fill 255 for '1', fill 0 for '0'
void display() {
for ( int i = 0; i < columns;i++) {
for ( int j = 0; j < rows;j++) {
if ((board[i][j] == 1)) fill(0);
else fill(255, 255, 255, 10);
noStroke();
rect(i*w, j*w, w, w);
}
}
}
}
-
terrain image:
1