Rock Island | 🏝️

Inspired by twitter.com/balidani/status/1215105239361163264

How it kinda works: (use the "step" slider to see this process 😃)
1. generate points in circle using poisson disc util Poisson- Disc Grow Patterns
2. grab group of points, make shapes
3. for each segment of shape, draw rectangle kind of shape upwards
4. use polygon util for clipping , draw from front to back Stars
5. use hatching for lighting

Thanks to @reinder for utils and help on this one!

Log in to post a comment.

const turtle = new Turtle();
let polygons = new Polygons();

const step = 5; // min=1, max=5, step=1
const density = 8; // min=4, max=10, step=1
const shape = 15; // min=10, max=20, step=1
const radius = 500; // min=100, max=3000, step=100

const seed = 2;// min=2, max=200, step=1

let groups;
let points;
let random;

function init() {
	random = new Random(seed);
    groups = [];
    points = new PoissonDisc(random, [[0, 0]], density).addPoints(radius);
    polygons = new Polygons();
    const takenPoints = [];
    loop1: for (let p1 of points) {
    	if (takenPoints.indexOf(p1) < 0) {
    		var group = [];
    		loop2: for (let p2 of points) {
    			if (takenPoints.indexOf(p2) < 0) {
    				if (distance(p1, p2) < shape) {
    					group.push(p2);
    	                if (takenPoints.indexOf(p1) < 0) {
    		                takenPoints.push(p1);
    	                }
    					takenPoints.push(p2);
    					if (group.length >= 10) {
    		                break loop2;
    		            }
    				}
    			}
    		}
    		if (group.length > 2) {
    			var mid = [0, 0];
    			for (let p of group) {
    				mid[0] += p[0];
    				mid[1] += p[1];
    			}
    			mid[0] /= group.length;
    			mid[1] /= group.length;
    			group.sort((a, b) => (angle(mid,a) - Math.PI) - (angle(mid,b) - Math.PI));
    		
    
    			groups.push({group: group, mid:mid});
    		}
    	}
    }
    
    groups.sort((a, b) => a.mid[1] - b.mid[1]);
}

// let animation = 0; //min=0, max=1, step=0.01
function walk(i, anim) {
    // if (anim === 1) anim = animation;
    
    if (i == 0) init();
    
	let _step = 1;
	switch (step) {
		case _step++: {
			if (points[i][2]) {
				turtle.jump(points[i]);
				turtle.circle(0.4);
			}
			return i < points.length - 1;
		}

		case _step++: {
	    	if (points[i][2]) {
				turtle.jump(points[i]);
				turtle.circle(0.4);
			}
			let g = groups.pop();
			if (g != null) {
			    let group = g.group;
    			for (let j = 0; j < group.length; j++) {
    				let p1 = group[j];
    				let p2 = group[(j + 1) % group.length];
    
    				turtle.jump(p2[0], p2[1]);
    				turtle.goto(p1[0], p1[1]);
    				turtle.goto(p2[0], p2[1]);
    			}
    			let p = group[0];
    			turtle.goto(p[0], p[1]);
			}
			return groups.length > 0 || i < points.length - 1;
		}
		case _step++: {
			let g = groups.pop();
			if (g) {
    			let group = g.group;
			
    			let h = Math.min(distance([0, 0], group[0]) / (85), 1);
    			h = 1 - Math.pow(1 - h, 1 + random.next());
    			h = 1 - h;
    			h *= 100;
    			h += 3;
    			
    			group.forEach(p => {
    				p[1] += 45;
    				p[1] /= 2;
    			});
    
    			for (let j = 0; j < group.length; j++) {
    				let p1 = group[j];
    				let p2 = group[(j + 1) % group.length];
    
    				turtle.jump(p2[0], p2[1]);
    				turtle.goto(p2[0], p2[1] - h);
    				turtle.goto(p1[0], p1[1] - h);
    				turtle.goto(p1[0], p1[1]);
    				turtle.goto(p2[0], p2[1]);
    			}
    			let p = group[0];
    			turtle.goto(p[0], p[1]);
			}
			return groups.length > 0;
		}
		
        case _step++:
		case _step++: {
			let g = groups.pop();
			if (g ) {
    			let group = g.group;
    			if (g.mid[0]>-100 && g.mid[0]<100) {
    			    
        			let h = Math.min(distance([0, 0], group[0]) / (85), 1);
        			h = 1 - Math.pow(1 - h, 1 + random.next());
        			h -= (0.5 + Math.sin(groups.length + anim*Math.PI*2)/2) / 6 * (1-anim);
        			h = 1 - h;
        			h *= 100;
        			h += 3;
        
        			group.forEach(p => {
        				p[1] += 45;
        				p[1] /= 2;
        			});
        			const roofShape = polygons.create();
	                group.forEach((_, j) => roofShape.addPoints([group[j][0], group[j][1] - h]) );
        			roofShape.addOutline();
        			polygons.draw(turtle, roofShape, true);
        
                    const segments = [];
                    group.forEach((_, i) => segments.push( [group[i], group[(i+1) % group.length]] ));
                    segments.sort( (s0, s1) => Math.max(s1[0][1], s1[1][1]) - Math.max(s0[0][1], s0[1][1]));
                    segments.forEach( segment => {
                    	const p1 = segment[0];
                    	const p2 = segment[1];
    
                        if ( p2[0]-p1[0] < 0 ) {
                        	const shape = polygons.create();
            				shape.addPoints(
            					[p2[0], p2[1]],
            					[p2[0], p2[1] - h],
            					[p1[0], p1[1] - h],
            					[p1[0], p1[1]],
            					[p2[0], p2[1]]
            				);
            
                            if (step > 4) {
                				const v = [p2[0] - p1[0], p2[1] - p1[1]];
                				const length = distance(v);
                				const normal = [v[1] / length, -v[0] / length];
                				let lightAngle = 2.1; // min=0, max=7, step=0.01
                				lightAngle += anim * Math.PI*2;
                				const lightDirection = [Math.sin(lightAngle), Math.cos(lightAngle)];
                				const diff = lightDirection[0] * normal[0] + lightDirection[1] * normal[1];
                			    let hatching = 0.2 + (0.5 + diff * 0.5) * 2; // 0.2 - 2
                				
                				// make light parts super light
            					if (hatching > 1.4) {
                				    let d = (hatching - 1.4) * 2;
                				    const ease = t => Math.pow(2, 10.0 * (t - 1.0));
                				    hatching = hatching-0.5 + ease(d) * 0.5;
                				}
                				if (hatching < 9) {
            				        shape.addHatching(Math.PI/4, hatching);
                				}
                            }
            				shape.addOutline();
            
            				polygons.draw(turtle, shape, true);
                        }
                    });
        
    			}
			}
			return groups.length > 0;
		}
	}
}


function modulo(i,  n) {
    // positive modulo
    return (n + (i % n)) % n;
}

const __EMPTY_ = [0,0];
function distance(p1, p2 = __EMPTY_) {
	var delta = [p1[0] - p2[0], p1[1] - p2[1]];
	return Math.sqrt(delta[0] * delta[0] + delta[1] * delta[1]);
}

function angle(a, b) {
	return Math.atan2(a[1] - b[1], a[0] - b[0]);
}


// 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;
        }
        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);
}


// Forked from "Poisson- Disc Grow Patterns" by reinder
// https://turtletoy.net/turtle/b5510898dc

////////////////////////////////////////////////////////////////
// Poisson-Disc utility code. Created by Reinder Nijhoff 2019
// https://turtletoy.net/turtle/b5510898dc
////////////////////////////////////////////////////////////////
function PoissonDisc(random, startPoints, radius) {
    class PoissonDiscGrid {
        constructor(sp, radius) {
            this.cellSize = 1/Math.sqrt(2)/radius;
            this.radius2 = radius*radius;
            this.cells = [];
            sp.forEach( p => this.insert(p) );
        }
        insert(p) {
            const x = p[0]*this.cellSize|0, y=p[1]*this.cellSize|0;
            for (let xi = x-1; xi<=x+1; xi++) {
                for (let yi = y-1; yi<=y+1; yi++) {
                    const ps = this.cell(xi,yi);
                    for (let i=0; i<ps.length; i++) {
                        if ((ps[i][0]-p[0])**2 + (ps[i][1]-p[1])**2 < this.radius2) {
                            return false;
                        }
                    }
                }       
            }
            this.cell(x, y).push(p);
            return true;
        }
        cell(x,y) {
            const c = this.cells;
            return (c[x]?c[x]:c[x]=[])[y]?c[x][y]:c[x][y]=[];
        }
    }
    class PoissonDisc {
        constructor(random, sp, radius) {
            this.result = [...sp];
            this.active = [...sp];
            this.grid = new PoissonDiscGrid(sp, radius);
            this.random = random;
        }
        addPoints(count, maxTries=16, loosePacking=0, randomGrowOrder=0) {
        	mainLoop: while (this.active.length > 0 && count > 0) {
        		const index = (this.random.next() * this.active.length * randomGrowOrder) | 0;
        		const point = this.active[index];
        		for (let i=0; i < maxTries; i++) {
        			const a = this.random.next() * 2 * Math.PI;
        			const d = (this.random.next()*loosePacking + 1) * radius;
        			const p = [point[0] + Math.cos(a)*d, point[1] + Math.sin(a)*d, point];
        			if (this.grid.insert(p)) {
            			this.result.push(p);
            			this.active.push(p);
            			count--;
            			continue mainLoop;
        			}
        		}
    		    this.active.splice(index, 1);
        	}
        	return this.result;
        }
    }
    return new PoissonDisc(random, startPoints, radius);
}


////////////////////////////////////////////////////////////////
// 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);
		}
	};
}