14=ca+pascal

67
Processing Processing 1 4 Cellular Automata Cellular Automata 細細細細細 細細細細細 Pascal Triangle Pascal Triangle 細細細細細細 細細細細細細

Upload: gene-kao

Post on 19-Apr-2017

213 views

Category:

Documents


1 download

TRANSCRIPT

ProcessingProcessing

14

Cellular AutomataCellular Automata 細胞自動機細胞自動機Pascal TrianglePascal Triangle 巴斯卡三角形巴斯卡三角形

Cellular AutomataCellular Automata

19501950 Stanislaw M. Ulam Stanislaw M. Ulam John von NeumannJohn von Neumann Cellular AutomataCellular Automata

19701970 John Horton ConwayJohn Horton Conway The Game of LifeThe Game of Life19721972 Martin GardenerMartin Gardener Scientific AmericanScientific American 19801980 Stephen WolframStephen Wolfram 1D CA1D CA

MathematicaMathematica

Simulation 模擬Bottom-Up 由下而上Self-Similarity 自我相似性New Computer 新一代的電腦

一維二維 The Game of Life三維

C

N

S

EW

i-2 i-1 i i+1 i+2 1D

2D

ifif

thenthen

ONON

OFFOFF

7 = 4 + 2 + 1

6 = 4 + 2 + 0

5 = 4 + 0 + 1

4 = 4 + 0 + 0

3 = 0 + 2 + 1

2 = 0 + 2 + 0

1 = 0 + 0 + 1

0 = 0 + 0 + 0

202122

Rule 30 = 16 + 8 + 4 + 2 = 30

1248163264128

Rule 165 = 128 + 32 + 4 + 1 = 165

1248163264128

01111000

10100101

0248163264128

Rule 30

Rule 30

0248163264128

Rule 30

Rule 165

Rule 165

int canvas_size = 820;int w = canvas_size;int h = canvas_size;int cell_size = 2;

int num_cells = w / cell_size;int[] cells_prev = new int[num_cells];int[] cells_new = new int[num_cells];

int[] rules = new int[8];int rule_num = 30;int gen = 1;int current_row = 0;

1/41/4

void setup() { size(w, h); frameRate(30); background(0); initialization(); cells_prev[num_cells / 2] = 1; set_rule(rule_num);} void draw() { draw_automata(); for (int i = 1; i < num_cells-1; i++) { int left = cells_prev[i-1]; int me = cells_prev[i]; int right = cells_prev[i+1]; if (rules(left, me, right) == 1) {cells_new[i] = 1;} else {cells_new[i] = 0;} } re_initialization(); gen++; if (gen > num_cells) {noLoop();}} 2/42/4

void initialization () { for (int i = 0; i < num_cells; i ++) { cells_prev[i] = 0; cells_new [i] = 0; } }

void re_initialization () { for (int i = 0; i < num_cells; i ++) { cells_prev[i] = cells_new [i]; } }

void set_rule (int n) { for (int i = 7; i >=0; i --) { rules[i] = n % 2; n = n /2; } }

3/43/4

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;}

void draw_automata () { for (int i = 0; i < num_cells; i ++) { if (cells_prev[i] == 1) { rect(i*cell_size,current_row*cell_size,cell_size,cell_size); } } current_row ++;}

4/44/4

int canvas_size = 820;int w = canvas_size;int h = canvas_size;int cell_size = 2;

int num_cells = w / cell_size;int[] cells_prev = new int[num_cells];int[] cells_new = new int[num_cells];

int[] rules = new int[8];int rule_num = 30;int gen = 1;int current_row = 0; void setup() { size(w, h); frameRate(30); background(0); initialization(); cells_prev[num_cells / 2] = 1; set_rule(rule_num);} void draw() { draw_automata(); for (int i = 1; i < num_cells-1; i++) { int left = cells_prev[i-1]; int me = cells_prev[i]; int right = cells_prev[i+1]; if (rules(left, me, right) == 1) {cells_new[i] = 1;} else {cells_new[i] = 0;} } re_initialization(); gen++; if (gen > num_cells) {noLoop();}}

void initialization () { for (int i = 0; i < num_cells; i ++) { cells_prev[i] = 0; cells_new [i] = 0; } }

void re_initialization () { for (int i = 0; i < num_cells; i ++) { cells_prev[i] = cells_new [i]; } }

void set_rule (int n) { for (int i = 7; i >=0; i --) { rules[i] = n % 2; n = n /2; } }

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;}

void draw_automata () { for (int i = 0; i < num_cells; i ++) { if (cells_prev[i] == 1) { rect(i*cell_size,current_row*cell_size,cell_size,cell_size); } } current_row ++;}

1/4 – 4/41/4 – 4/4

Ex. 49-01, P.462 from textbook.

int[] rules = { 0, 0, 0, 1, 1, 1, 1, 0 };int gen = 1; // Generationcolor on = color(255);color off = color(0); void setup() {size(101, 101);frameRate(8); // Slow down to 8 frames each secondbackground(0);set(width/2, 0, on); // Set the top middle pixel to white}

1/31/3

void draw() {// For each pixel, determine new state by examining current// state and neighbor states and ignore edges that have only// one neighborfor (int i = 1; i < width-1; i++) {int left = get(i-1, gen-1); // Left neighborint me = get(i, gen-1); // Current pixelint right = get(i+1, gen-1); // Right neighborif (rules(left, me, right) == 1) {set(i, gen, on);}}gen++; // Increment the generation by 1if (gen > height-1) { // If reached the bottom of the screen,noLoop(); // stop the program}} 2/32/3

// Implement the rulesint rules(color a, color b, color c) {if ((a == on ) && (b == on ) && (c == on )) { return rules[0]; }if ((a == on ) && (b == on ) && (c == off)) { return rules[1]; }if ((a == on ) && (b == off) && (c == on )) { return rules[2]; }if ((a == on ) && (b == off) && (c == off)) { return rules[3]; }if ((a == off) && (b == on ) && (c == on )) { return rules[4]; }if ((a == off) && (b == on ) && (c == off)) { return rules[5]; }if ((a == off) && (b == off) && (c == on )) { return rules[6]; }if ((a == off) && (b == off) && (c == off)) { return rules[7]; }return 0;}

3/33/3

int[] rules = { 0, 0, 0, 1, 1, 1, 1, 0 };int gen = 1; // Generationcolor on = color(255);color off = color(0); void setup() {size(101, 101);frameRate(8); // Slow down to 8 frames each secondbackground(0);set(width/2, 0, on); // Set the top middle pixel to white} void draw() {// For each pixel, determine new state by examining current// state and neighbor states and ignore edges that have only// one neighborfor (int i = 1; i < width-1; i++) {int left = get(i-1, gen-1); // Left neighborint me = get(i, gen-1); // Current pixelint right = get(i+1, gen-1); // Right neighborif (rules(left, me, right) == 1) {set(i, gen, on);}}gen++; // Increment the generation by 1if (gen > height-1) { // If reached the bottom of the screen,noLoop(); // stop the program}}// Implement the rulesint rules(color a, color b, color c) {if ((a == on ) && (b == on ) && (c == on )) { return rules[0]; }if ((a == on ) && (b == on ) && (c == off)) { return rules[1]; }if ((a == on ) && (b == off) && (c == on )) { return rules[2]; }if ((a == on ) && (b == off) && (c == off)) { return rules[3]; }if ((a == off) && (b == on ) && (c == on )) { return rules[4]; }if ((a == off) && (b == on ) && (c == off)) { return rules[5]; }if ((a == off) && (b == off) && (c == on )) { return rules[6]; }if ((a == off) && (b == off) && (c == off)) { return rules[7]; }return 0;} 1/3 – 3/31/3 – 3/3

set ()

get ()

Rhino VBScript

Rule 30Rule 30

Rule 41Rule 41

Rule 64Rule 64

Rule 73Rule 73

細胞空間細胞空間 格狀、蜂巢格狀、蜂巢細胞狀態細胞狀態 ONON 、、 OFFOFF鄰近關係鄰近關係 22 、、 44 、、 88轉變規則轉變規則 個數個數

1. Grey Level2. 5 Elements3. More Initial Points4. More Than 2D

• 對於存活的細胞(塗色的方格):對於存活的細胞(塗色的方格): – 當八個鄰近細胞中,只有零個或一個是活細胞時, 則該細胞會因孤獨而死亡– 當八個鄰近細胞中,恰有二或三個是活細胞時,則該細胞繼續存活– 當八個鄰近細胞中,有四個或超過四個是活細胞時,則該細胞會因擁擠而死亡

• 對於死亡的細胞(未塗色的空方格):對於死亡的細胞(未塗色的空方格):當八個鄰近細胞中,恰有三個是活細胞時,則該處誕生一個活細胞

The Game of LifeThe Game of Life

int[][] grid, futureGrid;void setup() {size(540, 100);frameRate(8);grid = new int[width][height];futureGrid = new int[width][height];float density = 0.3 * width * height;for (int i = 0; i < density; i++) {grid[int(random(width))][int(random(height))] = 1;}background(0);}

void draw() {for (int x = 1; x < width-1; x++) {for (int y = 1; y < height-1; y++) {// Check the number of neighbors (adjacent cells)int nb = neighbors(x, y);if ((grid[x][y] == 1) && (nb < 2)) {futureGrid[x][y] = 0; // Isolation deathset(x, y, color(0));} else if ((grid[x][y] == 1) && (nb > 3)) {futureGrid[x][y] = 0; // Overpopulation deathset(x, y, color(0));} else if ((grid[x][y] == 0) && (nb == 3)) {futureGrid[x][y] = 1; // Birthset(x, y, color(255));} else {futureGrid[x][y] = grid[x][y]; // Survive}}}// Swap current and future gridsint[][] temp = grid;grid = futureGrid;futureGrid = temp;}

// Count the number of adjacent cells 'on'int neighbors(int x, int y) {return grid[x][y-1] + // Northgrid[x+1][y-1] + // Northeastgrid[x+1][y] + // Eastgrid[x+1][y+1] + // Souteastgrid[x][y+1] + // Southgrid[x-1][y+1] + // Southwestgrid[x-1][y] + // Westgrid[x-1][y-1]; // Northwest} Ex. 49-02, P.466 from textbook.

int[][] grid, futureGrid;void setup() {size(540, 100);frameRate(8);grid = new int[width][height];futureGrid = new int[width][height];float density = 0.3 * width * height;for (int i = 0; i < density; i++) {grid[int(random(width))][int(random(height))] = 1;}background(0);}

void draw() {for (int x = 1; x < width-1; x++) {for (int y = 1; y < height-1; y++) {// Check the number of neighbors (adjacent cells)int nb = neighbors(x, y);if ((grid[x][y] == 1) && (nb < 2)) {futureGrid[x][y] = 0; // Isolation deathset(x, y, color(0));} else if ((grid[x][y] == 1) && (nb > 3)) {futureGrid[x][y] = 0; // Overpopulation deathset(x, y, color(0));} else if ((grid[x][y] == 0) && (nb == 3)) {futureGrid[x][y] = 1; // Birthset(x, y, color(255));} else {futureGrid[x][y] = grid[x][y]; // Survive}}}// Swap current and future gridsint[][] temp = grid;grid = futureGrid;futureGrid = temp;}

// Count the number of adjacent cells 'on'int neighbors(int x, int y) {int north = (y + height-1) % height;int south = (y + 1) % height;int east = (x + 1) % width;int west = (x + width-1) % width;return grid[x][north] + // Northgrid[east][north] + // Northeastgrid[east][y] + // Eastgrid[east][south] + // Southeastgrid[x][south] + // Southgrid[west][south] + // Southwestgrid[west][y] + // Westgrid[west][north]; // Northwest}

Ex. 49-03, P.467 from textbook.

Pascal TrianglePascal Triangle

Pascal Triangle巴斯卡三角形巴斯卡三角形

int[][] a;int a_size = 32;int cell = 10; void setup() { a = new int[a_size][a_size]; size(a_size*cell,a_size*cell); ellipseMode(CORNER); smooth(); noLoop();} 1/101/10

void initialize () { for(int i = 0; i < a_size; i ++) { for (int j = 0; j <= i; j ++) { a[j][i] = 1; } }}

2/102/10

void print_pascal () { for(int i = 0; i < a_size; i ++) { for (int j = 0; j <= i; j ++) { print(a[j][i] + " "); } println(); } }

3/103/10

void draw () { initialize(); print_pascal(); }

4/104/10

1/10 - 4/101/10 - 4/10

int[][] a;int a_size = 32;int cell = 10; void setup() { a = new int[a_size][a_size]; size(a_size*cell,a_size*cell); ellipseMode(CORNER); smooth(); noLoop();}

void initialize () { for(int i = 0; i < a_size; i ++) { for (int j = 0; j <= i; j ++) { a[j][i] = 1; } }}

void print_pascal () { for(int i = 0; i < a_size; i ++) { for (int j = 0; j <= i; j ++) { print(a[j][i] + " "); } println(); } }

void draw () { initialize(); print_pascal(); }

long[][] a;int a_size = 64;int cell = 10; void setup() { a = new long[a_size][a_size]; size(a_size*cell,a_size*cell); ellipseMode(CORNER); smooth(); noLoop();} void draw () { initialize(); gen_pascal(); print_pascal(); draw_pascal(); }

void initialize () { for(int i = 0; i < a_size; i ++) { for (int j = 0; j <= i; j ++) { a[j][i] = 1; } }}

void print_pascal () { for(int i = 0; i < a_size; i ++) { for (int j = 0; j <= i; j ++) { print(a[j][i] + " "); } println(); } } NextNext

void gen_pascal () { for(int i = 2; i < a_size; i ++) { for (int j = 1; j <= i; j ++) { a[j][i] = a[j-1][i-1] + a[j][i-1] ; } } }

5/105/10

void draw_1_dot (long n) { if ((n % 2) == 0) {fill(0);} else {fill(255);} ellipse(0,0,cell,cell);}

void draw_pascal () { for(int i = 0; i < a_size; i ++) { pushMatrix(); for (int j = 0; j <= i; j ++) { draw_1_dot(a[j][i]); translate(cell,0); } popMatrix(); translate(0,cell); } } 6/106/10

void draw () { initialize(); gen_pascal(); print_pascal(); draw_pascal(); }

7/107/10

void draw_1_dot (long n) { if ((n % 2) == 0) {fill(0);} else {fill(255);} ellipse(0,0,cell,cell);}

void draw_pascal () { for(int i = 0; i < a_size; i ++) { pushMatrix(); translate((width/2)-(i+1)*(cell/2.0),0); for (int j = 0; j <= i; j ++) { draw_1_dot(a[j][i]); translate(cell,0); } popMatrix(); translate(0,cell); } } 8/108/10

int[][] a;

long[][] a;

9/109/10

int a_size = 32;

int a_size = 64;

a = new int[a_size][a_size];

a = new long[a_size][a_size];

10/1010/10

1/10 - 10/101/10 - 10/10

long[][] a;int a_size = 64;int cell = 10; void setup() { a = new long[a_size][a_size]; size(a_size*cell,a_size*cell); ellipseMode(CORNER); noStroke(); background(0); smooth(); noLoop();}

void initialize () { for(int i = 0; i < a_size; i ++) { for (int j = 0; j <= i; j ++) { a[j][i] = 1; } }}

void print_pascal () { for(int i = 0; i < a_size; i ++) { for (int j = 0; j <= i; j ++) { print(a[j][i] + " "); } println(); } }

void draw () { initialize(); gen_pascal(); print_pascal(); draw_pascal();}

void gen_pascal () { for(int i = 2; i < a_size; i ++) { for (int j = 1; j <= i; j ++) { a[j][i] = a[j-1][i-1] + a[j][i-1] ; } } }

void draw_1_dot (long n) { if ((n % 3) == 0) {fill(255,0,0);} else {fill(255,255,0);} ellipse(0,0,cell,cell);}

void draw_pascal () { for(int i = 0; i < a_size; i ++) { pushMatrix(); translate((width/2)-(i+1)*(cell/2.0),0); for (int j = 0; j <= i; j ++) { draw_1_dot(a[j][i]); translate(cell,0); } popMatrix(); translate(0,cell); } }

INT (32 bits) : -2,147,483,648 to 2,147,483,647 LONG (64 bits) : -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807

MOD by 2

Waclaw SierpinskiBlaise Pascal

Serpinsky Triangle

MOD by 3

MOD by 4

MOD by 5

MOD by 12

Ch. 49