Skip to content

Instantly share code, notes, and snippets.

@analogpixel
Created July 8, 2015 21:10
Show Gist options
  • Save analogpixel/8b8a6f825bdb539c775c to your computer and use it in GitHub Desktop.
Save analogpixel/8b8a6f825bdb539c775c to your computer and use it in GitHub Desktop.
3d turtle and stuff
import java.util.*;
String system;
String system2;
Turtle t;
class Turtle {
Stack st;
float x, y,z; // Current position of the turtle
float angle = -90; // Current heading of the turtle
boolean penDown = true; // Is pen down?
// Set up initial position
Turtle (float xin, float yin) {
x = xin;
y = yin;
z = -500;
st = new Stack();
}
// Move forward by the specified distance
void forward (float distance) {
// Calculate the new position
float xtarget = x+cos(radians(angle)) * distance;
float ytarget = y+sin(radians(angle)) * distance;
// If the pen is down, draw a line to the new position
if (penDown) line(x, y, z, xtarget, ytarget, z);
// Update position
x = xtarget;
y = ytarget;
}
void push() {
st.push( new Float[]{x,y,z} );
}
void pop() {
Float[] a = (Float []) st.pop();
x = a[0];
y = a[1];
z = a[2];
}
void down(float distance) {
z = z + distance;
}
void up(float distance) {
z = z - distance;
}
// Move back by specified distance
void back (float distance) {
forward(-distance);
}
// Turn left by given angle
void left (float turnangle) {
angle -= turnangle;
}
// Turn right by given angle
void right (float turnangle) {
angle += turnangle;
}
// Set the pen to be up
void penUp() {
penDown = false;
}
// Set the pen to be down
void penDown() {
penDown = true;
}
}
void setup() {
size(1920,1200,P3D);
t = new Turtle(width*4, -height*3);
system = new String("aa");
system2 = new String();
println("Building String");
for (int i=0; i < 6; i++) {
for (char ch: system.toCharArray()) {
if (ch == 'a') {
system2 += "abab";
} else if (ch =='b') {
system2 += "bba[aa]b";
} else {
system2 += ch;
}
}
system = system2;
}
println("Drawing Image");
lights();
background(255);
rotateY(radians(20));
rotateX(radians(50));
rotateZ(radians(-60));
for (char ch: system.toCharArray()) {
if (ch == 'a') {
t.left(90);
t.forward(50);
t.left(90);
t.forward(50);
} else if (ch =='b') {
t.right(90);
t.forward(50);
t.right(90);
t.forward(50);
t.left(90);
t.forward(50);
} else if (ch == '[') {
t.push();
t.up(1500);
} else if (ch == ']') {
t.pop();
}
}
saveFrame("ls_out.jpg");
}
void draw() {
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment