Advertisement
fbz

1DCAknitpurple

fbz
Feb 7th, 2014
579
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.30 KB | None | 0 0
  1. //processing sketch, modded by fbz from an example from processing book
  2. //1d elementary cellular automata with tiff saving ability
  3. //handy script for output on a knitting machine
  4. //knit with this exact code: http://www.ravelry.com/projects/fbz/purple-and-black-rule-110-scarf/
  5. //Processing: A Programming Handbook for Visual Designers and Artists
  6. // http://processing.org/books/
  7. // http://processing.org/img/learning/Processing-Examples-002.zip
  8.  
  9. int[] rules = { 0, 1, 1, 0, 1, 1, 1, 0 }; //which rule would you like?
  10. int gen = 1; // Generation
  11. color on = color(255);
  12. color off = color(0);
  13.  
  14. void setup() {
  15. size(61, 816); // width, length. knit scarf in 75 or less and from about 640 to 800 long.
  16. //frameRate(8); // Slow down to 8 frames each second
  17. background(0);
  18. set(width-1, 0, on); //set the next to last pixel to white, handy for rule 110
  19. set(23, 0, on); //turn on a second pixel for the seed row
  20. set(42, 0, on); //turn on another pixel for the seed row to white
  21. }
  22.  
  23. void draw() {
  24. // For each pixel, determine new state by examining current
  25. // state and neighbor states and ignore edges that have only
  26. // one neighbor
  27. for (int i = 1; i < width - 1; i++) {
  28. int left = get(i - 1, gen - 1); // Left neighbor
  29. int me = get(i, gen - 1); // Current pixel
  30. int right = get(i + 1, gen - 1); // Right neighbor
  31. if (rules(left, me, right) == 1) {
  32. set(i, gen, on);
  33. }
  34. }
  35. gen++; // Increment the generation by 1
  36. if (gen > height - 1) { // If reached the bottom of the screen,
  37. noLoop(); // stop the program
  38. }
  39. save("1dCA_rule110_1.tif"); //save a tiff image of output in sketch folder, rename by hand.
  40. }
  41. // Implement the rules
  42. int rules(color a, color b, color c) {
  43. if ((a == on) && (b == on) && (c == on)) {
  44. return rules[0];
  45. }
  46. if ((a == on) && (b == on) && (c == off)) {
  47. return rules[1];
  48. }
  49. if ((a == on) && (b == off) && (c == on)) {
  50. return rules[2];
  51. }
  52. if ((a == on) && (b == off) && (c == off)) {
  53. return rules[3];
  54. }
  55. if ((a == off) && (b == on) && (c == on)) {
  56. return rules[4];
  57. }
  58. if ((a == off) && (b == on) && (c == off)) {
  59. return rules[5];
  60. }
  61. if ((a == off) && (b == off) && (c == on)) {
  62. return rules[6];
  63. }
  64. if ((a == off) && (b == off) && (c == off)) {
  65. return rules[7];
  66. }
  67. return 0;
  68. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement