Created
October 23, 2015 21:39
-
-
Save camb416/5cce36c0c7ad8c550222 to your computer and use it in GitHub Desktop.
Turtle drawing program with branches
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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