var hiddenImage;
var turtle1;
var turtle2;
function preload() {
var imageLink = "http://i.imgur.com/Q1e8Pxg.jpg";
hiddenImage = loadImage(imageLink);
}
function setup() {
createCanvas(500, 500);
background(0);
hiddenImage.loadPixels();
turtle1 = makeTurtle(width/2, height/2);
turtle2 = makeTurtle(width/2, height/2);
frameRate(100);
}
function draw() {
//image(hiddenImage, 0, 0);
var pixcolor = hiddenImage.get(turtle1.x, turtle1.y);
print(pixcolor);
turtle1.setColor(pixcolor);
turtle1.setWeight(3);
turtle1.penDown();
if (turtle1.x < width-20 & turtle1.x > 0) {
turtle1.forward(1);
turtle1.left(random(-5,5));
}
if (turtle1.x > width-20 || turtle1.x < 20 || turtle1.y < 20 || turtle1.y > height-20 ) {
turtle1.forward(-5);
turtle1.left(random(5,10));
}
var pixcolor2 = hiddenImage.get(turtle2.x, turtle2.y);
turtle2.setColor(pixcolor2);
turtle2.setWeight(3);
turtle2.left(random(-5,5));
turtle2.left(random(-5,5));
turtle2.penDown();
if (turtle2.x < width-20 & turtle2.x > 0) {
turtle2.forward(1);
turtle2.right(random(-5,5));
}
if (turtle2.x > width-20 || turtle2.x < 20 || turtle2.y < 20 || turtle2.y > height-20 ) {
turtle2.forward(-5);
turtle2.right(random(5,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 used two turtles to draw the pixels of a hidden image. The turtles turn in random directions within a certain range of degrees, and they also turn once they hit the edge of the screen. Below are some possible turtle paths as they move across the screen.