Advertisement
fbz

elementaryCAknit

fbz
Jan 13th, 2014
1,343
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.39 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. //here is the scarf knit with this exact code: http://www.ravelry.com/projects/fbz/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, 900); // width, length. knit scarf in up to 75 wide and 640 to 700 long.
  16. // ^^ add extra 10 to 15 stitches in the width for an industrial knitting machine ^^
  17. //frameRate(8); // Slow down to 8 frames each second
  18. background(0);
  19. set(width-1, 0, on); //set the next to last pixel to white, handy for rule 110
  20. set(4, 0, on); //turn on a second pixel for the seed row
  21. set(42, 0, on); //turn on another pixel for the seed row to white
  22. }
  23.  
  24. void draw() {
  25. // For each pixel, determine new state by examining current
  26. // state and neighbor states and ignore edges that have only
  27. // one neighbor
  28. for (int i = 1; i < width - 1; i++) {
  29. int left = get(i - 1, gen - 1); // Left neighbor
  30. int me = get(i, gen - 1); // Current pixel
  31. int right = get(i + 1, gen - 1); // Right neighbor
  32. if (rules(left, me, right) == 1) {
  33. set(i, gen, on);
  34. }
  35. }
  36. gen++; // Increment the generation by 1
  37. if (gen > height - 1) { // If reached the bottom of the screen,
  38. noLoop(); // stop the program
  39. }
  40. save("1dca_rule110_0.tif"); //save a tiff image of output in sketch folder, rename by hand.
  41. }
  42. // Implement the rules
  43. int rules(color a, color b, color c) {
  44. if ((a == on) && (b == on) && (c == on)) {
  45. return rules[0];
  46. }
  47. if ((a == on) && (b == on) && (c == off)) {
  48. return rules[1];
  49. }
  50. if ((a == on) && (b == off) && (c == on)) {
  51. return rules[2];
  52. }
  53. if ((a == on) && (b == off) && (c == off)) {
  54. return rules[3];
  55. }
  56. if ((a == off) && (b == on) && (c == on)) {
  57. return rules[4];
  58. }
  59. if ((a == off) && (b == on) && (c == off)) {
  60. return rules[5];
  61. }
  62. if ((a == off) && (b == off) && (c == on)) {
  63. return rules[6];
  64. }
  65. if ((a == off) && (b == off) && (c == off)) {
  66. return rules[7];
  67. }
  68. return 0;
  69. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement