Advertisement
fbz

create an elementary cellular automaton GIF

fbz
Jun 14th, 2015
2,541
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.29 KB | None | 0 0
  1. //_1DCA_save_GIF by fbz written in Processing
  2. //generates an elementary cellular automata and saves it as an animated gif
  3. //get the gifAnimation processing library at http://extrapixel.github.io/gif-animation/
  4. import gifAnimation.*;
  5.  
  6. GifMaker gifExport;
  7. int[] rules = { 0, 1, 1, 0, 1, 1, 1, 0 };
  8. int gen = 1; // Generation
  9. color on = color(255);
  10. color off = color(0);
  11.  
  12. void setup() {
  13. size(61, 680); //width and length of the gif
  14. background(0);
  15. set(width-1, 0, on); //set the next to last pixel to white
  16. set(23, 0, on); //turn some more pixels on for the seed row
  17. set(42, 0, on);
  18. //set(7, 0, on);
  19. //set(38, 0, on);
  20. //set(28, 0, on);
  21. //set(46, 0, on);
  22. frameRate(15);
  23. gifExport = new GifMaker(this, "rule110_1.gif");
  24. gifExport.setRepeat(0); // make it an "endless" animation
  25. gifExport.setTransparent(255, 255, 255); //make white transparent
  26. gifExport.setDelay(1000/15); //divide by the frameRate
  27. }
  28.  
  29. void draw() {
  30. // For each pixel, determine new state by examining current
  31. // state and neighbor states and ignore edges that have only
  32. // one neighbor
  33. for (int i = 1; i < width - 1; i++) {
  34. int left = get(i - 1, gen - 1); // Left neighbor
  35. int me = get(i, gen - 1); // Current pixel
  36. int right = get(i + 1, gen - 1); // Right neighbor
  37. if (rules(left, me, right) == 1) {
  38. set(i, gen, on);
  39. }
  40. }
  41. gifExport.addFrame(); //add a new frame to the gif
  42. gen++; // Increment the generation by 1
  43. if (gen > height - 1) { // If it reached the bottom of the screen,
  44. noLoop(); // stop the program
  45. gifExport.finish(); //write gif file to sketch folder
  46. }
  47. }
  48. // Implement the rules
  49. int rules(color a, color b, color c) {
  50. if ((a == on) && (b == on) && (c == on)) {
  51. return rules[0];
  52. }
  53. if ((a == on) && (b == on) && (c == off)) {
  54. return rules[1];
  55. }
  56. if ((a == on) && (b == off) && (c == on)) {
  57. return rules[2];
  58. }
  59. if ((a == on) && (b == off) && (c == off)) {
  60. return rules[3];
  61. }
  62. if ((a == off) && (b == on) && (c == on)) {
  63. return rules[4];
  64. }
  65. if ((a == off) && (b == on) && (c == off)) {
  66. return rules[5];
  67. }
  68. if ((a == off) && (b == off) && (c == on)) {
  69. return rules[6];
  70. }
  71. if ((a == off) && (b == off) && (c == off)) {
  72. return rules[7];
  73. }
  74. return 0;
  75. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement