mirror of
https://github.com/processing/processing4.git
synced 2026-06-16 04:26:26 +02:00
This commit is contained in:
@@ -0,0 +1,80 @@
|
||||
/**
|
||||
* Conway's Game of Life
|
||||
* by Mike Davis.
|
||||
*
|
||||
* This program is a simple version of Conway's
|
||||
* game of Life. A lit point turns off if there
|
||||
* are fewer than two or more than three surrounding
|
||||
* lit points. An unlit point turns on if there
|
||||
* are exactly three lit neighbors. The 'density'
|
||||
* parameter determines how much of the board will
|
||||
* start out lit.
|
||||
*/
|
||||
|
||||
int sx, sy;
|
||||
float density = 0.5;
|
||||
int[][][] world;
|
||||
|
||||
void setup()
|
||||
{
|
||||
size(640, 200, P2D);
|
||||
frameRate(12);
|
||||
sx = width;
|
||||
sy = height;
|
||||
world = new int[sx][sy][2];
|
||||
|
||||
// Set random cells to 'on'
|
||||
for (int i = 0; i < sx * sy * density; i++) {
|
||||
world[(int)random(sx)][(int)random(sy)][1] = 1;
|
||||
}
|
||||
}
|
||||
|
||||
void draw()
|
||||
{
|
||||
background(0);
|
||||
|
||||
// Drawing and update cycle
|
||||
for (int x = 0; x < sx; x=x+1) {
|
||||
for (int y = 0; y < sy; y=y+1) {
|
||||
//if (world[x][y][1] == 1)
|
||||
// Change recommended by The.Lucky.Mutt
|
||||
if ((world[x][y][1] == 1) || (world[x][y][1] == 0 && world[x][y][0] == 1))
|
||||
{
|
||||
world[x][y][0] = 1;
|
||||
set(x, y, #FFFFFF);
|
||||
}
|
||||
if (world[x][y][1] == -1)
|
||||
{
|
||||
world[x][y][0] = 0;
|
||||
}
|
||||
world[x][y][1] = 0;
|
||||
}
|
||||
}
|
||||
// Birth and death cycle
|
||||
for (int x = 0; x < sx; x=x+1) {
|
||||
for (int y = 0; y < sy; y=y+1) {
|
||||
int count = neighbors(x, y);
|
||||
if (count == 3 && world[x][y][0] == 0)
|
||||
{
|
||||
world[x][y][1] = 1;
|
||||
}
|
||||
if ((count < 2 || count > 3) && world[x][y][0] == 1)
|
||||
{
|
||||
world[x][y][1] = -1;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Count the number of adjacent cells 'on'
|
||||
int neighbors(int x, int y)
|
||||
{
|
||||
return world[(x + 1) % sx][y][0] +
|
||||
world[x][(y + 1) % sy][0] +
|
||||
world[(x + sx - 1) % sx][y][0] +
|
||||
world[x][(y + sy - 1) % sy][0] +
|
||||
world[(x + 1) % sx][(y + 1) % sy][0] +
|
||||
world[(x + sx - 1) % sx][(y + 1) % sy][0] +
|
||||
world[(x + sx - 1) % sx][(y + sy - 1) % sy][0] +
|
||||
world[(x + 1) % sx][(y + sy - 1) % sy][0];
|
||||
}
|
||||
@@ -0,0 +1,101 @@
|
||||
import processing.core.*;
|
||||
|
||||
import java.applet.*;
|
||||
import java.awt.*;
|
||||
import java.awt.image.*;
|
||||
import java.awt.event.*;
|
||||
import java.io.*;
|
||||
import java.net.*;
|
||||
import java.text.*;
|
||||
import java.util.*;
|
||||
import java.util.zip.*;
|
||||
import java.util.regex.*;
|
||||
|
||||
public class Conway extends PApplet {
|
||||
|
||||
/**
|
||||
* Conway's Game of Life
|
||||
* by Mike Davis.
|
||||
*
|
||||
* This program is a simple version of Conway's
|
||||
* game of Life. A lit point turns off if there
|
||||
* are fewer than two or more than three surrounding
|
||||
* lit points. An unlit point turns on if there
|
||||
* are exactly three lit neighbors. The 'density'
|
||||
* parameter determines how much of the board will
|
||||
* start out lit.
|
||||
*/
|
||||
|
||||
int sx, sy;
|
||||
float density = 0.5f;
|
||||
int[][][] world;
|
||||
|
||||
public void setup()
|
||||
{
|
||||
size(640, 200, P3D);
|
||||
frameRate(12);
|
||||
sx = width;
|
||||
sy = height;
|
||||
world = new int[sx][sy][2];
|
||||
stroke(255);
|
||||
|
||||
// Set random cells to 'on'
|
||||
for (int i = 0; i < sx * sy * density; i++) {
|
||||
world[(int)random(sx)][(int)random(sy)][1] = 1;
|
||||
}
|
||||
}
|
||||
|
||||
public void draw()
|
||||
{
|
||||
background(0);
|
||||
|
||||
// Drawing and update cycle
|
||||
for (int x = 0; x < sx; x=x+1) {
|
||||
for (int y = 0; y < sy; y=y+1) {
|
||||
//if (world[x][y][1] == 1)
|
||||
// Change recommended by The.Lucky.Mutt
|
||||
if ((world[x][y][1] == 1) || (world[x][y][1] == 0 && world[x][y][0] == 1))
|
||||
{
|
||||
world[x][y][0] = 1;
|
||||
point(x, y);
|
||||
}
|
||||
if (world[x][y][1] == -1)
|
||||
{
|
||||
world[x][y][0] = 0;
|
||||
}
|
||||
world[x][y][1] = 0;
|
||||
}
|
||||
}
|
||||
// Birth and death cycle
|
||||
for (int x = 0; x < sx; x=x+1) {
|
||||
for (int y = 0; y < sy; y=y+1) {
|
||||
int count = neighbors(x, y);
|
||||
if (count == 3 && world[x][y][0] == 0)
|
||||
{
|
||||
world[x][y][1] = 1;
|
||||
}
|
||||
if ((count < 2 || count > 3) && world[x][y][0] == 1)
|
||||
{
|
||||
world[x][y][1] = -1;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Count the number of adjacent cells 'on'
|
||||
public int neighbors(int x, int y)
|
||||
{
|
||||
return world[(x + 1) % sx][y][0] +
|
||||
world[x][(y + 1) % sy][0] +
|
||||
world[(x + sx - 1) % sx][y][0] +
|
||||
world[x][(y + sy - 1) % sy][0] +
|
||||
world[(x + 1) % sx][(y + 1) % sy][0] +
|
||||
world[(x + sx - 1) % sx][(y + 1) % sy][0] +
|
||||
world[(x + sx - 1) % sx][(y + sy - 1) % sy][0] +
|
||||
world[(x + 1) % sx][(y + sy - 1) % sy][0];
|
||||
}
|
||||
|
||||
static public void main(String args[]) {
|
||||
PApplet.main(new String[] { "Conway" });
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,81 @@
|
||||
/**
|
||||
* Conway's Game of Life
|
||||
* by Mike Davis.
|
||||
*
|
||||
* This program is a simple version of Conway's
|
||||
* game of Life. A lit point turns off if there
|
||||
* are fewer than two or more than three surrounding
|
||||
* lit points. An unlit point turns on if there
|
||||
* are exactly three lit neighbors. The 'density'
|
||||
* parameter determines how much of the board will
|
||||
* start out lit.
|
||||
*/
|
||||
|
||||
int sx, sy;
|
||||
float density = 0.5;
|
||||
int[][][] world;
|
||||
|
||||
void setup()
|
||||
{
|
||||
size(640, 200, P3D);
|
||||
frameRate(12);
|
||||
sx = width;
|
||||
sy = height;
|
||||
world = new int[sx][sy][2];
|
||||
stroke(255);
|
||||
|
||||
// Set random cells to 'on'
|
||||
for (int i = 0; i < sx * sy * density; i++) {
|
||||
world[(int)random(sx)][(int)random(sy)][1] = 1;
|
||||
}
|
||||
}
|
||||
|
||||
void draw()
|
||||
{
|
||||
background(0);
|
||||
|
||||
// Drawing and update cycle
|
||||
for (int x = 0; x < sx; x=x+1) {
|
||||
for (int y = 0; y < sy; y=y+1) {
|
||||
//if (world[x][y][1] == 1)
|
||||
// Change recommended by The.Lucky.Mutt
|
||||
if ((world[x][y][1] == 1) || (world[x][y][1] == 0 && world[x][y][0] == 1))
|
||||
{
|
||||
world[x][y][0] = 1;
|
||||
point(x, y);
|
||||
}
|
||||
if (world[x][y][1] == -1)
|
||||
{
|
||||
world[x][y][0] = 0;
|
||||
}
|
||||
world[x][y][1] = 0;
|
||||
}
|
||||
}
|
||||
// Birth and death cycle
|
||||
for (int x = 0; x < sx; x=x+1) {
|
||||
for (int y = 0; y < sy; y=y+1) {
|
||||
int count = neighbors(x, y);
|
||||
if (count == 3 && world[x][y][0] == 0)
|
||||
{
|
||||
world[x][y][1] = 1;
|
||||
}
|
||||
if ((count < 2 || count > 3) && world[x][y][0] == 1)
|
||||
{
|
||||
world[x][y][1] = -1;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Count the number of adjacent cells 'on'
|
||||
int neighbors(int x, int y)
|
||||
{
|
||||
return world[(x + 1) % sx][y][0] +
|
||||
world[x][(y + 1) % sy][0] +
|
||||
world[(x + sx - 1) % sx][y][0] +
|
||||
world[x][(y + sy - 1) % sy][0] +
|
||||
world[(x + 1) % sx][(y + 1) % sy][0] +
|
||||
world[(x + sx - 1) % sx][(y + 1) % sy][0] +
|
||||
world[(x + sx - 1) % sx][(y + sy - 1) % sy][0] +
|
||||
world[(x + 1) % sx][(y + sy - 1) % sy][0];
|
||||
}
|
||||
Binary file not shown.
|
After Width: | Height: | Size: 2.2 KiB |
@@ -0,0 +1,133 @@
|
||||
/**
|
||||
* Spore 1
|
||||
* by Mike Davis.
|
||||
*
|
||||
* A short program for alife experiments. Click in the window to restart.
|
||||
* Each cell is represented by a pixel on the display as well as an entry in
|
||||
* the array 'cells'. Each cell has a run() method, which performs actions
|
||||
* based on the cell's surroundings. Cells run one at a time (to avoid conflicts
|
||||
* like wanting to move to the same space) and in random order.
|
||||
*/
|
||||
|
||||
World w;
|
||||
int numcells = 0;
|
||||
int maxcells = 6700;
|
||||
Cell[] cells = new Cell[maxcells];
|
||||
color spore_color;
|
||||
// set lower for smoother animation, higher for faster simulation
|
||||
int runs_per_loop = 10000;
|
||||
color black = color(0, 0, 0);
|
||||
|
||||
void setup()
|
||||
{
|
||||
size(640, 200, P2D);
|
||||
frameRate(24);
|
||||
clearscr();
|
||||
w = new World();
|
||||
spore_color = color(172, 255, 128);
|
||||
seed();
|
||||
}
|
||||
|
||||
void seed()
|
||||
{
|
||||
// Add cells at random places
|
||||
for (int i = 0; i < maxcells; i++)
|
||||
{
|
||||
int cX = (int)random(width);
|
||||
int cY = (int)random(height);
|
||||
if (w.getpix(cX, cY) == black)
|
||||
{
|
||||
w.setpix(cX, cY, spore_color);
|
||||
cells[numcells] = new Cell(cX, cY);
|
||||
numcells++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void draw()
|
||||
{
|
||||
// Run cells in random order
|
||||
for (int i = 0; i < runs_per_loop; i++) {
|
||||
int selected = min((int)random(numcells), numcells - 1);
|
||||
cells[selected].run();
|
||||
}
|
||||
}
|
||||
|
||||
void clearscr()
|
||||
{
|
||||
background(0);
|
||||
}
|
||||
|
||||
class Cell
|
||||
{
|
||||
int x, y;
|
||||
Cell(int xin, int yin)
|
||||
{
|
||||
x = xin;
|
||||
y = yin;
|
||||
}
|
||||
|
||||
// Perform action based on surroundings
|
||||
void run()
|
||||
{
|
||||
// Fix cell coordinates
|
||||
while(x < 0) {
|
||||
x+=width;
|
||||
}
|
||||
while(x > width - 1) {
|
||||
x-=width;
|
||||
}
|
||||
while(y < 0) {
|
||||
y+=height;
|
||||
}
|
||||
while(y > height - 1) {
|
||||
y-=height;
|
||||
}
|
||||
|
||||
// Cell instructions
|
||||
if (w.getpix(x + 1, y) == black) {
|
||||
move(0, 1);
|
||||
} else if (w.getpix(x, y - 1) != black && w.getpix(x, y + 1) != black) {
|
||||
move((int)random(9) - 4, (int)random(9) - 4);
|
||||
}
|
||||
}
|
||||
|
||||
// Will move the cell (dx, dy) units if that space is empty
|
||||
void move(int dx, int dy) {
|
||||
if (w.getpix(x + dx, y + dy) == black) {
|
||||
w.setpix(x + dx, y + dy, w.getpix(x, y));
|
||||
w.setpix(x, y, color(0));
|
||||
x += dx;
|
||||
y += dy;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// The World class simply provides two functions, get and set, which access the
|
||||
// display in the same way as getPixel and setPixel. The only difference is that
|
||||
// the World class's get and set do screen wraparound ("toroidal coordinates").
|
||||
class World
|
||||
{
|
||||
void setpix(int x, int y, int c) {
|
||||
while(x < 0) x+=width;
|
||||
while(x > width - 1) x-=width;
|
||||
while(y < 0) y+=height;
|
||||
while(y > height - 1) y-=height;
|
||||
set(x, y, c);
|
||||
}
|
||||
|
||||
color getpix(int x, int y) {
|
||||
while(x < 0) x+=width;
|
||||
while(x > width - 1) x-=width;
|
||||
while(y < 0) y+=height;
|
||||
while(y > height - 1) y-=height;
|
||||
return get(x, y);
|
||||
}
|
||||
}
|
||||
|
||||
void mousePressed()
|
||||
{
|
||||
numcells = 0;
|
||||
setup();
|
||||
}
|
||||
|
||||
@@ -0,0 +1,153 @@
|
||||
import processing.core.*;
|
||||
|
||||
import java.applet.*;
|
||||
import java.awt.*;
|
||||
import java.awt.image.*;
|
||||
import java.awt.event.*;
|
||||
import java.io.*;
|
||||
import java.net.*;
|
||||
import java.text.*;
|
||||
import java.util.*;
|
||||
import java.util.zip.*;
|
||||
import java.util.regex.*;
|
||||
|
||||
public class Spore1 extends PApplet {
|
||||
|
||||
/**
|
||||
* Spore 1
|
||||
* by Mike Davis.
|
||||
*
|
||||
* A short program for alife experiments. Click in the window to restart.
|
||||
* Each cell is represented by a pixel on the display as well as an entry in
|
||||
* the array 'cells'. Each cell has a run() method, which performs actions
|
||||
* based on the cell's surroundings. Cells run one at a time (to avoid conflicts
|
||||
* like wanting to move to the same space) and in random order.
|
||||
*/
|
||||
|
||||
World w;
|
||||
int numcells = 0;
|
||||
int maxcells = 6700;
|
||||
Cell[] cells = new Cell[maxcells];
|
||||
int spore_color;
|
||||
// set lower for smoother animation, higher for faster simulation
|
||||
int runs_per_loop = 10000;
|
||||
int black = color(0, 0, 0);
|
||||
|
||||
public void setup()
|
||||
{
|
||||
size(640, 200, P2D);
|
||||
frameRate(24);
|
||||
clearscr();
|
||||
w = new World();
|
||||
spore_color = color(172, 255, 128);
|
||||
seed();
|
||||
}
|
||||
|
||||
public void seed()
|
||||
{
|
||||
// Add cells at random places
|
||||
for (int i = 0; i < maxcells; i++)
|
||||
{
|
||||
int cX = (int)random(width);
|
||||
int cY = (int)random(height);
|
||||
if (w.getpix(cX, cY) == black)
|
||||
{
|
||||
w.setpix(cX, cY, spore_color);
|
||||
cells[numcells] = new Cell(cX, cY);
|
||||
numcells++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void draw()
|
||||
{
|
||||
// Run cells in random order
|
||||
for (int i = 0; i < runs_per_loop; i++) {
|
||||
int selected = min((int)random(numcells), numcells - 1);
|
||||
cells[selected].run();
|
||||
}
|
||||
}
|
||||
|
||||
public void clearscr()
|
||||
{
|
||||
background(0);
|
||||
}
|
||||
|
||||
class Cell
|
||||
{
|
||||
int x, y;
|
||||
Cell(int xin, int yin)
|
||||
{
|
||||
x = xin;
|
||||
y = yin;
|
||||
}
|
||||
|
||||
// Perform action based on surroundings
|
||||
public void run()
|
||||
{
|
||||
// Fix cell coordinates
|
||||
while(x < 0) {
|
||||
x+=width;
|
||||
}
|
||||
while(x > width - 1) {
|
||||
x-=width;
|
||||
}
|
||||
while(y < 0) {
|
||||
y+=height;
|
||||
}
|
||||
while(y > height - 1) {
|
||||
y-=height;
|
||||
}
|
||||
|
||||
// Cell instructions
|
||||
if (w.getpix(x + 1, y) == black) {
|
||||
move(0, 1);
|
||||
} else if (w.getpix(x, y - 1) != black && w.getpix(x, y + 1) != black) {
|
||||
move((int)random(9) - 4, (int)random(9) - 4);
|
||||
}
|
||||
}
|
||||
|
||||
// Will move the cell (dx, dy) units if that space is empty
|
||||
public void move(int dx, int dy) {
|
||||
if (w.getpix(x + dx, y + dy) == black) {
|
||||
w.setpix(x + dx, y + dy, w.getpix(x, y));
|
||||
w.setpix(x, y, color(0));
|
||||
x += dx;
|
||||
y += dy;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// The World class simply provides two functions, get and set, which access the
|
||||
// display in the same way as getPixel and setPixel. The only difference is that
|
||||
// the World class's get and set do screen wraparound ("toroidal coordinates").
|
||||
class World
|
||||
{
|
||||
public void setpix(int x, int y, int c) {
|
||||
while(x < 0) x+=width;
|
||||
while(x > width - 1) x-=width;
|
||||
while(y < 0) y+=height;
|
||||
while(y > height - 1) y-=height;
|
||||
set(x, y, c);
|
||||
}
|
||||
|
||||
public int getpix(int x, int y) {
|
||||
while(x < 0) x+=width;
|
||||
while(x > width - 1) x-=width;
|
||||
while(y < 0) y+=height;
|
||||
while(y > height - 1) y-=height;
|
||||
return get(x, y);
|
||||
}
|
||||
}
|
||||
|
||||
public void mousePressed()
|
||||
{
|
||||
numcells = 0;
|
||||
setup();
|
||||
}
|
||||
|
||||
|
||||
static public void main(String args[]) {
|
||||
PApplet.main(new String[] { "Spore1" });
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,133 @@
|
||||
/**
|
||||
* Spore 1
|
||||
* by Mike Davis.
|
||||
*
|
||||
* A short program for alife experiments. Click in the window to restart.
|
||||
* Each cell is represented by a pixel on the display as well as an entry in
|
||||
* the array 'cells'. Each cell has a run() method, which performs actions
|
||||
* based on the cell's surroundings. Cells run one at a time (to avoid conflicts
|
||||
* like wanting to move to the same space) and in random order.
|
||||
*/
|
||||
|
||||
World w;
|
||||
int numcells = 0;
|
||||
int maxcells = 6700;
|
||||
Cell[] cells = new Cell[maxcells];
|
||||
color spore_color;
|
||||
// set lower for smoother animation, higher for faster simulation
|
||||
int runs_per_loop = 10000;
|
||||
color black = color(0, 0, 0);
|
||||
|
||||
void setup()
|
||||
{
|
||||
size(640, 200, P2D);
|
||||
frameRate(24);
|
||||
clearscr();
|
||||
w = new World();
|
||||
spore_color = color(172, 255, 128);
|
||||
seed();
|
||||
}
|
||||
|
||||
void seed()
|
||||
{
|
||||
// Add cells at random places
|
||||
for (int i = 0; i < maxcells; i++)
|
||||
{
|
||||
int cX = (int)random(width);
|
||||
int cY = (int)random(height);
|
||||
if (w.getpix(cX, cY) == black)
|
||||
{
|
||||
w.setpix(cX, cY, spore_color);
|
||||
cells[numcells] = new Cell(cX, cY);
|
||||
numcells++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void draw()
|
||||
{
|
||||
// Run cells in random order
|
||||
for (int i = 0; i < runs_per_loop; i++) {
|
||||
int selected = min((int)random(numcells), numcells - 1);
|
||||
cells[selected].run();
|
||||
}
|
||||
}
|
||||
|
||||
void clearscr()
|
||||
{
|
||||
background(0);
|
||||
}
|
||||
|
||||
class Cell
|
||||
{
|
||||
int x, y;
|
||||
Cell(int xin, int yin)
|
||||
{
|
||||
x = xin;
|
||||
y = yin;
|
||||
}
|
||||
|
||||
// Perform action based on surroundings
|
||||
void run()
|
||||
{
|
||||
// Fix cell coordinates
|
||||
while(x < 0) {
|
||||
x+=width;
|
||||
}
|
||||
while(x > width - 1) {
|
||||
x-=width;
|
||||
}
|
||||
while(y < 0) {
|
||||
y+=height;
|
||||
}
|
||||
while(y > height - 1) {
|
||||
y-=height;
|
||||
}
|
||||
|
||||
// Cell instructions
|
||||
if (w.getpix(x + 1, y) == black) {
|
||||
move(0, 1);
|
||||
} else if (w.getpix(x, y - 1) != black && w.getpix(x, y + 1) != black) {
|
||||
move((int)random(9) - 4, (int)random(9) - 4);
|
||||
}
|
||||
}
|
||||
|
||||
// Will move the cell (dx, dy) units if that space is empty
|
||||
void move(int dx, int dy) {
|
||||
if (w.getpix(x + dx, y + dy) == black) {
|
||||
w.setpix(x + dx, y + dy, w.getpix(x, y));
|
||||
w.setpix(x, y, color(0));
|
||||
x += dx;
|
||||
y += dy;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// The World class simply provides two functions, get and set, which access the
|
||||
// display in the same way as getPixel and setPixel. The only difference is that
|
||||
// the World class's get and set do screen wraparound ("toroidal coordinates").
|
||||
class World
|
||||
{
|
||||
void setpix(int x, int y, int c) {
|
||||
while(x < 0) x+=width;
|
||||
while(x > width - 1) x-=width;
|
||||
while(y < 0) y+=height;
|
||||
while(y > height - 1) y-=height;
|
||||
set(x, y, c);
|
||||
}
|
||||
|
||||
color getpix(int x, int y) {
|
||||
while(x < 0) x+=width;
|
||||
while(x > width - 1) x-=width;
|
||||
while(y < 0) y+=height;
|
||||
while(y > height - 1) y-=height;
|
||||
return get(x, y);
|
||||
}
|
||||
}
|
||||
|
||||
void mousePressed()
|
||||
{
|
||||
numcells = 0;
|
||||
setup();
|
||||
}
|
||||
|
||||
Binary file not shown.
|
After Width: | Height: | Size: 2.2 KiB |
@@ -0,0 +1,174 @@
|
||||
/**
|
||||
* Spore 2
|
||||
* by Mike Davis.
|
||||
*
|
||||
* A short program for alife experiments. Click in the window to restart.
|
||||
* Each cell is represented by a pixel on the display as well as an entry in
|
||||
* the array 'cells'. Each cell has a run() method, which performs actions
|
||||
* based on the cell's surroundings. Cells run one at a time (to avoid conflicts
|
||||
* like wanting to move to the same space) and in random order.
|
||||
*/
|
||||
|
||||
World w;
|
||||
int maxcells = 8000;
|
||||
int numcells;
|
||||
Cell[] cells = new Cell[maxcells];
|
||||
color spore1, spore2, spore3, spore4;
|
||||
color black = color(0, 0, 0);
|
||||
// set lower for smoother animation, higher for faster simulation
|
||||
int runs_per_loop = 10000;
|
||||
|
||||
void setup()
|
||||
{
|
||||
size(640, 200, P2D);
|
||||
frameRate(24);
|
||||
clearscr();
|
||||
w = new World();
|
||||
spore1 = color(128, 172, 255);
|
||||
spore2 = color(64, 128, 255);
|
||||
spore3 = color(255, 128, 172);
|
||||
spore4 = color(255, 64, 128);
|
||||
numcells = 0;
|
||||
seed();
|
||||
}
|
||||
|
||||
void seed()
|
||||
{
|
||||
// Add cells at random places
|
||||
for (int i = 0; i < maxcells; i++)
|
||||
{
|
||||
int cX = int(random(width));
|
||||
int cY = int(random(height));
|
||||
int c;
|
||||
float r = random(1);
|
||||
if (r < 0.25) c = spore1;
|
||||
else if (r < 0.5) c = spore2;
|
||||
else if (r < 0.75) c = spore3;
|
||||
else c = spore4;
|
||||
if (w.getpix(cX, cY) == black)
|
||||
{
|
||||
w.setpix(cX, cY, c);
|
||||
cells[numcells] = new Cell(cX, cY);
|
||||
numcells++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void draw()
|
||||
{
|
||||
// Run cells in random order
|
||||
for (int i = 0; i < runs_per_loop; i++) {
|
||||
int selected = min((int)random(numcells), numcells - 1);
|
||||
cells[selected].run();
|
||||
}
|
||||
}
|
||||
|
||||
void clearscr()
|
||||
{
|
||||
for (int y = 0; y < height; y++) {
|
||||
for (int x = 0; x < width; x++) {
|
||||
set(x, y, color(0));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class Cell
|
||||
{
|
||||
int x, y;
|
||||
Cell(int xin, int yin)
|
||||
{
|
||||
x = xin;
|
||||
y = yin;
|
||||
}
|
||||
// Perform action based on surroundings
|
||||
void run()
|
||||
{
|
||||
// Fix cell coordinates
|
||||
while(x < 0) {
|
||||
x+=width;
|
||||
}
|
||||
while(x > width - 1) {
|
||||
x-=width;
|
||||
}
|
||||
while(y < 0) {
|
||||
y+=height;
|
||||
}
|
||||
while(y > height - 1) {
|
||||
y-=height;
|
||||
}
|
||||
|
||||
// Cell instructions
|
||||
int myColor = w.getpix(x, y);
|
||||
if (myColor == spore1) {
|
||||
if (w.getpix(x - 1, y + 1) == black && w.getpix(x + 1, y + 1) == black && w.getpix(x, y + 1) == black) move(0, 1);
|
||||
else if (w.getpix(x - 1, y) == spore2 && w.getpix(x - 1, y - 1) != black) move(0, -1);
|
||||
else if (w.getpix(x - 1, y) == spore2 && w.getpix(x - 1, y - 1) == black) move(-1, -1);
|
||||
else if (w.getpix(x + 1, y) == spore1 && w.getpix(x + 1, y - 1) != black) move(0, -1);
|
||||
else if (w.getpix(x + 1, y) == spore1 && w.getpix(x + 1, y - 1) == black) move(1, -1);
|
||||
else move((int)random(3) - 1, 0);
|
||||
} else if (myColor == spore2) {
|
||||
if (w.getpix(x - 1, y + 1) == black && w.getpix(x + 1, y + 1) == black && w.getpix(x, y + 1) == black) move(0, 1);
|
||||
else if (w.getpix(x + 1, y) == spore1 && w.getpix(x + 1, y - 1) != black) move(0, -1);
|
||||
else if (w.getpix(x + 1, y) == spore1 && w.getpix(x + 1, y - 1) == black) move(1, -1);
|
||||
else if (w.getpix(x - 1, y) == spore2 && w.getpix(x - 1, y - 1) != black) move(0, -1);
|
||||
else if (w.getpix(x - 1, y) == spore2 && w.getpix(x - 1, y - 1) == black) move(-1, -1);
|
||||
else move((int)random(3) - 1, 0);
|
||||
}
|
||||
else if (myColor == spore3)
|
||||
{
|
||||
if (w.getpix(x - 1, y - 1) == black && w.getpix(x + 1, y - 1) == black && w.getpix(x, y - 1) == black) move(0, -1);
|
||||
else if (w.getpix(x - 1, y) == spore4 && w.getpix(x - 1, y + 1) != black) move(0, 1);
|
||||
else if (w.getpix(x - 1, y) == spore4 && w.getpix(x - 1, y + 1) == black) move(-1, 1);
|
||||
else if (w.getpix(x + 1, y) == spore3 && w.getpix(x + 1, y + 1) != black) move(0, 1);
|
||||
else if (w.getpix(x + 1, y) == spore3 && w.getpix(x + 1, y + 1) == black) move(1, 1);
|
||||
else move((int)random(3) - 1, 0);
|
||||
}
|
||||
else if (myColor == spore4)
|
||||
{
|
||||
if (w.getpix(x - 1, y - 1) == black && w.getpix(x + 1, y - 1) == black && w.getpix(x, y - 1) == black) move(0, -1);
|
||||
else if (w.getpix(x + 1, y) == spore3 && w.getpix(x + 1, y + 1) != black) move(0, 1);
|
||||
else if (w.getpix(x + 1, y) == spore3 && w.getpix(x + 1, y + 1) == black) move(1, 1);
|
||||
else if (w.getpix(x - 1, y) == spore4 && w.getpix(x - 1, y + 1) != black) move(0, 1);
|
||||
else if (w.getpix(x - 1, y) == spore4 && w.getpix(x - 1, y + 1) == black) move(-1, 1);
|
||||
else move((int)random(3) - 1, 0);
|
||||
}
|
||||
}
|
||||
|
||||
// Will move the cell (dx, dy) units if that space is empty
|
||||
void move(int dx, int dy) {
|
||||
if (w.getpix(x + dx, y + dy) == black) {
|
||||
w.setpix(x + dx, y + dy, w.getpix(x, y));
|
||||
w.setpix(x, y, color(0));
|
||||
x += dx;
|
||||
y += dy;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// The World class simply provides two functions, get and set, which access the
|
||||
// display in the same way as getPixel and setPixel. The only difference is that
|
||||
// the World class's get and set do screen wraparound ("toroidal coordinates").
|
||||
class World
|
||||
{
|
||||
void setpix(int x, int y, int c) {
|
||||
while(x < 0) x+=width;
|
||||
while(x > width - 1) x-=width;
|
||||
while(y < 0) y+=height;
|
||||
while(y > height - 1) y-=height;
|
||||
set(x, y, c);
|
||||
}
|
||||
|
||||
color getpix(int x, int y) {
|
||||
while(x < 0) x+=width;
|
||||
while(x > width - 1) x-=width;
|
||||
while(y < 0) y+=height;
|
||||
while(y > height - 1) y-=height;
|
||||
return get(x, y);
|
||||
}
|
||||
}
|
||||
|
||||
void mousePressed()
|
||||
{
|
||||
setup();
|
||||
}
|
||||
|
||||
@@ -0,0 +1,196 @@
|
||||
import processing.core.*;
|
||||
|
||||
import java.applet.*;
|
||||
import java.awt.*;
|
||||
import java.awt.image.*;
|
||||
import java.awt.event.*;
|
||||
import java.io.*;
|
||||
import java.net.*;
|
||||
import java.text.*;
|
||||
import java.util.*;
|
||||
import java.util.zip.*;
|
||||
import java.util.regex.*;
|
||||
|
||||
public class Spore2 extends PApplet {
|
||||
|
||||
/**
|
||||
* Spore 2
|
||||
* by Mike Davis.
|
||||
*
|
||||
* A short program for alife experiments. Click in the window to restart.
|
||||
* Each cell is represented by a pixel on the display as well as an entry in
|
||||
* the array 'cells'. Each cell has a run() method, which performs actions
|
||||
* based on the cell's surroundings. Cells run one at a time (to avoid conflicts
|
||||
* like wanting to move to the same space) and in random order.
|
||||
*/
|
||||
|
||||
World w;
|
||||
int maxcells = 8000;
|
||||
int numcells;
|
||||
Cell[] cells = new Cell[maxcells];
|
||||
int spore1, spore2, spore3, spore4;
|
||||
int black = color(0, 0, 0);
|
||||
// set lower for smoother animation, higher for faster simulation
|
||||
int runs_per_loop = 10000;
|
||||
|
||||
public void setup()
|
||||
{
|
||||
size(640, 200, P2D);
|
||||
frameRate(24);
|
||||
clearscr();
|
||||
w = new World();
|
||||
spore1 = color(128, 172, 255);
|
||||
spore2 = color(64, 128, 255);
|
||||
spore3 = color(255, 128, 172);
|
||||
spore4 = color(255, 64, 128);
|
||||
numcells = 0;
|
||||
seed();
|
||||
}
|
||||
|
||||
public void seed()
|
||||
{
|
||||
// Add cells at random places
|
||||
for (int i = 0; i < maxcells; i++)
|
||||
{
|
||||
int cX = PApplet.parseInt(random(width));
|
||||
int cY = PApplet.parseInt(random(height));
|
||||
int c;
|
||||
float r = random(1);
|
||||
if (r < 0.25f) c = spore1;
|
||||
else if (r < 0.5f) c = spore2;
|
||||
else if (r < 0.75f) c = spore3;
|
||||
else c = spore4;
|
||||
if (w.getpix(cX, cY) == black)
|
||||
{
|
||||
w.setpix(cX, cY, c);
|
||||
cells[numcells] = new Cell(cX, cY);
|
||||
numcells++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void draw()
|
||||
{
|
||||
// Run cells in random order
|
||||
for (int i = 0; i < runs_per_loop; i++) {
|
||||
int selected = min((int)random(numcells), numcells - 1);
|
||||
cells[selected].run();
|
||||
}
|
||||
|
||||
println(frameRate);
|
||||
}
|
||||
|
||||
public void clearscr()
|
||||
{
|
||||
for (int y = 0; y < height; y++) {
|
||||
for (int x = 0; x < width; x++) {
|
||||
set(x, y, color(0));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class Cell
|
||||
{
|
||||
int x, y;
|
||||
Cell(int xin, int yin)
|
||||
{
|
||||
x = xin;
|
||||
y = yin;
|
||||
}
|
||||
// Perform action based on surroundings
|
||||
public void run()
|
||||
{
|
||||
// Fix cell coordinates
|
||||
while(x < 0) {
|
||||
x+=width;
|
||||
}
|
||||
while(x > width - 1) {
|
||||
x-=width;
|
||||
}
|
||||
while(y < 0) {
|
||||
y+=height;
|
||||
}
|
||||
while(y > height - 1) {
|
||||
y-=height;
|
||||
}
|
||||
|
||||
// Cell instructions
|
||||
int myColor = w.getpix(x, y);
|
||||
if (myColor == spore1) {
|
||||
if (w.getpix(x - 1, y + 1) == black && w.getpix(x + 1, y + 1) == black && w.getpix(x, y + 1) == black) move(0, 1);
|
||||
else if (w.getpix(x - 1, y) == spore2 && w.getpix(x - 1, y - 1) != black) move(0, -1);
|
||||
else if (w.getpix(x - 1, y) == spore2 && w.getpix(x - 1, y - 1) == black) move(-1, -1);
|
||||
else if (w.getpix(x + 1, y) == spore1 && w.getpix(x + 1, y - 1) != black) move(0, -1);
|
||||
else if (w.getpix(x + 1, y) == spore1 && w.getpix(x + 1, y - 1) == black) move(1, -1);
|
||||
else move((int)random(3) - 1, 0);
|
||||
} else if (myColor == spore2) {
|
||||
if (w.getpix(x - 1, y + 1) == black && w.getpix(x + 1, y + 1) == black && w.getpix(x, y + 1) == black) move(0, 1);
|
||||
else if (w.getpix(x + 1, y) == spore1 && w.getpix(x + 1, y - 1) != black) move(0, -1);
|
||||
else if (w.getpix(x + 1, y) == spore1 && w.getpix(x + 1, y - 1) == black) move(1, -1);
|
||||
else if (w.getpix(x - 1, y) == spore2 && w.getpix(x - 1, y - 1) != black) move(0, -1);
|
||||
else if (w.getpix(x - 1, y) == spore2 && w.getpix(x - 1, y - 1) == black) move(-1, -1);
|
||||
else move((int)random(3) - 1, 0);
|
||||
}
|
||||
else if (myColor == spore3)
|
||||
{
|
||||
if (w.getpix(x - 1, y - 1) == black && w.getpix(x + 1, y - 1) == black && w.getpix(x, y - 1) == black) move(0, -1);
|
||||
else if (w.getpix(x - 1, y) == spore4 && w.getpix(x - 1, y + 1) != black) move(0, 1);
|
||||
else if (w.getpix(x - 1, y) == spore4 && w.getpix(x - 1, y + 1) == black) move(-1, 1);
|
||||
else if (w.getpix(x + 1, y) == spore3 && w.getpix(x + 1, y + 1) != black) move(0, 1);
|
||||
else if (w.getpix(x + 1, y) == spore3 && w.getpix(x + 1, y + 1) == black) move(1, 1);
|
||||
else move((int)random(3) - 1, 0);
|
||||
}
|
||||
else if (myColor == spore4)
|
||||
{
|
||||
if (w.getpix(x - 1, y - 1) == black && w.getpix(x + 1, y - 1) == black && w.getpix(x, y - 1) == black) move(0, -1);
|
||||
else if (w.getpix(x + 1, y) == spore3 && w.getpix(x + 1, y + 1) != black) move(0, 1);
|
||||
else if (w.getpix(x + 1, y) == spore3 && w.getpix(x + 1, y + 1) == black) move(1, 1);
|
||||
else if (w.getpix(x - 1, y) == spore4 && w.getpix(x - 1, y + 1) != black) move(0, 1);
|
||||
else if (w.getpix(x - 1, y) == spore4 && w.getpix(x - 1, y + 1) == black) move(-1, 1);
|
||||
else move((int)random(3) - 1, 0);
|
||||
}
|
||||
}
|
||||
|
||||
// Will move the cell (dx, dy) units if that space is empty
|
||||
public void move(int dx, int dy) {
|
||||
if (w.getpix(x + dx, y + dy) == black) {
|
||||
w.setpix(x + dx, y + dy, w.getpix(x, y));
|
||||
w.setpix(x, y, color(0));
|
||||
x += dx;
|
||||
y += dy;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// The World class simply provides two functions, get and set, which access the
|
||||
// display in the same way as getPixel and setPixel. The only difference is that
|
||||
// the World class's get and set do screen wraparound ("toroidal coordinates").
|
||||
class World
|
||||
{
|
||||
public void setpix(int x, int y, int c) {
|
||||
while(x < 0) x+=width;
|
||||
while(x > width - 1) x-=width;
|
||||
while(y < 0) y+=height;
|
||||
while(y > height - 1) y-=height;
|
||||
set(x, y, c);
|
||||
}
|
||||
|
||||
public int getpix(int x, int y) {
|
||||
while(x < 0) x+=width;
|
||||
while(x > width - 1) x-=width;
|
||||
while(y < 0) y+=height;
|
||||
while(y > height - 1) y-=height;
|
||||
return get(x, y);
|
||||
}
|
||||
}
|
||||
|
||||
public void mousePressed()
|
||||
{
|
||||
setup();
|
||||
}
|
||||
|
||||
|
||||
static public void main(String args[]) {
|
||||
PApplet.main(new String[] { "Spore2" });
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,176 @@
|
||||
/**
|
||||
* Spore 2
|
||||
* by Mike Davis.
|
||||
*
|
||||
* A short program for alife experiments. Click in the window to restart.
|
||||
* Each cell is represented by a pixel on the display as well as an entry in
|
||||
* the array 'cells'. Each cell has a run() method, which performs actions
|
||||
* based on the cell's surroundings. Cells run one at a time (to avoid conflicts
|
||||
* like wanting to move to the same space) and in random order.
|
||||
*/
|
||||
|
||||
World w;
|
||||
int maxcells = 8000;
|
||||
int numcells;
|
||||
Cell[] cells = new Cell[maxcells];
|
||||
color spore1, spore2, spore3, spore4;
|
||||
color black = color(0, 0, 0);
|
||||
// set lower for smoother animation, higher for faster simulation
|
||||
int runs_per_loop = 10000;
|
||||
|
||||
void setup()
|
||||
{
|
||||
size(640, 200, P2D);
|
||||
frameRate(24);
|
||||
clearscr();
|
||||
w = new World();
|
||||
spore1 = color(128, 172, 255);
|
||||
spore2 = color(64, 128, 255);
|
||||
spore3 = color(255, 128, 172);
|
||||
spore4 = color(255, 64, 128);
|
||||
numcells = 0;
|
||||
seed();
|
||||
}
|
||||
|
||||
void seed()
|
||||
{
|
||||
// Add cells at random places
|
||||
for (int i = 0; i < maxcells; i++)
|
||||
{
|
||||
int cX = int(random(width));
|
||||
int cY = int(random(height));
|
||||
int c;
|
||||
float r = random(1);
|
||||
if (r < 0.25) c = spore1;
|
||||
else if (r < 0.5) c = spore2;
|
||||
else if (r < 0.75) c = spore3;
|
||||
else c = spore4;
|
||||
if (w.getpix(cX, cY) == black)
|
||||
{
|
||||
w.setpix(cX, cY, c);
|
||||
cells[numcells] = new Cell(cX, cY);
|
||||
numcells++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void draw()
|
||||
{
|
||||
// Run cells in random order
|
||||
for (int i = 0; i < runs_per_loop; i++) {
|
||||
int selected = min((int)random(numcells), numcells - 1);
|
||||
cells[selected].run();
|
||||
}
|
||||
|
||||
println(frameRate);
|
||||
}
|
||||
|
||||
void clearscr()
|
||||
{
|
||||
for (int y = 0; y < height; y++) {
|
||||
for (int x = 0; x < width; x++) {
|
||||
set(x, y, color(0));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class Cell
|
||||
{
|
||||
int x, y;
|
||||
Cell(int xin, int yin)
|
||||
{
|
||||
x = xin;
|
||||
y = yin;
|
||||
}
|
||||
// Perform action based on surroundings
|
||||
void run()
|
||||
{
|
||||
// Fix cell coordinates
|
||||
while(x < 0) {
|
||||
x+=width;
|
||||
}
|
||||
while(x > width - 1) {
|
||||
x-=width;
|
||||
}
|
||||
while(y < 0) {
|
||||
y+=height;
|
||||
}
|
||||
while(y > height - 1) {
|
||||
y-=height;
|
||||
}
|
||||
|
||||
// Cell instructions
|
||||
int myColor = w.getpix(x, y);
|
||||
if (myColor == spore1) {
|
||||
if (w.getpix(x - 1, y + 1) == black && w.getpix(x + 1, y + 1) == black && w.getpix(x, y + 1) == black) move(0, 1);
|
||||
else if (w.getpix(x - 1, y) == spore2 && w.getpix(x - 1, y - 1) != black) move(0, -1);
|
||||
else if (w.getpix(x - 1, y) == spore2 && w.getpix(x - 1, y - 1) == black) move(-1, -1);
|
||||
else if (w.getpix(x + 1, y) == spore1 && w.getpix(x + 1, y - 1) != black) move(0, -1);
|
||||
else if (w.getpix(x + 1, y) == spore1 && w.getpix(x + 1, y - 1) == black) move(1, -1);
|
||||
else move((int)random(3) - 1, 0);
|
||||
} else if (myColor == spore2) {
|
||||
if (w.getpix(x - 1, y + 1) == black && w.getpix(x + 1, y + 1) == black && w.getpix(x, y + 1) == black) move(0, 1);
|
||||
else if (w.getpix(x + 1, y) == spore1 && w.getpix(x + 1, y - 1) != black) move(0, -1);
|
||||
else if (w.getpix(x + 1, y) == spore1 && w.getpix(x + 1, y - 1) == black) move(1, -1);
|
||||
else if (w.getpix(x - 1, y) == spore2 && w.getpix(x - 1, y - 1) != black) move(0, -1);
|
||||
else if (w.getpix(x - 1, y) == spore2 && w.getpix(x - 1, y - 1) == black) move(-1, -1);
|
||||
else move((int)random(3) - 1, 0);
|
||||
}
|
||||
else if (myColor == spore3)
|
||||
{
|
||||
if (w.getpix(x - 1, y - 1) == black && w.getpix(x + 1, y - 1) == black && w.getpix(x, y - 1) == black) move(0, -1);
|
||||
else if (w.getpix(x - 1, y) == spore4 && w.getpix(x - 1, y + 1) != black) move(0, 1);
|
||||
else if (w.getpix(x - 1, y) == spore4 && w.getpix(x - 1, y + 1) == black) move(-1, 1);
|
||||
else if (w.getpix(x + 1, y) == spore3 && w.getpix(x + 1, y + 1) != black) move(0, 1);
|
||||
else if (w.getpix(x + 1, y) == spore3 && w.getpix(x + 1, y + 1) == black) move(1, 1);
|
||||
else move((int)random(3) - 1, 0);
|
||||
}
|
||||
else if (myColor == spore4)
|
||||
{
|
||||
if (w.getpix(x - 1, y - 1) == black && w.getpix(x + 1, y - 1) == black && w.getpix(x, y - 1) == black) move(0, -1);
|
||||
else if (w.getpix(x + 1, y) == spore3 && w.getpix(x + 1, y + 1) != black) move(0, 1);
|
||||
else if (w.getpix(x + 1, y) == spore3 && w.getpix(x + 1, y + 1) == black) move(1, 1);
|
||||
else if (w.getpix(x - 1, y) == spore4 && w.getpix(x - 1, y + 1) != black) move(0, 1);
|
||||
else if (w.getpix(x - 1, y) == spore4 && w.getpix(x - 1, y + 1) == black) move(-1, 1);
|
||||
else move((int)random(3) - 1, 0);
|
||||
}
|
||||
}
|
||||
|
||||
// Will move the cell (dx, dy) units if that space is empty
|
||||
void move(int dx, int dy) {
|
||||
if (w.getpix(x + dx, y + dy) == black) {
|
||||
w.setpix(x + dx, y + dy, w.getpix(x, y));
|
||||
w.setpix(x, y, color(0));
|
||||
x += dx;
|
||||
y += dy;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// The World class simply provides two functions, get and set, which access the
|
||||
// display in the same way as getPixel and setPixel. The only difference is that
|
||||
// the World class's get and set do screen wraparound ("toroidal coordinates").
|
||||
class World
|
||||
{
|
||||
void setpix(int x, int y, int c) {
|
||||
while(x < 0) x+=width;
|
||||
while(x > width - 1) x-=width;
|
||||
while(y < 0) y+=height;
|
||||
while(y > height - 1) y-=height;
|
||||
set(x, y, c);
|
||||
}
|
||||
|
||||
color getpix(int x, int y) {
|
||||
while(x < 0) x+=width;
|
||||
while(x > width - 1) x-=width;
|
||||
while(y < 0) y+=height;
|
||||
while(y > height - 1) y-=height;
|
||||
return get(x, y);
|
||||
}
|
||||
}
|
||||
|
||||
void mousePressed()
|
||||
{
|
||||
setup();
|
||||
}
|
||||
|
||||
Binary file not shown.
|
After Width: | Height: | Size: 2.2 KiB |
@@ -0,0 +1,94 @@
|
||||
class CA {
|
||||
|
||||
int[] cells; // An array of 0s and 1s
|
||||
int generation; // How many generations?
|
||||
int scl; // How many pixels wide/high is each cell?
|
||||
|
||||
int[] rules; // An array to store the ruleset, for example {0,1,1,0,1,1,0,1}
|
||||
|
||||
CA(int[] r) {
|
||||
rules = r;
|
||||
scl = 1;
|
||||
cells = new int[width/scl];
|
||||
restart();
|
||||
}
|
||||
|
||||
CA() {
|
||||
scl = 1;
|
||||
cells = new int[width/scl];
|
||||
randomize();
|
||||
restart();
|
||||
}
|
||||
|
||||
// Set the rules of the CA
|
||||
void setRules(int[] r) {
|
||||
rules = r;
|
||||
}
|
||||
|
||||
// Make a random ruleset
|
||||
void randomize() {
|
||||
for (int i = 0; i < 8; i++) {
|
||||
rules[i] = int(random(2));
|
||||
}
|
||||
}
|
||||
|
||||
// Reset to generation 0
|
||||
void restart() {
|
||||
for (int i = 0; i < cells.length; i++) {
|
||||
cells[i] = 0;
|
||||
}
|
||||
cells[cells.length/2] = 1; // We arbitrarily start with just the middle cell having a state of "1"
|
||||
generation = 0;
|
||||
}
|
||||
|
||||
// The process of creating the new generation
|
||||
void generate() {
|
||||
// First we create an empty array for the new values
|
||||
int[] nextgen = new int[cells.length];
|
||||
// For every spot, determine new state by examing current state, and neighbor states
|
||||
// Ignore edges that only have one neighor
|
||||
for (int i = 1; i < cells.length-1; i++) {
|
||||
int left = cells[i-1]; // Left neighbor state
|
||||
int me = cells[i]; // Current state
|
||||
int right = cells[i+1]; // Right neighbor state
|
||||
nextgen[i] = rules(left,me,right); // Compute next generation state based on ruleset
|
||||
}
|
||||
// Copy the array into current value
|
||||
cells = (int[]) nextgen.clone();
|
||||
generation++;
|
||||
}
|
||||
|
||||
// This is the easy part, just draw the cells, fill 255 for '1', fill 0 for '0'
|
||||
void render() {
|
||||
for (int i = 0; i < cells.length; i++) {
|
||||
if (cells[i] == 1) fill(255);
|
||||
else fill(0);
|
||||
noStroke();
|
||||
rect(i*scl,generation*scl, scl,scl);
|
||||
}
|
||||
}
|
||||
|
||||
// Implementing the Wolfram rules
|
||||
// Could be improved and made more concise, but here we can explicitly see what is going on for each case
|
||||
int rules (int a, int b, int c) {
|
||||
if (a == 1 && b == 1 && c == 1) return rules[0];
|
||||
if (a == 1 && b == 1 && c == 0) return rules[1];
|
||||
if (a == 1 && b == 0 && c == 1) return rules[2];
|
||||
if (a == 1 && b == 0 && c == 0) return rules[3];
|
||||
if (a == 0 && b == 1 && c == 1) return rules[4];
|
||||
if (a == 0 && b == 1 && c == 0) return rules[5];
|
||||
if (a == 0 && b == 0 && c == 1) return rules[6];
|
||||
if (a == 0 && b == 0 && c == 0) return rules[7];
|
||||
return 0;
|
||||
}
|
||||
|
||||
// The CA is done if it reaches the bottom of the screen
|
||||
boolean finished() {
|
||||
if (generation > height/scl) {
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,38 @@
|
||||
/**
|
||||
* Wolfram Cellular Automata
|
||||
* by Daniel Shiffman.
|
||||
*
|
||||
* Simple demonstration of a Wolfram 1-dimensional cellular automata
|
||||
* When the system reaches bottom of the window, it restarts with a new ruleset
|
||||
* Mouse click restarts as well.
|
||||
*/
|
||||
|
||||
CA ca; // An instance object to describe the Wolfram basic Cellular Automata
|
||||
|
||||
void setup() {
|
||||
size(640, 360, P2D);
|
||||
frameRate(30);
|
||||
background(0);
|
||||
int[] ruleset = {0,1,0,1,1,0,1,0}; // An initial rule system
|
||||
ca = new CA(ruleset); // Initialize CA
|
||||
}
|
||||
|
||||
void draw() {
|
||||
ca.render(); // Draw the CA
|
||||
ca.generate(); // Generate the next level
|
||||
|
||||
if (ca.finished()) { // If we're done, clear the screen, pick a new ruleset and restart
|
||||
background(0);
|
||||
ca.randomize();
|
||||
ca.restart();
|
||||
}
|
||||
}
|
||||
|
||||
void mousePressed() {
|
||||
background(0);
|
||||
ca.randomize();
|
||||
ca.restart();
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,94 @@
|
||||
class CA {
|
||||
|
||||
int[] cells; // An array of 0s and 1s
|
||||
int generation; // How many generations?
|
||||
int scl; // How many pixels wide/high is each cell?
|
||||
|
||||
int[] rules; // An array to store the ruleset, for example {0,1,1,0,1,1,0,1}
|
||||
|
||||
CA(int[] r) {
|
||||
rules = r;
|
||||
scl = 1;
|
||||
cells = new int[width/scl];
|
||||
restart();
|
||||
}
|
||||
|
||||
CA() {
|
||||
scl = 1;
|
||||
cells = new int[width/scl];
|
||||
randomize();
|
||||
restart();
|
||||
}
|
||||
|
||||
// Set the rules of the CA
|
||||
void setRules(int[] r) {
|
||||
rules = r;
|
||||
}
|
||||
|
||||
// Make a random ruleset
|
||||
void randomize() {
|
||||
for (int i = 0; i < 8; i++) {
|
||||
rules[i] = int(random(2));
|
||||
}
|
||||
}
|
||||
|
||||
// Reset to generation 0
|
||||
void restart() {
|
||||
for (int i = 0; i < cells.length; i++) {
|
||||
cells[i] = 0;
|
||||
}
|
||||
cells[cells.length/2] = 1; // We arbitrarily start with just the middle cell having a state of "1"
|
||||
generation = 0;
|
||||
}
|
||||
|
||||
// The process of creating the new generation
|
||||
void generate() {
|
||||
// First we create an empty array for the new values
|
||||
int[] nextgen = new int[cells.length];
|
||||
// For every spot, determine new state by examing current state, and neighbor states
|
||||
// Ignore edges that only have one neighor
|
||||
for (int i = 1; i < cells.length-1; i++) {
|
||||
int left = cells[i-1]; // Left neighbor state
|
||||
int me = cells[i]; // Current state
|
||||
int right = cells[i+1]; // Right neighbor state
|
||||
nextgen[i] = rules(left,me,right); // Compute next generation state based on ruleset
|
||||
}
|
||||
// Copy the array into current value
|
||||
cells = (int[]) nextgen.clone();
|
||||
generation++;
|
||||
}
|
||||
|
||||
// This is the easy part, just draw the cells, fill 255 for '1', fill 0 for '0'
|
||||
void render() {
|
||||
for (int i = 0; i < cells.length; i++) {
|
||||
if (cells[i] == 1) fill(255);
|
||||
else fill(0);
|
||||
noStroke();
|
||||
rect(i*scl,generation*scl, scl,scl);
|
||||
}
|
||||
}
|
||||
|
||||
// Implementing the Wolfram rules
|
||||
// Could be improved and made more concise, but here we can explicitly see what is going on for each case
|
||||
int rules (int a, int b, int c) {
|
||||
if (a == 1 && b == 1 && c == 1) return rules[0];
|
||||
if (a == 1 && b == 1 && c == 0) return rules[1];
|
||||
if (a == 1 && b == 0 && c == 1) return rules[2];
|
||||
if (a == 1 && b == 0 && c == 0) return rules[3];
|
||||
if (a == 0 && b == 1 && c == 1) return rules[4];
|
||||
if (a == 0 && b == 1 && c == 0) return rules[5];
|
||||
if (a == 0 && b == 0 && c == 1) return rules[6];
|
||||
if (a == 0 && b == 0 && c == 0) return rules[7];
|
||||
return 0;
|
||||
}
|
||||
|
||||
// The CA is done if it reaches the bottom of the screen
|
||||
boolean finished() {
|
||||
if (generation > height/scl) {
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,152 @@
|
||||
import processing.core.*;
|
||||
|
||||
import java.applet.*;
|
||||
import java.awt.*;
|
||||
import java.awt.image.*;
|
||||
import java.awt.event.*;
|
||||
import java.io.*;
|
||||
import java.net.*;
|
||||
import java.text.*;
|
||||
import java.util.*;
|
||||
import java.util.zip.*;
|
||||
import java.util.regex.*;
|
||||
|
||||
public class Wolfram extends PApplet {
|
||||
|
||||
/**
|
||||
* Wolfram Cellular Automata
|
||||
* by Daniel Shiffman.
|
||||
*
|
||||
* Simple demonstration of a Wolfram 1-dimensional cellular automata
|
||||
* When the system reaches bottom of the window, it restarts with a new ruleset
|
||||
* Mouse click restarts as well.
|
||||
*/
|
||||
|
||||
CA ca; // An instance object to describe the Wolfram basic Cellular Automata
|
||||
|
||||
public void setup() {
|
||||
size(640, 360, P2D);
|
||||
frameRate(30);
|
||||
background(0);
|
||||
int[] ruleset = {0,1,0,1,1,0,1,0}; // An initial rule system
|
||||
ca = new CA(ruleset); // Initialize CA
|
||||
}
|
||||
|
||||
public void draw() {
|
||||
ca.render(); // Draw the CA
|
||||
ca.generate(); // Generate the next level
|
||||
|
||||
if (ca.finished()) { // If we're done, clear the screen, pick a new ruleset and restart
|
||||
background(0);
|
||||
ca.randomize();
|
||||
ca.restart();
|
||||
}
|
||||
}
|
||||
|
||||
public void mousePressed() {
|
||||
background(0);
|
||||
ca.randomize();
|
||||
ca.restart();
|
||||
}
|
||||
|
||||
|
||||
|
||||
class CA {
|
||||
|
||||
int[] cells; // An array of 0s and 1s
|
||||
int generation; // How many generations?
|
||||
int scl; // How many pixels wide/high is each cell?
|
||||
|
||||
int[] rules; // An array to store the ruleset, for example {0,1,1,0,1,1,0,1}
|
||||
|
||||
CA(int[] r) {
|
||||
rules = r;
|
||||
scl = 1;
|
||||
cells = new int[width/scl];
|
||||
restart();
|
||||
}
|
||||
|
||||
CA() {
|
||||
scl = 1;
|
||||
cells = new int[width/scl];
|
||||
randomize();
|
||||
restart();
|
||||
}
|
||||
|
||||
// Set the rules of the CA
|
||||
public void setRules(int[] r) {
|
||||
rules = r;
|
||||
}
|
||||
|
||||
// Make a random ruleset
|
||||
public void randomize() {
|
||||
for (int i = 0; i < 8; i++) {
|
||||
rules[i] = PApplet.parseInt(random(2));
|
||||
}
|
||||
}
|
||||
|
||||
// Reset to generation 0
|
||||
public void restart() {
|
||||
for (int i = 0; i < cells.length; i++) {
|
||||
cells[i] = 0;
|
||||
}
|
||||
cells[cells.length/2] = 1; // We arbitrarily start with just the middle cell having a state of "1"
|
||||
generation = 0;
|
||||
}
|
||||
|
||||
// The process of creating the new generation
|
||||
public void generate() {
|
||||
// First we create an empty array for the new values
|
||||
int[] nextgen = new int[cells.length];
|
||||
// For every spot, determine new state by examing current state, and neighbor states
|
||||
// Ignore edges that only have one neighor
|
||||
for (int i = 1; i < cells.length-1; i++) {
|
||||
int left = cells[i-1]; // Left neighbor state
|
||||
int me = cells[i]; // Current state
|
||||
int right = cells[i+1]; // Right neighbor state
|
||||
nextgen[i] = rules(left,me,right); // Compute next generation state based on ruleset
|
||||
}
|
||||
// Copy the array into current value
|
||||
cells = (int[]) nextgen.clone();
|
||||
generation++;
|
||||
}
|
||||
|
||||
// This is the easy part, just draw the cells, fill 255 for '1', fill 0 for '0'
|
||||
public void render() {
|
||||
for (int i = 0; i < cells.length; i++) {
|
||||
if (cells[i] == 1) fill(255);
|
||||
else fill(0);
|
||||
noStroke();
|
||||
rect(i*scl,generation*scl, scl,scl);
|
||||
}
|
||||
}
|
||||
|
||||
// Implementing the Wolfram rules
|
||||
// Could be improved and made more concise, but here we can explicitly see what is going on for each case
|
||||
public int rules (int a, int b, int c) {
|
||||
if (a == 1 && b == 1 && c == 1) return rules[0];
|
||||
if (a == 1 && b == 1 && c == 0) return rules[1];
|
||||
if (a == 1 && b == 0 && c == 1) return rules[2];
|
||||
if (a == 1 && b == 0 && c == 0) return rules[3];
|
||||
if (a == 0 && b == 1 && c == 1) return rules[4];
|
||||
if (a == 0 && b == 1 && c == 0) return rules[5];
|
||||
if (a == 0 && b == 0 && c == 1) return rules[6];
|
||||
if (a == 0 && b == 0 && c == 0) return rules[7];
|
||||
return 0;
|
||||
}
|
||||
|
||||
// The CA is done if it reaches the bottom of the screen
|
||||
public boolean finished() {
|
||||
if (generation > height/scl) {
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static public void main(String args[]) {
|
||||
PApplet.main(new String[] { "Wolfram" });
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
/**
|
||||
* Wolfram Cellular Automata
|
||||
* by Daniel Shiffman.
|
||||
*
|
||||
* Simple demonstration of a Wolfram 1-dimensional cellular automata
|
||||
* When the system reaches bottom of the window, it restarts with a new ruleset
|
||||
* Mouse click restarts as well.
|
||||
*/
|
||||
|
||||
CA ca; // An instance object to describe the Wolfram basic Cellular Automata
|
||||
|
||||
void setup() {
|
||||
size(640, 360, P2D);
|
||||
frameRate(30);
|
||||
background(0);
|
||||
int[] ruleset = {0,1,0,1,1,0,1,0}; // An initial rule system
|
||||
ca = new CA(ruleset); // Initialize CA
|
||||
}
|
||||
|
||||
void draw() {
|
||||
ca.render(); // Draw the CA
|
||||
ca.generate(); // Generate the next level
|
||||
|
||||
if (ca.finished()) { // If we're done, clear the screen, pick a new ruleset and restart
|
||||
background(0);
|
||||
ca.randomize();
|
||||
ca.restart();
|
||||
}
|
||||
}
|
||||
|
||||
void mousePressed() {
|
||||
background(0);
|
||||
ca.randomize();
|
||||
ca.restart();
|
||||
}
|
||||
|
||||
|
||||
|
||||
Binary file not shown.
|
After Width: | Height: | Size: 2.2 KiB |
Reference in New Issue
Block a user