Delaunay triangulation with some hatching
#triangulation
Log in to post a comment.
const seed = 250; // min=1, max=500, step=1 const size = 100; // min=50, max=150, step=5 const amount = 200; // min=10, max=1000, step=5 const hatching = 0.5; // min=0.0, max=1.0, step=0.05 const outlines = 0.5; // min=0.0, max=1.0, step=0.05 const density = 0.85; // min=0, max=1, step=0.05 const uniform = 0.8; // min=0, max=1, step=0.05 const destroyed = 0.1; // min=0, max=1, step=0.05 const shape = 2; // min=0, max=3, step=1 (Raw, Square, Rounded, Round) const turtle = new Turtle(); const polygons = new Polygons(); const random = new Random(seed); const delaunay = new Delaunay(); const sizeSqrt = Math.ceil(Math.sqrt(amount)); function walk(step = 0) { if (step === 0) { let input = [...Array(amount).keys()].map(p => [ -size + ((p % sizeSqrt) / sizeSqrt) * size * 2, -size + ((p / sizeSqrt | 0) / sizeSqrt) * size * 2, ]); random.shuffle(input); input = input.filter(p => random.next() > destroyed / 2); input = input.filter(p => random.next() > destroyed / 2); input = input.filter(p => random.next() > destroyed / 2); input = input.filter(p => random.next() > destroyed / 2); input = input.filter(p => random.next() > destroyed / 2); if (shape == 1) { input.unshift([-size,-size]); input.unshift([size,-size]); input.unshift([size,size]); input.unshift([-size,size]); } else if (shape == 2 || shape == 3) { input = input.filter(p => p[0] * p[0] + p[1] * p[1] < (size * size)); const total = Math.max(amount - input.length, 20); if (shape == 3) for(let i=0;i<total;i++) { const a = i/total*Math.PI*2; input.unshift([Math.sin(a)*size,Math.cos(a)*size]); } } const spread = (1 - uniform) * size / 2; input.forEach(p => { const r = (shape == 0) ? random.next() : Math.max(0, Math.min(1, 1 - (p[0] * p[0] + p[1] * p[1]) / (size * size))) p[0] += random.range(-spread, spread) * r; p[1] += random.range(-spread, spread) * r; }) random.shuffle(input); delaunay.compute(input); } let idx = step * 3; let { indices, points } = delaunay; let p0 = points[indices[idx + 0]]; let p1 = points[indices[idx + 1]]; let p2 = points[indices[idx + 2]]; if (!p0 || !p1 || !p2) return false; const angle = Math.atan2(p0[1]-p1[1]-p2[1], p0[0]-p1[0]-p2[0]); if (outlines === 1 && hatching === 0) { if (random.next() < density) { turtle.jump(p0); turtle.goto(p1); turtle.goto(p2); turtle.goto(p0); } } else { const triangle = polygons.create(); triangle.addPoints(p0, p1, p2); if (random.next() < outlines) triangle.addOutline(); if (random.next() < hatching) triangle.addHatching(angle, (0.25 + (idx / indices.length) * 1.5) * lerp(1.0, 0.75, Math.min(amount, 1000) / 1000)); if (random.next() < density) polygons.draw(turtle, triangle); } return true; } function add2(p, v) { return [p[0] + v[0], p[1] + v[1]]; } function scl2(p, x, y = x) { return [p[0] * x, p[1] * y]; } function lerp(a,b,t) { return a + (b-a) * t; } function Delaunay() { // Ported from some output of my old Haxe project class Delaunay { constructor() { } compute(points) { this.points = points; let t = 0, n=0, c=0; let s = points.length; if (s < 3) return null; let e = 1e9; points.push([0, -e]); points.push([e, e]); points.push([-e, e]); this.indices = [points.length - 3, points.length - 2, points.length - 1]; this.circles = [0, 0, e]; for (let h = [], t = 0, n = 0, c = 0, r = 0; r < s; ) { let l = r++, p = 0; if (0 < this.indices.length) for (;;) { let d = points[l]; let a = this.circles[p] - d[0]; let o = this.circles[p + 1] - d[1]; let f = this.circles[p + 2] > a * a + o * o; if ( (f && ((t = this.indices[p]), (n = this.indices[p + 1]), (c = this.indices[p + 2]), h.push(t), h.push(n), h.push(n), h.push(c), h.push(c), h.push(t), this.indices.splice(p, 3), this.circles.splice(p, 3), (p -= 3)), !((p += 3) < this.indices.length)) ) break; } let u = 0; if (0 < h.length) for (;;) { let g = u + 2; if (g < h.length) for (;;) { if ( (h[u] == h[g] && h[u + 1] == h[g + 1]) || (h[u + 1] == h[g] && h[u] == h[g + 1]) ) { if ((h.splice(g, 2), h.splice(u, 2), (g -= 2), (u -= 2) < 0)) break; if (g < 0) break; } if (!((g += 2) < h.length)) break; } if (!((u += 2) < h.length)) break; } let v = 0; if (0 < h.length) for (;;) { this.indices.push(h[v]); this.indices.push(h[v + 1]); this.indices.push(l); let x = points[h[v]], y = points[h[v + 1]], b = points[l], k = y[0] - x[0], T = y[1] - x[1], m = b[0] - x[0], j = b[1] - x[1], q = k * (x[0] + y[0]) + T * (x[1] + y[1]), w = m * (x[0] + b[0]) + j * (x[1] + b[1]), z = 2 * (k * (b[1] - y[1]) - T * (b[0] - y[0])), A = (j * q - T * w) / z; this.circles.push(A); let B = (k * w - m * q) / z; if ( (this.circles.push(B), (A -= x[0]), (B -= x[1]), this.circles.push(A * A + B * B), !((v += 2) < h.length)) ) break; } for (; 0 != h.length; ) h.pop(); } (t = points.length - 3), (n = points.length - 2), (c = points.length - 1); let C = 0; if (0 < this.indices.length) for (;;) { if ( this.indices[C] == t || this.indices[C] == n || this.indices[C] == c || this.indices[C + 1] == t || this.indices[C + 1] == n || this.indices[C + 1] == c || this.indices[C + 2] == t || this.indices[C + 2] == n || this.indices[C + 2] == c ) { if ( (this.indices.splice(C, 3), (C -= 3), (C += 3) < this.indices.length) ) continue; break; } if (!((C += 3) < this.indices.length)) break; } return points.pop(), points.pop(), points.pop(), this.indices; } } return new Delaunay(); } // Seeded random - Mulberry32 function Random(seed) { class Random { constructor(seed) { this.seed = seed; } next() { var t = this.seed += 0x6D2B79F5; t = Math.imul(t ^ t >>> 15, t | 1); t ^= t + Math.imul(t ^ t >>> 7, t | 61); return ((t ^ t >>> 14) >>> 0) / 4294967296; } range(from, to) { var r = this.next(); return from + (to - from) * r; } either(a = 0, b = 1, chance = 0.5) { return this.next() > chance ? a : b; } shuffle(arr) { for (let i = arr.length - 1; i > 0; i--) { const j = Math.floor(this.next() * (i + 1)); [arr[i], arr[j]] = [arr[j], arr[i]]; } return arr; } } return new Random(seed); } //////////////////////////////////////////////////////////////// // Polygon Clipping utility code - Created by Reinder Nijhoff 2019 // https://turtletoy.net/turtle/a5befa1f8d //////////////////////////////////////////////////////////////// function Polygons() { const polygonList = []; 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 } addPoints(...points) { // 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+xmax)/2, (ymin+ymax)/2, (xmax-xmin)/2, (ymax-ymin)/2]; } addSegments(...points) { // add segments (each a pair of points) points.forEach(p => this.dp.push(p)); } addOutline() { 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]); } } addHatching(a, d) { 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) { // bouding box optimization by ge1doot. if (Math.abs(this.aabb[0] - p.aabb[0]) - (p.aabb[2] + this.aabb[2]) >= 0 && Math.abs(this.aabb[1] - p.aabb[1]) - (p.aabb[3] + this.aabb[3]) >= 0) return this.dp.length > 0; // 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; } }; return { list: () => polygonList, create: () => new Polygon(), draw: (turtle, p, addToVisList=true) => { for (let j = 0; j < polygonList.length && p.boolean(polygonList[j]); j++); p.draw(turtle); if (addToVisList) polygonList.push(p); } }; }