Circle Stacking ðŸ¥ž

Circle Stacking ðŸ¥ž (variation)

Concept: reddit.com/r/generat…rcular_logic_r_code/

```const towerCount = 100; //min=1 max=150 step=1
const interspacing = 1.3; //min=.5 max=3 step=.1
const noise = .5; //min=0 max=1 step=.1
const perspective = 1.4; //min=1 max=2 step=.1
const conify = .5; //min=-1 max=1 step=.1
const border = 5; //min=0 max=50 step=1
const heightMin = 30; //min=1 max=90 step=1
const heightMax = 100; //min=2 max=150 step=1
const rMin = 5; //min=1 max=20 step=1
const rMax = 30; //min=21 max=50 step=1

// You can find the Turtle API reference here: https://turtletoy.net/syntax
Canvas.setpenopacity(1);

// Global code will be evaluated once.
const turtle = new Turtle();
const polygons = new Polygons();

const normalize2 = (a) => scale2(a, 1/len2(a));
const length2 = (a) => Math.sqrt(lengthSquared2(a));
const lengthSquared2 = (a) => dot2(a,a);
const transform2 = (matrix, a) => [matrix[0]*a[0]+matrix[2]*a[1], matrix[1]*a[0]+matrix[3]*a[1]];
const scale2 = (a, scalar) => [a[0]*scalar,a[1]*scalar];
const add2 = (a,b) => [a[0]+b[0],a[1]+b[1]];
const sub2 = (a,b) => [a[0]-b[0],a[1]-b[1]];
const dist2 = (a,b) => Math.hypot(...sub2(a,b));
const lerp2 = (a,b,t) => [a[0]*(1-t)+b[0]*t,a[1]*(1-t)+b[1]*t];
const dot2 = (a,b) => a[0]*b[0]+a[1]*b[1];

if(border > 0) {
const brd = 100 - border;
([
[[-120, -brd], [120, -brd], [120, -120], [-120, -120]],
[[brd, -120], [brd, 120], [120, 120], [120, -120]],
[[-120, brd], [120, brd], [120, 120], [-120, 120]],
[[-brd, -120], [-brd, 120], [-120, 120], [-120, -120]]
]).forEach((b => {
const p = polygons.create();
polygons.draw(turtle, p);
}));

[[-1,-1],[1,-1],[1,1],[-1,1],[-1,-1]].forEach((i, k) => k == 0? turtle.jump(scale2(i, brd)): turtle.goto(scale2(i, brd)))
}

const getRandomHeight = (hMin, hMax) => Math.min(hMin, hMax) + (Math.random() * (Math.max(hMin, hMax) - Math.min(hMin, hMax)));
const getRandomRadius = (rMin, rMax) => Math.min(rMin, rMax) + (Math.random() * (Math.max(rMin, rMax) - Math.min(rMin, rMax)));
const getRandomLocation = () => {
return [
(Math.random() * (200 + rMax + rMax)) - 100 - rMax,
(Math.random() * (200 + rMax + rMax)) - 100 - rMax - heightMax]
}

const towers = [new Tower(
getRandomLocation(),
getRandomHeight(heightMin, heightMax)
)];

while(towers.length < towerCount) {//(let i = 0; i < 40; i++) {
let candidates = Array.apply(null,{length: 10}).map(b => getRandomLocation());

//calc distance to each tower for each candidate
candidates.forEach(c => c.distances = towers.map(t => [t, dist2(c, t.pos) - (t.r + rMin)]));
//filter candidates that are within a towers radius
candidates = candidates.filter(c => c.distances.filter(cd => cd[1] < 0).length == 0);
//sort remaining distances for large to small
candidates.forEach(c => c.distances.sort((a,b) => a[1] < b[1]? -1: 1));
//map the longest distance to the canditate
candidates = candidates.map(c => [c[0], c[1], ...c.distances[0]]);
//sort the candidates to find te candidate with largest distance
candidates.sort((a,b) => a[3] < b[3]? 1: -1);

if(candidates.length == 0) continue;

let radius = Math.min(rMax, Math.max(rMin, candidates[0][3]));

towers.push(new Tower(
[candidates[0][0], candidates[0][1]],
getRandomHeight(heightMin, heightMax)
));
}

towers.sort((a,b) => a.pos[1] < b.pos[1]? 1: -1);

// The walk function will be called until it returns false.
function walk(i) {
const c = heightMax - i;
const tmpTowers = towers.filter(t => t.height >= c);
tmpTowers.forEach(t => {
let p = polygons.create();
polygons.draw(turtle, p);
});
return  c > 0;
}

function Tower(pos, r, height) {
class Tower {
constructor(pos, r, h) {
this.pos = pos;
this.r = r;
this.height = h;

this.basePoints = [];
let max = Math.PI * 2 * this.r | 0;
for(let i = 0; i < max; i++) {
this.basePoints.push([
Math.cos(Math.PI * 2 * (i/max)) * this.r,
-Math.sin(Math.PI * 2 * (i/max)) * this.r / perspective
]);
}
}
getPoints(height) {
const dist = [Math.random() * noise * this.r/3.5, Math.random() * noise * this.r/3.5];
return this.basePoints
.map(p => scale2(p, conify == 0? 1: (conify > 0? 1 - (conify * height / heightMax): (conify * height / heightMax))))
//                .map(p => add2(p, [Math.random(), Math.random()]))