Skip to content

Instantly share code, notes, and snippets.

@gaocegege
Created January 29, 2016 10:50
Show Gist options
  • Save gaocegege/2b8e64af3dc8246949e7 to your computer and use it in GitHub Desktop.
Save gaocegege/2b8e64af3dc8246949e7 to your computer and use it in GitHub Desktop.
// 2D Array of objects
System sys;
// Number of columns and cols in the grid
// cols need to > 35 to support the gun test
int cols = 50;
void setup() {
size(400, 400);
sys = new System(cols, 400 / cols);
}
void draw() {
background(0);
sys.display();
}
// A Cell object
class Cell {
// A cell object knows about its location in the grid as well as its size with the variables x,y,w,h.
float x,y; // x,y location
float w,h; // width and height
float angle; // angle for oscillating brightness
color cor;
// Cell Constructor
Cell(float tempX, float tempY, float tempW, float tempH, color tempCor) {
x = tempX;
y = tempY;
w = tempW;
h = tempH;
cor = tempCor;
}
void display() {
stroke(255);
// Color calculated using sine wave
fill(cor);
rect(x,y,w,h);
}
}
class System {
Cell[][] grid;
int cols;
System(int tempCols, int size) {
color[] colors = new color[3];
colors[0] = color(255, 0, 0);
colors[1] = color(0, 255, 0);
colors[2] = color(0, 0, 255);
cols = tempCols;
grid = new Cell[cols][cols];
for (int i = 0; i < cols; i++) {
for (int j = 0; j < cols; j++) {
// Initialize each object
grid[i][j] = new Cell(i*size,j*size,size,size,colors[int(random(0, 3))]);
}
}
}
void display() {
// The counter variables i and j are also the column and row numbers and
// are used as arguments to the constructor for each object in the grid.
for (int i = 0; i < cols; i++) {
for (int j = 0; j < cols; j++) {
grid[i][j].display();
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment