Fork of Rock Island | 🏝️
- removed the PoissonDisc usage
- added the grid that is used in this turtle Grid variations by @reinder
This grid allows two shapes; squares / hexagons, which are randomly spit and distorted. Examples:
- Hexagons: Rock Island || 🏝️ (variation)
- Squares: Rock Island || 🏝️ (variation)
Log in to post a comment.
// Forked from "Rock Island 🏝️" by markknol
// https://turtletoy.net/turtle/68ef188d1f
// 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 seed = 1; // min=1, max=100, step=1
const grid = 15; // min=7, max=30, step=1
const randomDistortion = .5; // min=0, max=1, step=0.01
const spacing = 0.15; // min=0.0, max=1.0, step=0.01
const splitChange = .25; // min=0, max=1, step=0.01
let groups = [];
let polygons = new Polygons();
function init() {
for (let i = 0; i<grid*grid; i++) {
const scale = 150 / grid;
const x = (i % grid) - (grid/2|0);
const y = (i/grid|0) - (grid/2|0);
// create base shape
const baseShape = 1; // min=0, max=1, step=1
let points = baseShape ? createHexagon(x, y) : createSquare(x, y);
// give vertices random offset
points = randomizePoints(points, randomDistortion);
// shatter shape into shapes
const shapes = shatterPoints(points);
// for each shape: repeatedly draw and 'twist'
shapes.forEach( shapePoints => {
let points = shapePoints.map(point => scl(point, scale));
let mid = [0, 0];
points.forEach(p => {
mid[0] += p[0] / points.length;
mid[1] += p[1] / points.length;
});
points = points.map(p => add(scl(sub(p,mid), 0.3 + (1 - spacing) * 0.7), mid));
let mid2 = [0, 0];
points.forEach(p => {
mid2[0] += p[0] / points.length;
mid2[1] += p[1] / points.length;
});
points.sort((a, b) => (angle(mid2,a) - Math.PI) - (angle(mid2,b) - Math.PI));
groups.push({group:points, mid:mid2});
});
}
groups.sort((a,b) => a.mid[1] - b.mid[1]);
}
// The walk function will be called until it returns false.
function walk(i, anim) {
if (i == 0) init();
if (groups.length == 0) return;
let g = groups.pop();
let group = g.group;
let h = Math.min(distance([0, 0], group[0]) / (85), 1);
h = 1 - Math.pow(1 - h, 1 + rand());
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]]
);
const v = [p2[0] - p1[0], p2[1] - p1[1]];
const length = len(v);
const normal = [v[1] / length, -v[0] / len(v)];
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;
// 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;
}
// shape generators
function createSquare(x,y) {
return [[x-.5,y+.5], [x-.5,y-.5], [x+.5,y-.5], [x+.5,y+.5]];
}
function createHexagon(x,y) {
const h = .75, w = 3/8 * (h/(Math.sqrt(3)/4)), center = [x*2*w + (y%2)*w, y*3/2*h];
return [[0,-h],[-w,-h/2],[-w,h/2],[0,h],[w,h/2],[w,-h/2]].map(p => add(center, p));
}
// vec2 functions
function scl(a,b) { return [a[0]*b, a[1]*b]; }
function add(a,b) { return [a[0]+b[0], a[1]+b[1]]; }
function sub(a,b) { return [a[0]-b[0], a[1]-b[1]]; }
function dot(a,b) { return a[0]*b[0] + a[1]*b[1]; }
function len(a) { return Math.sqrt(a[0]**2 + a[1]**2); }
function nrm(a) { return scl(a, 1/len(a)); }
function lrp(a,b,f) { return [a[0]*f+b[0]*(1-f), a[1]*f+b[1]*(1-f)]; }
function angle(a, b) { return Math.atan2(a[1] - b[1], a[0] - b[0]); }
function distance(a, b) { return len(sub(a, b)) };
// shape functions
function randomizePoints(points, scale = 1) {
return points.map( point => add(point,
scl( add( [hash(dot(point, [11, 13])), hash(dot(point, [17, 19]))], [-.5,-.5]), scale)));
}
function shatterPoints(points) {
if (points.length <= 3) return [points];
// calculate angle at each vertex and sort them
const l = points.length, angles = points.map( (p, i) => {
return [dot( nrm( sub(points[(i+1)%l],p)), nrm( sub( points[(i+l-1)%l],p))), i];
}).sort((a,b) => a[0]-b[0]);
// split at vertex with largest angle or just randomly
if (rand() < splitChange) {
const i = angles[0][1], d = 3 + rand()*(l-4)|0, p2 = [...points, ...points];
return [...shatterPoints([...p2].splice(i, d)), ...shatterPoints([...p2].splice(i+d-1, l-d+2))];
} else {
return [points];
}
}
// pseudo random methods
function hash(p) {
p += seed;
p = 1103515245 * (((p) >> 1) ^ (p));
p = 1103515245 * (p ^ (p>>3));
p = p ^ (p >> 16);
return p / 1103515245 % 1;
}
let rseed = seed;
function rand() {
rseed = 1103515245 * (((rseed) >> 1) ^ (rseed));
rseed = 1103515245 * (rseed ^ (rseed>>3));
rseed = rseed ^ (rseed >> 16);
return rseed / 1103515245 % 1;
}
////////////////////////////////////////////////////////////////
// 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);
}
};
}