//Grace Wanying Hou
//15-104 Section D
//ghou@andrew.cmu.edu
//Project 11
var turtle =[];
var count = 10;
var dp;
function preload(){
var imageurl = "https://i.imgur.com/S483jxr.jpg";
dp = loadImage(imageurl);//loading the pic of my boy friendo
}
function setup() {
background(0);
createCanvas(380,300);
image(dp,0,0)
dp.loadPixels();
for (var i = 0; i < count; i ++) {//setup the strokes
turtle[i] = makeTurtle(0, 0);
turtle[i].penDown;
}
strokeJoin(MITER);
strokeCap(PROJECT);
frameRate(30);
}
function draw() {
for (var i = 0; i < count; i ++) {
var pointcolour = dp.get(floor(mouseX),floor(mouseY));
turtle[i].setColor(color(pointcolour)); //setting the colour to the pixel at the mouse point
turtle[i].setWeight(random(15));//randomizing the weight
turtle[i].turnToward(mouseX,mouseY, turtle[0].angleTo(pmouseX, pmouseY)); //turn along mouse movement.
turtle[i].forward(turtle[i].distanceTo(mouseX, mouseY));//move along mouse movement.
}
}
//given turtle stuffs
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) {
strokeJoin(MITER);
strokeCap(PROJECT);
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 week I used turtle graphics to abstract colours from a portrait and “painting” with strokes randomized by mouse movements and those colours.