Project 11 Lydia Jin

sketch

//Lydia Jin
//Section D
//jialuj@andrew.cmu.edu
//Project 11
var myTurtle1;
var flag=false;
function setup(){
    createCanvas(600,600);
    background('black');
    //create 5 turtles that does different things
    myTurtle1=new makeTurtle();
    noLoop();
    }
function draw(){

    strokeJoin(MITER);

    myTurtle1.setColor('silver');
    myTurtle1.setWeight(2);
    //draw stars
    for (i=0;i<50;i++){
      ellipse(random(width),random(height),3,5);
    }
}
//draws constellation when mouse pressed
function mousePressed(){
  noStroke();
  fill('silver');
  var x=mouseX;
  var y=mouseY;
  ellipse(x,y,8,8);
  stroke(0);
  if (flag==false){
      myTurtle1.penDown();
      myTurtle1.goto(x,y);  
  } else{
    //resets the pen
    flag=false;
    myTurtle1.penUp();
    myTurtle1.goto(x,y);
  }
  
        
}
//resets the status when key is pressed
function keyPressed(){
    if (flag==false){
      flag = true;
    } 
}

//////////////////////////////////////////////////////////////////////////////////////////////
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 canvas represents a night sky in the universe. The user can create constellations using the turtle by clicking the mouse and connecting the dots. Once you finish one drawing, you can press any key and start on a new constellation. Below are two finished drawings:

screenshot-1

screenshot2

Leave a Reply