### Isometric alien art

Fork of
Rock Island || 🏝️
and
Alien art: (x ^ y) % 9 by @llemarie
which uses a formula from twitter.com/aemkei/status/1378106731386040322

```// Forked from "Rock Island || 🏝️" by markknol
// https://turtletoy.net/turtle/deca53a32d

// 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 grid = 100; // min=7, max=175, step=1
const thickness = 1.0; // min=0.0, max=1.0, step=0.01
const minHeight = 4; // min=2.0, max=40.0, step=1
const maxHeight = 20; // min=2.0, max=40.0, step=1

let groups = [];
let polygons = new Polygons();

function init() {
for (let i = 0; i<grid*grid; i++) {
const scale = 200 / grid;
const x = (i % grid) - (grid/2|0);
const y = (i/grid|0) - (grid/2|0);

// create base shape
const shapes = [createHexagon(x, y)];
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 + thickness * 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]);
}

const ease = t => t * (2.0 - t);

function walk(i, anim) {
if (i == 0) init();
if (groups.length == 0) return;

let g = groups.pop();
let group = g.group;

// source: https://turtletoy.net/turtle/a3e0be78c0 by @llemarie
const mod = 9; // min=3 max=50 step=1
const xx=i%grid, yy=Math.floor(i/grid);
if (((xx ^ yy) % mod) == 0) {
const t = Math.sqrt(Math.pow(xx-grid/2,2)+Math.pow(yy-grid/2,2)) / grid * 2;
const h = lerp(minHeight, maxHeight, ease(t) );
group.forEach(p => {
p[1] += minHeight * 0.5;
});

const roofShape = polygons.create();
group.forEach((_, j) => roofShape.addPoints([group[j][0], group[j][1] - h]) );
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();
[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) {
}
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];
}
// 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 lerp(a,b,f) { return a*f+b*(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)) };

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