xxxxxxxxxx
97
function setup() {
createCanvas(800, 800);
background(220);
let cloud = []
for (let i = 0; i < 150; i++) {
cloud[i] = {x: random(width), y: random(height)}
circle(cloud[i].x, cloud[i].y, 5)
}
let Clouds = [[cloud]]
for (let i = 0; i < 5; i++) {
let newCloud = []
for (let cloud of Clouds) {
// Calculate Bounds
let b = bounds(cloud)
// Define Lines
let l0, l1;
if (i%2 == 0) {
l0 = { x: 0, y: b.y0 + (b.y1 - b.y0) * random(0.1,0.9) }
l1 = { x: width, y: b.y0 + (b.y1 - b.y0) * random(0.1,0.9) }
} else {
l0 = { x: b.x0 + (b.x1 - b.x0) * random(0.1,0.9), y: 0 }
l1 = { x: b.x0 + (b.x1 - b.x0) * random(0.1,0.9), y: height }
}
let cloud1 = []
let cloud2 = []
let rr = random();
for (let p of cloud) {
if (isLeft(p, l0,l1) > 0 || (i > 2 && rr > 0.2)) cloud1.push(p)
else cloud2.push(p)
}
newCloud.push(cloud1)
newCloud.push(cloud2)
}
Clouds = newCloud;
}
for (let cloud of Clouds) {
cloud = sortPoints(cloud)
for (let i = 0; i < cloud.length; i++) {
let p = cloud[i]
let p2 = cloud[(i+1)%cloud.length]
line(p.x,p.y,p2.x,p2.y)
}
}
}
function draw() {
}
let bounds = (cloud) => {
let minX = Infinity, maxX = -Infinity, minY = Infinity, maxY = -Infinity;
for (let p of cloud) {
minX = Math.min(p.x,minX)
minY = Math.min(p.y,minY)
maxX = Math.max(p.x,maxX)
maxY = Math.max(p.x,maxY)
}
return {x0 : minX, x1: maxX, y0: minY, y1: maxY}
};
function sortPoints(points) {
points = points.splice(0);
var p0 = {};
p0.y = Math.min.apply(null, points.map(p=>p.y));
p0.x = Math.max.apply(null, points.filter(p=>p.y == p0.y).map(p=>p.x));
points.sort((a,b)=>angleCompare(p0, a, b));
return points;
};
function angleCompare(p0, a, b) {
var left = isLeft(p0, a, b);
if (left == 0) return distCompare(p0, a, b);
return left;
}
function isLeft(p0, a, b) {
return (a.x-p0.x)*(b.y-p0.y) - (b.x-p0.x)*(a.y-p0.y);
}
function distCompare(p0, a, b) {
var distA = (p0.x-a.x)*(p0.x-a.x) + (p0.y-a.y)*(p0.y-a.y);
var distB = (p0.x-b.x)*(p0.x-b.x) + (p0.y-b.y)*(p0.y-b.y);
return distA - distB;
}