xxxxxxxxxx
178
let noiseMax = 1;
let xoff = 0;
let roff = 0;
let showPoints = false;
let r = 50;
let l = 30;
let allPoints = [];
let firstBody;
function setup() {
createCanvas(400, 400);
noLoop();
}
function draw() {
background(220);
noFill();
stroke(0);
strokeWeight(1);
translate(width / 2, height / 2);
circle(0, 0, r);
drawPoint(0, 0);
firstBody = drawLine(chooseHeadPoint);
allPoints.push(firstBody);
let arm = drawLine(startingFn());
allPoints.push(arm);
let leg = drawLine(startingFn());
allPoints.push(leg);
let ext = drawLine(startingFn());
allPoints.push(ext);
}
function drawPoint(x, y) {
if (showPoints) {
strokeWeight(5);
point(x, y);
strokeWeight(1);
}
}
function keyPressed() {
if (key === "p") {
showPoints = !showPoints;
}
}
function drawLine(starting) {
let points = [];
let recursionCounter = 0;
let newPoint = (x, y, a) => {
if (recursionCounter++ > 25) {
return;
}
let len = map(noise(roff), 0, 1, l*0.1, l*2);
// let r = map(random(), 0, 1, l*0.1, l*2);
let dX = cos(a) * len;
let dY = sin(a) * len;
let nX = x + dX;
let nY = y + dY;
roff += 0.5;
if (outOfBounds(nX, nY)) {
if (points.length < 7) {
points = [];
let start = starting(r);
let x1 = start.x, y1 = start.y, a1 = start.angle;
points.push({"x": x1, "y": y1});
newPoint(x1, y1, a1);
}
return;
} else {
x = nX;
y = nY;
points.push({"x": x, "y": y});
let angInc = angleInc;
if (random() > 0.92) {
angInc = random(PI, TWO_PI);
}
a += map(noise(xoff), 0, 1, -angInc, angInc);
xoff += 1;
newPoint(x, y, a);
}
};
// let start = chooseHeadPoint(r);
let start = starting(r);
let x = start.x, y = start.y, a = start.angle;
// let angleInc = random(PI/12, PI/2);
let angleInc = random(PI/6, PI/2);
points.push({"x": x, "y": y});
newPoint(x, y, a);
if (points.length > 2) {
points.pop();
}
drawPoint(points[0].x, points[0].y);
beginShape();
curveVertex(points[0].x, points[0].y);
points.forEach(p => {
curveVertex(p.x, p.y);
drawPoint(p.x, p.y);
});
let last = points[points.length-1];
curveVertex(last.x, last.y);
endShape();
return points;
}
function outOfBounds(x, y) {
let padding = 0;
if ((x < -width/2 + padding) ||
(x > width/2 - padding) ||
(y < -height/2 + padding) ||
(y > height/2 - padding) ||
(dist(0, 0, x, y) < 50)) {
return true;
} else {
return false;
}
}
function startingFn(r1=0.1, r2=0.8, r3=0.9) {
let rnd = random();
if (rnd <= r1) {
return chooseHeadPoint;
} else if ((rnd > r1) && (rnd <= r2)) {
return chooseFirstBodyPoint;
} else if ((rnd > r2) && (rnd <= r3)) {
return chooseBodyPoint;
} else {
return chooseRandomPoint;
}
}
let chooseHeadPoint = (r) => {
let randomAngle = random(0, TWO_PI);
let x = (cos(randomAngle) * r) / 2;
let y = (sin(randomAngle) * r) / 2;
let a = atan2(y, x);
return {"x": x, "y": y, "angle": a};
}
let chooseFirstBodyPoint = (r) => {
let p = random(firstBody);
let a = random(0, TWO_PI);
return {"x": p.x, "y": p.y, "angle": a};
}
let chooseBodyPoint = (r) => {
let points = random(allPoints);
let p = random(points);
let a = random(0, TWO_PI);
return {"x": p.x, "y": p.y, "angle": a};
}
let chooseRandomPoint = (r) => {
let padding = 20;
let x = random(-width/2 + padding, width/2 - padding);
let y = random(-height/2 + padding, height/2 - padding);
let a = random(0, TWO_PI);
return {"x": x, "y": y, "angle": a};
}