Ziningy1-project 11

sketch

//Zining Ye 
//15104 lecture 1 recitation C
//ziningy1@andrew.cmu.edu 
//Project-10 

var mturtle;
var angle=90;
var path=10;

function setup(){
    createCanvas(480,480);
    background(150,170,200);
    frameRate(20); //set the framerate 
    mturtle=makeTurtle(width/2,height/2);
}

function draw(){
 
        
        for(var i=0;i<4;i++){ //make the turtle draw a square
        
        mturtle.setWeight(1)
        mturtle.left(angle);
        mturtle.forward(path);
    }

 mturtle.setColor(color(random(0,150),random(50,150),random(0,255))); //set the randomness of the color 
    mturtle.penUp();
    mturtle.right(2); //turn 2 degree every time it loops 
    path+=0.3 //the length of the square increases 
    mturtle.penDown(); 
    

}


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;}




   


    







This composition is inspired by one of the lecture notes where we put put the turtle codes in the draw function, so this way it loops and draws the pattern gradually. I made the square shape rotates certain degree everytime, while it is also growing in size.

Leave a Reply