function setup() {
createCanvas(400, 400);
background(0);
strokeJoin(MITER);
strokeCap(PROJECT);
}
function mousePressed(){
drawturtles(mouseX, mouseY);
}
function drawturtles() {
var t1 = makeTurtle(mouseX + 20, mouseY);
var t2 = makeTurtle(mouseX - 20, mouseY);
var t3 = makeTurtle(mouseX, mouseY + 20);
var t4 = makeTurtle(mouseX, mouseY - 20);
t1.penDown;
t2.penDown;
t3.penDown;
t4.pendown;
for(var i = 0; i < 480; i++){
stroke(random(0,255),0,0);
t1.forward(random(0,40));
t1.right (random(0, 180));
t1.forward (random(0,40));
stroke(random(0,255), random(0,255), random(0,255));
t2.forward(random(0,40));
t2.right (random(0, 180));
t2.forward (random(0,40));
stroke(random(0,255), random(0,255), random(0,255));
t3.forward(random(0,40));
t3.right (random(0, 180));
t3.forward (random(0,40));
stroke(random(0,255), random(0,255), random(0,255));
t3.forward(random(0,40));
t3.right (random(0, 180));
t3.forward (random(0,40));
}
}
function draw() {
}
function turtleLeft(d){this.angle-=d;}function turtleRight(d){this.angle+=d;}
function turtleForward(p){var rad=radians(this.angle);var newx=this.x+cos(rad)*p;
var newy=this.y+sin(rad)*p;this.goto(newx,newy);}function turtleBack(p){
this.forward(-p);}function turtlePenDown(){this.penIsDown=true;}
function turtlePenUp(){this.penIsDown = false;}function turtleGoTo(x,y){
if(this.penIsDown){stroke(this.color);strokeWeight(this.weight);
line(this.x,this.y,x,y);}this.x = x;this.y = y;}function turtleDistTo(x,y){
return sqrt(sq(this.x-x)+sq(this.y-y));}function turtleAngleTo(x,y){
var absAngle=degrees(atan2(y-this.y,x-this.x));
var angle=((absAngle-this.angle)+360)%360.0;return angle;}
function turtleTurnToward(x,y,d){var angle = this.angleTo(x,y);if(angle< 180){
this.angle+=d;}else{this.angle-=d;}}function turtleSetColor(c){this.color=c;}
function turtleSetWeight(w){this.weight=w;}function turtleFace(angle){
this.angle = angle;}function makeTurtle(tx,ty){var turtle={x:tx,y:ty,
angle:0.0,penIsDown:true,color:color(128),weight:1,left:turtleLeft,
right:turtleRight,forward:turtleForward, back:turtleBack,penDown:turtlePenDown,
penUp:turtlePenUp,goto:turtleGoTo, angleto:turtleAngleTo,
turnToward:turtleTurnToward,distanceTo:turtleDistTo, angleTo:turtleAngleTo,
setColor:turtleSetColor, setWeight:turtleSetWeight,face:turtleFace};
return turtle;}
For this week’s assignment, I made a program in which four turtles are drawn around the mouse when it is clicked. They move forward and right by random increments until they move off of the canvas.