xxxxxxxxxx
109
let vertices = [];
let innerVertices = [];
let button;
function setup() {
let canvas = createCanvas(500, 500);
canvas.parent("p5-container")
noFill();
strokeWeight(2);
noLoop();
// Create a button and set its position and label
button = createButton('Refresh');
button.parent('button-container');
button.mousePressed(redrawSketch);
}
function redrawSketch() {
redraw();
}
function draw() {
clear();
let cols = 6;
let rows = 6;
let spacing = 80;
let baseRadius = 36;
for (let i = 0; i < cols; i++) {
for (let j = 0; j < rows; j++) {
let x = i * spacing + spacing / 2 + random(-20, 20);
let y = j * spacing + spacing / 2 + random(-20, 20);
let amplitude = random(0.5, 3);
let waves = random(20, 30);
let revolution = random(0.1, 0.9);
let angle = random(0, PI);
drawPencilShaving(x, y, baseRadius, amplitude, waves, TWO_PI * revolution, angle, '#f59d40', '#ffcf99', '#646464');
}
}
}
function drawPencilShaving(x, y, baseRadius, amplitude, waves, revolution, angle, outerColour, woodColour, leadColour) {
push();
translate(x, y); // Move to canvas center
rotate(angle);
// Calculate sine wave circle vertices
vertices = [];
for (let t = 0; t < revolution; t += 0.01) {
let r = baseRadius + amplitude * sin(waves * t);
let x = r * cos(t);
let y = r * sin(t);
vertices.push({ x, y });
}
noStroke();
// Calculate inner circle vertices by scaling the outer vertices
innerVertices = vertices.map(v => ({ x: v.x * 0.85, y: v.y * 0.85 }));
strokeJoin(ROUND);
fill(outerColour);
beginShape();
for (let v of vertices) {
vertex(v.x, v.y);
}
for (let i = innerVertices.length - 1; i >= 0; i--) {
let v = innerVertices[i];
vertex(v.x, v.y);
}
endShape(CLOSE);
let vertices2 = [];
for (let t = 0; t < revolution; t += 0.01) {
let r = baseRadius + amplitude * sin(waves / 3 * t);
let x = r * 0.4 * cos(t);
let y = r * 0.4 * sin(t);
vertices2.push({ x, y });
}
fill(woodColour);
beginShape();
for (let v of vertices2) {
vertex(v.x, v.y);
}
for (let i = innerVertices.length - 1; i >= 0; i--) {
let v = innerVertices[i];
vertex(v.x, v.y);
}
endShape(CLOSE);
let vertices3 = [];
for (let t = 0; t < revolution; t += 0.1) {
let r = baseRadius + amplitude * sin(waves / 2 * t);
let x = r * 0.4 * cos(t);
let y = r * 0.4 * sin(t);
vertices3.push({ x, y });
}
fill(leadColour);
beginShape();
for (let v of vertices3) {
vertex(v.x, v.y);
}
for (let i = vertices2.length - 1; i >= 0; i--) {
let v = vertices2[i];
vertex(v.x, v.y);
}
endShape(CLOSE);
pop();
}