### Downtownscape

Using the new and improved faster clipper.

```// LL 2021

const turtle = new Turtle();

const buildings = 265; // min=1 max=900 step=1
const seed = 0; // min=0 max=100 step=1
const style = 2; // min=0 max=3 step=1 (Preview,Stripes,Quads,Hatching (not good))
Canvas.setpenopacity(style == 0 ? 0.25 : 1);

const perspective = 1.8; // min=1 max=3 step=0.1

const camera_theta = -.1; // min=-1 max=1 step=0.01
const camera_phi = 0.3;  // min=0 max=1 step=0.01
const camera_r = 7;   // min=0.1 max=20 step=0.1
const look_at_z = 5;   // min=-10 max=10 step=0.1

const mult = (style == 1) ? 2 : 1;

function walk(i, t) {
if (i == 0) {
seed_t = (t < 1 && seed == 0) ? 1 : seed;
const cameraOffset = [
camera_r * perspective ** 3 * Math.cos((camera_theta+t*2) * Math.PI) * Math.sin((camera_phi/2+0.5) * Math.PI),
camera_r * perspective ** 3 * Math.sin((camera_theta+t*2) * Math.PI) * Math.sin((camera_phi/2+0.5) * Math.PI),
-camera_r * perspective ** 3 * Math.cos((camera_phi/2+0.5) * Math.PI)
];
const cameraLookAt = [0, 0, look_at_z];

const buildings_t = buildings;// * t;
rng = undefined;
polygons = new Polygons();

makeDome(0, 0, 5);

const countx = Math.ceil(buildings_t ** 0.5);
var countz = 1; while (countx * countz < buildings_t) countz++;
const count = Math.max(countx, countz);
for (var k=0; k<buildings_t; k++) {
const kx = (k % count    ) * 2.1 - (countx-1);
const kz = (k / count | 0) * 2.1 - (countz-1);
if (Math.floor((k % count)+5) % 6 == 0) continue;
//if (Math.floor((k / count | 0)+5) % 6 == 0) continue;
const height = 1 + (random()**10) * 10 | 0;
if (random() < 0.5) makeOddBuilding(kx, kz, 1, height);
else makeRectBuilding(kx, kz, 1, height);
}

const road_size = count * 2 * 1.1 + count;

all_quads.sort(function(a, b) { return a.z - b.z; });
}

drawSky();
return false;
}

const hmin = 0.3, hmax = 0.9;
var h = obj.fh ? 0.5 : 0;
if (style == 3) {
const light = getLight(obj.points);
h = hmin + light * (hmax - hmin);
}
drawPoints(obj.points, h, Math.PI/4);

return true;
}

function getLight(points) {
const v1 = sub3(points[1], points[0]);
const v2 = sub3(points[3], points[0]);
const n = normalize3(cross3(v1, v2));
return Math.min(1, Math.max(0, 0.5 + n[0] * n[2] * 2)) ** 2;
//return 0.5 * dot3(n, [0.9, 0, -0.436]) + 0.5;
}

function projectAndAdd(op0, op1, op2, op3, fh=false)
{
const pp0 = project(op0), pp1 = project(op1), pp2 = project(op2), pp3 = project(op3);
const zmax = Math.max(Math.max(Math.max(pp0[2], pp1[2]), pp2[2]), pp3[2])
const obj = { z:zmax, points:[pp0, pp1, pp2, pp3], opoints:[op0, op1, op2, op3], fh:fh };
}

function makeGround(size) {
const block_size = 0.25 / mult;
const count = size / block_size;
for (var i=0; i<count; i++) {
for (var j=0; j<count; j++) {
var x1 = -size/2 + i * block_size;
var z1 = -size/2 + j * block_size;
var x2 = x1 + block_size;
var z2 = z1 + block_size;

const len = Math.hypot((x1+x2)/2, (z1+z2)/2);
if (len > size/2 - block_size) continue;

projectAndAdd( [x1, 0, z1], [x1, 0, z2], [x2, 0, z2], [x2, 0, z1], true);
}
}
}

function makeDome(x, z, size) {
makeRectBuilding(x, z, size, size/4, true);
}

function makeRectBuilding(x, z, size , height, dome=false) {
const slices_x = dome ? 10 : (2 + random() * 8 | 0);
const slices_y = dome ? (10 * mult) : ((3 + random() * 5 | 0) * height * mult);
const slices_z = dome ? (10 * mult) : ((2 + random() * 8 | 0) * mult);

const astep = Math.PI * 2 / 4;
const sa = Math.PI / 4;// + random() * Math.PI * 2;
const ystep = height / slices_y * size;
for (var i=0; i<4; i++) {
const a = i * astep;
const x1 = x + size * Math.cos(sa + a);
const z1 = z + size * Math.sin(sa + a);
const x2 = x + size * Math.cos(sa + a + astep);
const z2 = z + size * Math.sin(sa + a + astep);

const slices_xz = (i&1) ? slices_z : slices_x;
const xstep = (x2-x1) / slices_xz;
const zstep = (z2-z1) / slices_xz;
for (var k=0; k<slices_xz; k++) {
const xk1 = x1 + k * xstep;
const zk1 = z1 + k * zstep;
const xk2 = xk1 + xstep;
const zk2 = zk1 + zstep;

for (var j=0; j<slices_y; j++) {
const y1 = j * ystep;

[nx0, ny0, nz0] = [xk1, y1, zk1];
[nx1, ny1, nz1] = [xk2, y1, zk2];
[nx2, ny2, nz2] = [xk2, y1 + ystep, zk2];
[nx3, ny3, nz3] = [xk1, y1 + ystep, zk1];
if (dome) {
[nx0, ny0, nz0] = add3(mulf(normalize3(sub3([nx0, ny0, nz0], [x, 0, z])), size), [x, 0, z]);
[nx1, ny1, nz1] = add3(mulf(normalize3(sub3([nx1, ny1, nz1], [x, 0, z])), size), [x, 0, z]);
[nx2, ny2, nz2] = add3(mulf(normalize3(sub3([nx2, ny2, nz2], [x, 0, z])), size), [x, 0, z]);
[nx3, ny3, nz3] = add3(mulf(normalize3(sub3([nx3, ny3, nz3], [x, 0, z])), size), [x, 0, z]);
}

[nx0, ny0, nz0],
[nx1, ny1, nz1],
[nx2, ny2, nz2],
[nx3, ny3, nz3]
);
}
}
}

const xx = [], zz = [];
for (var i=0; i<4; i++) {
xx[i] = x + size * Math.cos(sa + i * astep);
zz[i] = z + size * Math.sin(sa + i * astep);
}
for (var k=0; k<slices_z; k++) {
const xk0 = xx[0] + (xx[3] - xx[0]) / slices_z * k;
const zk0 = zz[0] + (zz[3] - zz[0]) / slices_z * k;
const xk1 = xx[0] + (xx[3] - xx[0]) / slices_z * (k+1);
const zk1 = zz[0] + (zz[3] - zz[0]) / slices_z * (k+1);
const xk2 = xx[1] + (xx[2] - xx[1]) / slices_z * (k+1);
const zk2 = zz[1] + (zz[2] - zz[1]) / slices_z * (k+1);
const xk3 = xx[1] + (xx[2] - xx[1]) / slices_z * k;
const zk3 = zz[1] + (zz[2] - zz[1]) / slices_z * k;

for (var j=0; j<slices_x; j++) {
const xj0 = xk0 + (xk3 - xk0) / slices_x * j;
const zj0 = zk0 + (zk3 - zk0) / slices_x * j;
const xj1 = xk0 + (xk3 - xk0) / slices_x * (j+1);
const zj1 = zk0 + (zk3 - zk0) / slices_x * (j+1);
const xj2 = xk1 + (xk2 - xk1) / slices_x * (j+1);
const zj2 = zk1 + (zk2 - zk1) / slices_x * (j+1);
const xj3 = xk1 + (xk2 - xk1) / slices_x * j;
const zj3 = zk1 + (zk2 - zk1) / slices_x * j;

[nx0, ny0, nz0] = [xj0, height * size, zj0];
[nx1, ny1, nz1] = [xj1, height * size, zj1];
[nx2, ny2, nz2] = [xj2, height * size, zj2];
[nx3, ny3, nz3] = [xj3, height * size, zj3];
if (dome) {
[nx0, ny0, nz0] = add3(mulf(normalize3(sub3([nx0, ny0, nz0], [x, 0, z])), size), [x, 0, z]);
[nx1, ny1, nz1] = add3(mulf(normalize3(sub3([nx1, ny1, nz1], [x, 0, z])), size), [x, 0, z]);
[nx2, ny2, nz2] = add3(mulf(normalize3(sub3([nx2, ny2, nz2], [x, 0, z])), size), [x, 0, z]);
[nx3, ny3, nz3] = add3(mulf(normalize3(sub3([nx3, ny3, nz3], [x, 0, z])), size), [x, 0, z]);
}

[nx0, ny0, nz0],
[nx1, ny1, nz1],
[nx2, ny2, nz2],
[nx3, ny3, nz3]
);
}
}
}

function makeOddBuilding(x, z, size , height) {
const choices = [3, 5, 6, 8, 12];
const sides = choices[random() * choices.length | 0];
const slices_xz = (2 + random() * 2 | 0) * mult;
const slices_y = (3 + random() * 2 | 0) * height * mult;
const slices_r = 2 + random() * 2 | 0;

const astep = Math.PI * 2 / sides;
const sa = Math.PI / 4 + random() * Math.PI * 2;
const ystep = height / slices_y * size;
for (var i=0; i<sides; i++) {
const a = i * astep;
const x1 = x + size * Math.cos(sa + a);
const z1 = z + size * Math.sin(sa + a);
const x2 = x + size * Math.cos(sa + a + astep);
const z2 = z + size * Math.sin(sa + a + astep);

const xstep = (x2-x1) / slices_xz;
const zstep = (z2-z1) / slices_xz;
for (var k=0; k<slices_xz; k++) {
const xk1 = x1 + k * xstep;
const zk1 = z1 + k * zstep;
const xk2 = xk1 + xstep;
const zk2 = zk1 + zstep;

for (var j=0; j<slices_y; j++) {
const y1 = j * ystep;

[xk1, y1, zk1],
[xk2, y1, zk2],
[xk2, y1 + ystep, zk2],
[xk1, y1 + ystep, zk1]
);
}
}
}

for (var i=0; i<sides; i++) {
const a0 = i * astep;
const a1 = (i+1) * astep;
const x0 = x + size * Math.cos(sa + a0);
const z0 = z + size * Math.sin(sa + a0);
const x1 = x + size * Math.cos(sa + a1);
const z1 = z + size * Math.sin(sa + a1);

const slices_j = slices_y / 2 | 0;
for (var j=0; j<slices_j; j++) {
const xj0 = x0 + (x - x0) / slices_j * j;
const zj0 = z0 + (z - z0) / slices_j * j;
const xj1 = x0 + (x - x0) / slices_j * (j+1);
const zj1 = z0 + (z - z0) / slices_j * (j+1);
const xj2 = x1 + (x - x1) / slices_j * (j+1);
const zj2 = z1 + (z - z1) / slices_j * (j+1);
const xj3 = x1 + (x - x1) / slices_j * j;
const zj3 = z1 + (z - z1) / slices_j * j;

for (var k=0; k<slices_xz; k++) {
const xk0 = xj0 + (xj3 - xj0) / slices_xz * k;
const zk0 = zj0 + (zj3 - zj0) / slices_xz * k;
const xk1 = xj0 + (xj3 - xj0) / slices_xz * (k+1);
const zk1 = zj0 + (zj3 - zj0) / slices_xz * (k+1);
const xk2 = xj1 + (xj2 - xj1) / slices_xz * (k+1);
const zk2 = zj1 + (zj2 - zj1) / slices_xz * (k+1);
const xk3 = xj1 + (xj2 - xj1) / slices_xz * k;
const zk3 = zj1 + (zj2 - zj1) / slices_xz * k;

[xk0, size * (height + height * .25 / slices_j * j), zk0],
[xk1, size * (height + height * .25 / slices_j * j), zk1],
[xk2, size * (height + height * .25 / slices_j * (j+1)), zk2],
[xk3, size * (height + height * .25 / slices_j * (j+1)), zk3]
);
}
}
}
}

function drawSky() {
const p1 = polygons.create();
const dpoints = [[-110,-110],[110,-110],[110,110],[-110,110]];
polygons.draw(turtle, p1, true);
}

function rotX(x, y, a) { return Math.cos(a) * x - Math.sin(a) * y; }
function rotY(x, y, a) { return Math.sin(a) * x + Math.cos(a) * y; }

function drawPoints(dpoints, hatching=0, angle=Math.PI/4) {
var good = true;
dpoints.forEach(p => good &= Math.min(Math.abs(p[0]), Math.abs(p[1])) < 100 );
if (!good) return;

if (style == 0) {
turtle.jump(dpoints[dpoints.length-1]);
dpoints.forEach(p=>turtle.goto(p));
} else {
const p1 = polygons.create();
if (style == 2 && !hatching) {
} else {
p1.dp.push(p1.cp[0], p1.cp[1]);
p1.dp.push(p1.cp[2], p1.cp[3]);
}
polygons.draw(turtle, p1, true);
}
}

function rotV(v, k, a) {
const c = Math.cos(a);
const s = Math.sin(a);
return add3( add3( mulf(v, c), mulf(cross3(k, v), s) ), mulf(k, dot3(k, v) * (1-c)) );
}

function project(op) {
const p = transform4([op[0], op[2], op[1], 1], viewProjectionMatrix);
const s = 5 * perspective **3;
if (p[2] < 0) return [];
return [ p[0]/p[3]*s, -p[1]/p[3]*s, p[2] ];
}

////////////////////////////////////////////////////////////////
// Projection from reinder's https://turtletoy.net/turtle/b3acf08303
let viewProjectionMatrix;
function setupCamera(t,e){const m=lookAt4m(t,e,[0,0,1]),n=perspective4m(.25,1);return multiply4m(n,m)}
function lookAt4m(o,n,r){const s=new Float32Array(16);n=normalize3(sub3(o,n)),r=normalize3(cross3(r,n));const t=normalize3(cross3(n,r));return s[0]=r[0],s[1]=t[0],s[2]=n[0],s[3]=0,s[4]=r[1],s[5]=t[1],s[6]=n[1],s[7]=0,s[8]=r[2],s[9]=t[2],s[10]=n[2],s[11]=0,s[12]=-(r[0]*o[0]+r[1]*o[1]+r[2]*o[2]),s[13]=-(t[0]*o[0]+t[1]*o[1]+t[2]*o[2]),s[14]=-(n[0]*o[0]+n[1]*o[1]+n[2]*o[2]),s[15]=1,s}
function perspective4m(t,n){const e=new Float32Array(16).fill(0,0);return e[5]=1/Math.tan(t/2),e[0]=e[5]/n,e[10]=e[11]=-1,e}
function multiply4m(t,r){const l=new Float32Array(16);for(let n=0;16>n;n+=4)for(let o=0;4>o;o++)l[n+o]=r[n+0]*t[0+o]+r[n+1]*t[4+o]+r[n+2]*t[8+o]+r[n+3]*t[12+o];return l}
function transform4(r,n){const t=new Float32Array(4);for(let o=0;4>o;o++)t[o]=n[o]*r[0]+n[o+4]*r[1]+n[o+8]*r[2]+n[o+12];return t}

function normalize3(a) { const len = len3(a); return scale3(a,len3<0.0001?1:1/len); }
function scale3(a,b) { return [a[0]*b,a[1]*b,a[2]*b]; }
function len3(a) { return Math.sqrt(dot3(a,a)); }
function add3(a,b) { return [a[0]+b[0],a[1]+b[1],a[2]+b[2]]; }
function sub3(a,b) { return [a[0]-b[0],a[1]-b[1],a[2]-b[2]]; }
function dot3(a,b) { return a[0]*b[0]+a[1]*b[1]+a[2]*b[2]; }
function cross3(a,b) { return [a[1]*b[2]-a[2]*b[1],a[2]*b[0]-a[0]*b[2],a[0]*b[1]-a[1]*b[0]]; }
function mulf(v, f) { return [v[0]*f,v[1]*f,v[2]*f]; }

// Random with seed
var rng;
function random() { if (rng === undefined) rng = new RNG(seed_t); return rng.nextFloat(); }
function RNG(t){return new class{constructor(t){this.m=2147483648,this.a=1103515245,this.c=12345,this.state=t||Math.floor(Math.random()*(this.m-1))}nextFloat(){return this.state=(this.a*this.state+this.c)%this.m,this.state/(this.m-1)}}(t)}

////////////////////////////////////////////////////////////////
// Polygon Clipping utility code - Created by Reinder Nijhoff 2019
// (Polygon binning by Lionel Lemarie 2021)
// https://turtletoy.net/turtle/a5befa1f8d
////////////////////////////////////////////////////////////////
function Polygons() {
const fullPolygonList = [];
const bin_size = 50;
const bins = Array.from({length: bin_size**2}, (_) => []);
const Polygon = class {
constructor() {
this.cp = [];       // clip path: array of [x,y] pairs
this.dp = [];       // 2d lines [x0,y0],[x1,y1] to draw
this.aabb = [];     // AABB bounding box
}
// add point to clip path and update bounding box
let xmin = 1e5, xmax = -1e5, ymin = 1e5, ymax = -1e5;
(this.cp = [...this.cp, ...points]).forEach( p => {
xmin = Math.min(xmin, p[0]), xmax = Math.max(xmax, p[0]);
ymin = Math.min(ymin, p[1]), ymax = Math.max(ymax, p[1]);
});
this.aabb = [xmin, ymin, xmax, ymax];
}
// add segments (each a pair of points)
points.forEach(p => this.dp.push(p));
}
for (let i = 0, l = this.cp.length; i < l; i++) {
this.dp.push(this.cp[i], this.cp[(i + 1) % l]);
}
}
draw(t) {
for (let i = 0, l = this.dp.length; i < l; i+=2) {
t.jump(this.dp[i]), t.goto(this.dp[i + 1]);
}
}
const tp = new Polygon();
tp.cp.push([-1e5,-1e5],[1e5,-1e5],[1e5,1e5],[-1e5,1e5]);
const dx = Math.sin(a) * d,   dy = Math.cos(a) * d;
const cx = Math.sin(a) * 200, cy = Math.cos(a) * 200;
for (let i = 0.5; i < 150 / d; i++) {
tp.dp.push([dx * i + cy,   dy * i - cx], [dx * i - cy,   dy * i + cx]);
tp.dp.push([-dx * i + cy, -dy * i - cx], [-dx * i - cy, -dy * i + cx]);
}
tp.boolean(this, false);
this.dp = [...this.dp, ...tp.dp];
}
inside(p) {
let int = 0; // find number of i ntersection points from p to far away
for (let i = 0, l = this.cp.length; i < l; i++) {
if (this.segment_intersect(p, [0.1, -1000], this.cp[i], this.cp[(i + 1) % l])) {
int++;
}
}
return int & 1; // if even your outside
}
boolean(p, diff = true) {
// polygon diff algorithm (narrow phase)
const ndp = [];
for (let i = 0, l = this.dp.length; i < l; i+=2) {
const ls0 = this.dp[i];
const ls1 = this.dp[i + 1];
// find all intersections with clip path
const int = [];
for (let j = 0, cl = p.cp.length; j < cl; j++) {
const pint = this.segment_intersect(ls0, ls1, p.cp[j], p.cp[(j + 1) % cl]);
if (pint !== false) {
int.push(pint);
}
}
if (int.length === 0) {
// 0 intersections, inside or outside?
if (diff === !p.inside(ls0)) {
ndp.push(ls0, ls1);
}
} else {
int.push(ls0, ls1);
// order intersection points on line ls.p1 to ls.p2
const cmpx = ls1[0] - ls0[0];
const cmpy = ls1[1] - ls0[1];
int.sort( (a,b) =>  (a[0] - ls0[0]) * cmpx + (a[1] - ls0[1]) * cmpy -
(b[0] - ls0[0]) * cmpx - (b[1] - ls0[1]) * cmpy);

for (let j = 0; j < int.length - 1; j++) {
if ((int[j][0] - int[j+1][0])**2 + (int[j][1] - int[j+1][1])**2 >= 0.001) {
if (diff === !p.inside([(int[j][0]+int[j+1][0])/2,(int[j][1]+int[j+1][1])/2])) {
ndp.push(int[j], int[j+1]);
}
}
}
}
}
return (this.dp = ndp).length > 0;
}
//port of http://paulbourke.net/geometry/pointlineplane/Helpers.cs
segment_intersect(l1p1, l1p2, l2p1, l2p2) {
const d   = (l2p2[1] - l2p1[1]) * (l1p2[0] - l1p1[0]) - (l2p2[0] - l2p1[0]) * (l1p2[1] - l1p1[1]);
if (d === 0) return false;
const n_a = (l2p2[0] - l2p1[0]) * (l1p1[1] - l2p1[1]) - (l2p2[1] - l2p1[1]) * (l1p1[0] - l2p1[0]);
const n_b = (l1p2[0] - l1p1[0]) * (l1p1[1] - l2p1[1]) - (l1p2[1] - l1p1[1]) * (l1p1[0] - l2p1[0]);
const ua = n_a / d;
const ub = n_b / d;
if (ua >= 0 && ua <= 1 && ub >= 0 && ub <= 1) {
return [l1p1[0] + ua * (l1p2[0] - l1p1[0]), l1p1[1] + ua * (l1p2[1] - l1p1[1])];
}
return false;
}
};
function getBinnedPolygonList(aabb) {
const indexList = {};
const width = 200 / bin_size;
for (var bi=0; bi<bin_size; bi++) {
const y = bi * width - 100;
const b_aabb = [0, y, 200, y + width];
if (aabb[3]<b_aabb[1] || aabb[1]>b_aabb[3]) continue;
for (var bj=0; bj<bin_size; bj++) {
const x = bj * width - 100;
b_aabb[0] = x, b_aabb[2] = x + width;
if (aabb[0]>b_aabb[2] || aabb[2]<b_aabb[0]) continue;
bins[bj + bi * bin_size].forEach(id => {
const p = fullPolygonList[id];
if (!(aabb[3]<p.aabb[1] || aabb[1]>p.aabb[3] || aabb[0]>p.aabb[2] || aabb[2]<p.aabb[0])) {
indexList[id] = 1;
}
});
}
}
const reducedPolygonList = Array.from(Object.keys(indexList), id => fullPolygonList[id]);
return reducedPolygonList;
};
fullPolygonList.push(p);
const id = fullPolygonList.length - 1, width = 200 / bin_size;
bins.forEach((b,i) => {
const x = (i % bin_size) * width - 100;
const y = (i / bin_size | 0)  * width - 100;
const aabb = [x, y, x + width, y + width];
if (!(aabb[3]<p.aabb[1] || aabb[1]>p.aabb[3] || aabb[0]>p.aabb[2] || aabb[2]<p.aabb[0])) {
b.push(id);
}
});
};
return {
list: () => fullPolygonList,
create: () => new Polygon(),
draw: (turtle, p, addToVisList=true) => {
reducedPolygonList = getBinnedPolygonList(p.aabb);
for (let j = 0; j < reducedPolygonList.length && p.boolean(reducedPolygonList[j]); j++);
p.draw(turtle);