Skip to content

Instantly share code, notes, and snippets.

@camb416
Created October 23, 2015 21:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save camb416/5cce36c0c7ad8c550222 to your computer and use it in GitHub Desktop.
Save camb416/5cce36c0c7ad8c550222 to your computer and use it in GitHub Desktop.
Turtle drawing program with branches
class Point{
float x,y;
Point(float _x, float _y){
x = _x;
y = _y;
}
};
class Turtle{
Point p;
float a; // angle
float s; // speed
float rv; // rotational velocity
Turtle(Point _p){
p = _p;
a = random(TWO_PI);
s = random(1.0);
rv = random(1.0)-0.5;
}
Turtle(Point _p,float _a,float _s){
p = _p;
a = _a;
s = _s;
rv = random(1.0)-0.5;
}
void update(){
rv = random(0.1)-0.05;
a += rv;
p.x += cos(a)*s;
p.y += sin(a)*s;
}
void draw(){
point(p.x,p.y);
}
}
Turtle t;
ArrayList<Turtle> turtles;
void setup(){
stroke(255,255,255,64);
turtles = new ArrayList<Turtle>();
size(1920,1080);
background(0);
for(int i=0;i<1;i++){
t = new Turtle(new Point(width/2,height/2));
turtles.add(t);
}
}
void draw(){
for(int i=0;i<turtles.size();i++){
t = turtles.get(i);
t.update();
}
for(int i=0;i<turtles.size();i++){
t = turtles.get(i);
t.draw();
}
}
void mousePressed(){
int numTurtles = turtles.size();
for(int i=0;i<numTurtles;i++){
t = turtles.get(i);
Turtle t2 = new Turtle(new Point(t.p.x,t.p.y));
turtles.add(t2);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment