akluk-Project-11-TurtleFreeStyle

For this project, I simply just used a lot of randomization while trying to emulate the game of snakes with different colors for trails they leave. Below is an image of my draft

A simple draft

SImple example of what the program draws

sketch
//Alvin Luk
//Section A
//akluk@andrew.cmu.edu
//Assignment-10-A

//define line weight, starting positions of turtles, right angle, 
//a unit of length for patterns, array of turtles, and max number of iterations
var line_weight = 10;
var r = 90;
var unit = 10;
var turtles = [];
var max_num = 34;
var cols = [];
function setup() {
    //setup canvas and stroke properties
    createCanvas(479, 479);
    background(255);
    stroke(0);
    strokeJoin(MITER);
    strokeCap(PROJECT);
    cols = [color(60,186,84),color(244,194,13),color(219,50,54),color(72,133,237)];
    //adds each turtle to its appropriate location, stroke weight
    //color, and also initial orientation. 
    for (var i = 0; i < cols.length; i++){
    	turtles.push(makeTurtle(width/2,height/2));
    	turtles[i].setWeight(line_weight);
    	turtles[i].setColor(cols[i]);
    	for (var j = 0; j < i ; j++) {
    		turtles[i].left(r);
    	}
	}
}


function draw() {
	for (var i = 0; i < turtles.length; i++){
		var k = random(10,30);
		var dir = random([0,1]);
		turtles[i].forward(k);
		if (dir == 0){
			turtles[i].left(r);
		}
		else {
			turtles[i].right(r);
		}
	}
}


function mousePressed(){
	turtles = [];
	for (var i = 0; i < cols.length; i++){
		turtles.push(makeTurtle(mouseX,mouseY));
    	turtles[i].setWeight(line_weight);
    	turtles[i].setColor(cols[i]);
    	for (var j = 0; j < i ; j++) {
    		turtles[i].left(r);
    	}
	}
}

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

Leave a Reply