Advertisement
Guest User

Untitled

a guest
Jul 28th, 2015
181
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.42 KB | None | 0 0
  1.  
  2. import java.util.*;
  3.  
  4. String system;
  5. String system2;
  6. Turtle t;
  7.  
  8. class Turtle {
  9. Stack st;
  10.  
  11. float x, y,z; // Current position of the turtle
  12. float angle = -90; // Current heading of the turtle
  13. boolean penDown = true; // Is pen down?
  14.  
  15. // Set up initial position
  16. Turtle (float xin, float yin) {
  17. x = xin;
  18. y = yin;
  19. z = -500;
  20. st = new Stack();
  21.  
  22. }
  23.  
  24. // Move forward by the specified distance
  25. void forward (float distance) {
  26.  
  27. // Calculate the new position
  28. float xtarget = x+cos(radians(angle)) * distance;
  29. float ytarget = y+sin(radians(angle)) * distance;
  30.  
  31. // If the pen is down, draw a line to the new position
  32. if (penDown) line(x, y, z, xtarget, ytarget, z);
  33.  
  34. // Update position
  35. x = xtarget;
  36. y = ytarget;
  37.  
  38. }
  39.  
  40. void push() {
  41. st.push( new Float[]{x,y,z} );
  42. }
  43.  
  44. void pop() {
  45. Float[] a = (Float []) st.pop();
  46. x = a[0];
  47. y = a[1];
  48. z = a[2];
  49. }
  50.  
  51. void down(float distance) {
  52. z = z + distance;
  53. }
  54.  
  55. void up(float distance) {
  56. z = z - distance;
  57. }
  58.  
  59. // Move back by specified distance
  60. void back (float distance) {
  61. forward(-distance);
  62. }
  63.  
  64. // Turn left by given angle
  65. void left (float turnangle) {
  66. angle -= turnangle;
  67. }
  68.  
  69. // Turn right by given angle
  70. void right (float turnangle) {
  71. angle += turnangle;
  72. }
  73.  
  74. // Set the pen to be up
  75. void penUp() {
  76. penDown = false;
  77. }
  78.  
  79. // Set the pen to be down
  80. void penDown() {
  81. penDown = true;
  82. }
  83. }
  84.  
  85.  
  86.  
  87. void setup() {
  88. size(1920,1200,P3D);
  89.  
  90. t = new Turtle(width*4, -height*3);
  91. system = new String("aa");
  92. system2 = new String();
  93.  
  94. println("Building String");
  95. for (int i=0; i < 6; i++) {
  96. for (char ch: system.toCharArray()) {
  97.  
  98. if (ch == 'a') {
  99. system2 += "abab";
  100. } else if (ch =='b') {
  101. system2 += "bba[aa]b";
  102. } else {
  103. system2 += ch;
  104. }
  105.  
  106. }
  107.  
  108. system = system2;
  109. }
  110.  
  111. println("Drawing Image");
  112. lights();
  113. background(255);
  114. rotateY(radians(20));
  115. rotateX(radians(50));
  116. rotateZ(radians(-60));
  117. for (char ch: system.toCharArray()) {
  118. if (ch == 'a') {
  119. t.left(90);
  120. t.forward(50);
  121. t.left(90);
  122. t.forward(50);
  123. } else if (ch =='b') {
  124. t.right(90);
  125. t.forward(50);
  126. t.right(90);
  127. t.forward(50);
  128. t.left(90);
  129. t.forward(50);
  130. } else if (ch == '[') {
  131. t.push();
  132. t.up(1500);
  133.  
  134. } else if (ch == ']') {
  135. t.pop();
  136. }
  137.  
  138. }
  139.  
  140. saveFrame("ls_out.jpg");
  141. }
  142.  
  143.  
  144. void draw() {
  145.  
  146.  
  147.  
  148. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement