Advertisement
Guest User

Untitled

a guest
Feb 11th, 2016
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.41 KB | None | 0 0
  1. Life life;
  2. PImage img;
  3. boolean running = false;
  4.  
  5. void setup() {
  6. size(500, 500);
  7. //Load the image.
  8. img = loadImage("eye.jpg");
  9. //Create a new Game of Life, with each cell being 5 pixels in dimension.
  10. //And a threshold of 105.
  11. life = new Life(5, 105);
  12. frameRate(10);
  13. }
  14.  
  15. void draw() {
  16. //If the game of life is running, then update it and display it each iteration of draw().
  17. if (running) {
  18. life.update();
  19. life.display();
  20. }
  21. }
  22.  
  23. //Press the mouse to start/stop the game of life.
  24. void mousePressed() {
  25. running = !running;
  26. }
  27.  
  28. class Life {
  29. private int cellSize;
  30. private int currentState[][];
  31. private int tempState[][];
  32. private color pixl;
  33. private int average;
  34. private int threshold;
  35.  
  36. public Life(int size, int thresh) {
  37. this.cellSize = size;
  38. this.threshold = thresh;
  39. this.currentState = new int[width/this.cellSize][height/this.cellSize];
  40. this.tempState = new int[width/this.cellSize][height/this.cellSize];
  41. this.initialize();
  42. }
  43.  
  44. public void initialize() {
  45. //Iterate over the whole image, using the size of the cell as the step.
  46. for (int x = 0; x < img.width / this.cellSize; x++) {
  47. for (int y = 0; y < img.height / this.cellSize; y++) {
  48. pixl = img.get(x * this.cellSize, y * this.cellSize);
  49. //Finding how "bright" each pixel is. The lower the value, the darker the pixel.
  50. //For instance, a dark blue of rgb(0, 0, 153) is (0 + 0 + 153) / 3 = 51.
  51. //A light yellow of rgb(255, 255, 102) is (255 + 255 + 102) / 3 = 204.
  52. average = (int(red(pixl)) + int(green(pixl)) + int(blue(pixl))) / 3;
  53.  
  54. /* Now that we have the average of the pixel at this location, we have to check if
  55. it falls under the threshold specified. If the pixel is dark enough, then the corresponding
  56. cell in the game-of-life board is set to living. */
  57. if (average < this.threshold) {
  58. this.currentState[x][y] = 1;
  59. } else {
  60. //Pixel is too bright, discard it.
  61. this.currentState[x][y] = 0;
  62. }
  63. }
  64. }
  65. //After initialization, display the loaded image.
  66. this.display();
  67. }
  68.  
  69. public void update() {
  70. //Copy all the values of currentState in to tempState.
  71. for (int x = 0; x < width / this.cellSize; x++) {
  72. for (int y = 0; y < height / this.cellSize; y++) {
  73. this.tempState[x][y] = this.currentState[x][y];
  74. }
  75. }
  76.  
  77. //Iterate through all the cells of the 2D array.
  78. for (int x = 0; x < width / this.cellSize; x++) {
  79. for (int y = 0; y < height / this.cellSize; y++) {
  80. //If the cell is living.
  81. if (this.tempState[x][y] == 1) {
  82. /* Check neihbors. If under 2, kill the cell due to underpopulation.
  83. If more than 3, then kill the cell due to overpopulation. Rules 1 and 3. */
  84. if (this.countNeighbors(x, y) < 2 || this.countNeighbors(x, y) > 3) {
  85. this.currentState[x][y] = 0;
  86. }
  87. } else {
  88. //A dead cell with 3 live neighbors becomes alive through reproduction. Rule 4.
  89. if (this.countNeighbors(x, y) == 3) {
  90. this.currentState[x][y] = 1;
  91. }
  92. } // Rule 2. "Any live cell with two or three live neighbours lives on to the next generation.".
  93. }
  94. }
  95. }
  96.  
  97. //Displays the board.
  98. public void display() {
  99. //noStroke determines wether the squares will have an outline.
  100. noStroke();
  101.  
  102. //Iterate through the whole currentState array.
  103. for (int x = 0; x < width / this.cellSize; x++) {
  104. for (int y = 0; y < height / this.cellSize; y++) {
  105. //If a cell is alive.
  106. if (this.currentState[x][y] == 1) {
  107. //Draw a black square.
  108. fill(0);
  109. rect(x * this.cellSize, y * this.cellSize, this.cellSize, this.cellSize);
  110. } else {
  111. //If it's not alive, draw a white square.
  112. fill(255, 255, 255);
  113. rect(x * this.cellSize, y * this.cellSize, this.cellSize, this.cellSize);
  114. }
  115. }
  116. }
  117. }
  118.  
  119. public int countNeighbors(int row, int col) {
  120. int count = 0;
  121.  
  122. for(int i = row - 1; i <= row + 1; i++) {
  123. //Checks overflow.
  124. if (i >= 0 && i < this.tempState.length) {
  125. for(int j = col - 1; j <= col + 1; j++) {
  126. if (j >= 0 && j < this.tempState[i].length) {
  127. if (i != row || j != col) {
  128. if (this.tempState[i][j] == 1) {
  129. count++;
  130. }
  131. }
  132. }
  133. }
  134. }
  135. }
  136.  
  137. return count;
  138. }
  139. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement