xxxxxxxxxx
84
let points = [];
// Try layering multiple snowflakes!
// Uncomment all the lines with "pointsLayer2"
// You can also try adding some opacity to the fill!
// let pointsLayer2 = [];
function setup() {
createCanvas(600, 600);
points = generateSnowflake(6, height/2);
// pointsLayer2 = generateSnowflake(6, height/2);
}
function draw() {
background(50);
fill(255);
noStroke();
drawPoints(points, width/2, height/2);
// drawPoints(pointsLayer2, width/2, height/2);
}
function drawPoints(points, x, y) {
beginShape();
for(let i = 0; i < points.length; i ++) {
const point = points[i];
vertex(x + point.x, y + point.y);
}
endShape(CLOSE);
}
function generateSnowflake(numSegments, radius) {
const segmentPoints = pointsForSegment(numSegments, radius);
return snowflakeFromSegment(numSegments, segmentPoints);
}
// Returns a list of points (in polar coordinates!)
// that define the shape of one "segment" of a snowflake
function pointsForSegment(numSegments, radius) {
const numPointsInSeg = int(random(4, 10));
const segmentPoints = [];
const segmentAngle = PI/numSegments;
// Generate random points in half of the segment
// These are stored in polar coordinates
for(let i = 0; i < numPointsInSeg; i ++) {
const a = random(segmentAngle);
const r = random(radius);
segmentPoints.push(createVector(a, r));
}
// Reflect the points by flipping the angle
for(let i = numPointsInSeg - 1; i >= 0; i --) {
const point = segmentPoints[i];
segmentPoints.push(createVector(-point.x, point.y));
}
return segmentPoints;
}
// Takes the points that make up one segment
// and returns a list of points that make up an
// entire snowflake (in cartesian coordinates!)
function snowflakeFromSegment(numSegments, segmentPoints) {
const points = [];
for(let i = 0; i < numSegments; i ++) {
const ang = i * TWO_PI/numSegments;
for(let j = 0; j < segmentPoints.length; j ++) {
const point = segmentPoints[j];
const x = cos(point.x + ang) * point.y;
const y = sin(point.x + ang) * point.y;
points.push(createVector(x, y));
}
}
return points;
}
function mouseReleased() {
points = generateSnowflake(6, height/2);
// pointsLayer2 = generateSnowflake(6, height/2);
}