Twisted grid 🌀

Quadrant square tile variations

Concept: reddit.com/r/generative/comments/tdbxwe/code/
Modified concept: reddit.com/r/generat…_post_i_saw_on_here/

Log in to post a comment.

// You can find the Turtle API reference here: https://turtletoy.net/syntax
Canvas.setpenopacity(.7);

const layers=11; //min=1 max=30 step=1
const grid=15; //min=1 max=50 step=1
const margin=10; //min=0 max=50 step=1
const tileMargin = .12;//min=0 max=1 step=.01
const maxRotation=.1; //min=-.5 max=.5 step=.01
const orthogonalAt=1; //min=0 max=2 step=1 (Bottom, Middle, Top)
const rotationIncrease = 3;//min=0 max=4 step=1 (Linear, Quadratic, Inverse quadratic, Sine, Cosine)
const shapeDistribution = 0;//min=0 max=1 step=1 (Random by shape and configuration, Random by shape and then random by configuration)

// Global code will be evaluated once.
const turtle = new Turtle();
const polygons = new Polygons();

const locations = [[-1, -1], [0, -1], [1, -1],[-1, 0], [0, 0], [1, 0],[-1, 1], [0, 1], [1, 1]];
const tilesPerLayer = grid**2;
const tileSize = (200 - margin - margin) / grid;
let tiles = Array.from({length: tilesPerLayer}, (i) => Math.random() * 17 | 0); //first = full, next 4 are halves, next 4 are 3 out of 4 quadrant clockwise, next 4 are 3 out of 4 cclockwise, next 4 are single quadrant
if(shapeDistribution == 1) tiles = Array.from({length: tilesPerLayer}, function(i) {
    switch(Math.random() * 4 | 0) {
        case 1: return (Math.random() * 4 | 0) + 1; //any configuration of half
        case 2: return (Math.random() * 8 | 0) + 5; //any configuration of 3 quadrants
        case 3: return (Math.random() * 4 | 0) + 13; //any single quadrant
    }
    return 0; //full
});

const rot2 = (a) => [Math.cos(a), -Math.sin(a), Math.sin(a), Math.cos(a)];
const trans2 = (m, a) => [m[0]*a[0]+m[2]*a[1], m[1]*a[0]+m[3]*a[1]];
const scale2 = (a, s) => [a[0]*s, a[1]*s];
const add2 = (a, b) => [a[0]+b[0], a[1]+b[1]];

// The walk function will be called until it returns false.
function walk(i) {
    const layer = i / tilesPerLayer | 0, j = i - (tilesPerLayer * layer), column = j % grid, row =  j / grid | 0;
    let rot = 1 - (layer / layers); switch(rotationIncrease) { case 1: rot = 1 - (layer / Math.max(1, layers - 1))**2; break; case 2: rot = (1- (layer / Math.max(1, layers - 1)))**2; break; case 3: rot = 1 - Math.sin((layer / Math.max(1, layers - 1)) * Math.PI / 2); break; case 4: rot = Math.cos((layer / Math.max(1, layers - 1)) * Math.PI / 2); break; }
    drawTile(j, [((column + .5) - (grid/2)) * tileSize, ((row + .5) - (grid/2)) * tileSize], tileSize * (1-tileMargin), rot2((rot - (orthogonalAt == 0? 0: orthogonalAt == 1? .5: 1)) * maxRotation));
    return i < tilesPerLayer * layers - 1;
}

function drawTile(i, offset, size, rotation) {
    let tile = [0, 2, 8, 6]; switch(tiles[i]) {case 1: tile = [0, 1, 7, 6]; break;case 2: tile = [1, 2, 8, 7]; break;case 3: tile = [0, 2, 5, 3]; break;case 4: tile = [3, 5, 8, 6]; break;case 5: tile = [0, 2, 8, 7, 4, 3]; break;case 6: tile = [2, 8, 6, 3, 4, 1]; break;case 7: tile = [8, 6, 0, 1, 4, 5]; break;case 8: tile = [6, 0, 2, 5, 4, 7]; break;case 9: tile = [0, 6, 8, 5, 4, 1]; break;case 10: tile = [6, 8, 2, 1, 4, 3]; break;case 11: tile = [8, 2, 0, 3, 4, 7]; break;case 12: tile = [2, 0, 6, 7, 4, 5]; break;case 13: tile = [0, 1, 4, 3]; break;case 14: tile = [1, 2, 5, 4]; break;case 15: tile = [3, 4, 7, 6]; break;case 16: tile = [4, 5, 8, 7]; break;}
    
    const p = polygons.create();
    p.addPoints(...tile.map((i) => trans2(rotation, add2(scale2(locations[i], size / 2), offset))));
    p.addOutline();
    polygons.draw(turtle, p);
}

////////////////////////////////////////////////////////////////
// Polygon Clipping utility code - Created by Reinder Nijhoff 2019
// (Polygon binning by Lionel Lemarie 2021)
// https://turtletoy.net/turtle/a5befa1f8d
////////////////////////////////////////////////////////////////
function Polygons(){const t=[],s=25,e=Array.from({length:s**2},t=>[]),n=class{constructor(){this.cp=[],this.dp=[],this.aabb=[]}addPoints(...t){let s=1e5,e=-1e5,n=1e5,h=-1e5;(this.cp=[...this.cp,...t]).forEach(t=>{s=Math.min(s,t[0]),e=Math.max(e,t[0]),n=Math.min(n,t[1]),h=Math.max(h,t[1])}),this.aabb=[s,n,e,h]}addSegments(...t){t.forEach(t=>this.dp.push(t))}addOutline(){for(let t=0,s=this.cp.length;t<s;t++)this.dp.push(this.cp[t],this.cp[(t+1)%s])}draw(t){for(let s=0,e=this.dp.length;s<e;s+=2)t.jump(this.dp[s]),t.goto(this.dp[s+1])}addHatching(t,s){const e=new n;e.cp.push([-1e5,-1e5],[1e5,-1e5],[1e5,1e5],[-1e5,1e5]);const h=Math.sin(t)*s,o=Math.cos(t)*s,a=200*Math.sin(t),i=200*Math.cos(t);for(let t=.5;t<150/s;t++)e.dp.push([h*t+i,o*t-a],[h*t-i,o*t+a]),e.dp.push([-h*t+i,-o*t-a],[-h*t-i,-o*t+a]);e.boolean(this,!1),this.dp=[...this.dp,...e.dp]}inside(t){let s=0;for(let e=0,n=this.cp.length;e<n;e++)this.segment_intersect(t,[.1,-1e3],this.cp[e],this.cp[(e+1)%n])&&s++;return 1&s}boolean(t,s=!0){const e=[];for(let n=0,h=this.dp.length;n<h;n+=2){const h=this.dp[n],o=this.dp[n+1],a=[];for(let s=0,e=t.cp.length;s<e;s++){const n=this.segment_intersect(h,o,t.cp[s],t.cp[(s+1)%e]);!1!==n&&a.push(n)}if(0===a.length)s===!t.inside(h)&&e.push(h,o);else{a.push(h,o);const n=o[0]-h[0],i=o[1]-h[1];a.sort((t,s)=>(t[0]-h[0])*n+(t[1]-h[1])*i-(s[0]-h[0])*n-(s[1]-h[1])*i);for(let n=0;n<a.length-1;n++)(a[n][0]-a[n+1][0])**2+(a[n][1]-a[n+1][1])**2>=.001&&s===!t.inside([(a[n][0]+a[n+1][0])/2,(a[n][1]+a[n+1][1])/2])&&e.push(a[n],a[n+1])}}return(this.dp=e).length>0}segment_intersect(t,s,e,n){const h=(n[1]-e[1])*(s[0]-t[0])-(n[0]-e[0])*(s[1]-t[1]);if(0===h)return!1;const o=((n[0]-e[0])*(t[1]-e[1])-(n[1]-e[1])*(t[0]-e[0]))/h,a=((s[0]-t[0])*(t[1]-e[1])-(s[1]-t[1])*(t[0]-e[0]))/h;return o>=0&&o<=1&&a>=0&&a<=1&&[t[0]+o*(s[0]-t[0]),t[1]+o*(s[1]-t[1])]}};return{list:()=>t,create:()=>new n,draw:(n,h,o=!0)=>{reducedPolygonList=function(n){const h={},o=200/s;for(var a=0;a<s;a++){const c=a*o-100,r=[0,c,200,c+o];if(!(n[3]<r[1]||n[1]>r[3]))for(var i=0;i<s;i++){const c=i*o-100;r[0]=c,r[2]=c+o,n[0]>r[2]||n[2]<r[0]||e[i+a*s].forEach(s=>{const e=t[s];n[3]<e.aabb[1]||n[1]>e.aabb[3]||n[0]>e.aabb[2]||n[2]<e.aabb[0]||(h[s]=1)})}}return Array.from(Object.keys(h),s=>t[s])}(h.aabb);for(let t=0;t<reducedPolygonList.length&&h.boolean(reducedPolygonList[t]);t++);h.draw(n),o&&function(n){t.push(n);const h=t.length-1,o=200/s;e.forEach((t,e)=>{const a=e%s*o-100,i=(e/s|0)*o-100,c=[a,i,a+o,i+o];c[3]<n.aabb[1]||c[1]>n.aabb[3]||c[0]>n.aabb[2]||c[2]<n.aabb[0]||t.push(h)})}(h)}}}