AndrewWang-Project-11

sketch

var myTurtles = [];
var img;
function preload(){
    img = loadImage("https://i.imgur.com/eQWbexS.png");
}
function setup() {
    createCanvas(600, 400);
    var turtle = new makeTurtle(width/2, height/2);
    turtle.setColor("yellow");
    turtle.setWeight(4);
    myTurtles.push(turtle);
    strokeJoin(MITER);
    strokeCap(PROJECT);
   
}
function mousePressed(){
    //creates new turtle on mouse click
    var ranColor = random(0,255);
    px = random(0,width);
    py = random(0,height);
    turtle = new makeTurtle(px,py);
    turtle.setColor('yellow');
    turtle.setWeight(4);
    myTurtles.push(turtle);
}

function draw() {
    //draws image
    background(255);
    image(img, 0, 0, 600, 400);
    //draws firefly 
    for(i=0; i<myTurtles.length; i++){
        myTurtles[i].penDown();
        drawTurtles(myTurtles[i]);
        myTurtles[i].penUp();
    }
}

function drawTurtles(turtle){
    //draws a firefly
    angle = random(-90,90);
    distance = random(0,3);
    turtle.right(angle);
    turtle.forward(distance);
}
////////

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

I wanted to replicate the effect of fireflies using the turtles. I made them move randomly and change direction randomly in order to do this over a dark relevant background.

Leave a Reply