/*
week 10-assignment-b
Xindi Lyu
Section A
xindil@andrew.cmu.edu
*/
var T1
var T2
var T3
var T4
//make the hexagons
function setup(){
createCanvas(600,400);
background(255);
T1=makeTurtle(width/2,height/2);
T1.setColor(color("pink"));
T1.setWeight(5);
T1.penDown();
T2=makeTurtle(width/20,height/4);
T2.setColor("green");
T2.setWeight(5);
T2.penDown();
frameRate(10);
T3=makeTurtle(width*9/10,height/2);
T3.setColor(color("blue"));
T3.setWeight(5);
T3.penDown();
}
//get the hexagons to orient in a golden ratio spiral
function draw(){
T1.forward(6);
T1.left(random(0,6));
T1.turnToward(mouseX,mouseY,10);
T2.forward(6);
T2.left(random(0,6));
T2.turnToward(mouseX,mouseY,10);
T3.forward(6);
T3.left(random(1,6));
T3.turnToward(mouseX,mouseY,10);
}
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 project I created three turtles following the cursor while maintaining their own circulating procedure. It resulted in a very interesting knotting effect.