Sunburst 🌞

Yet another parameterised implementation of a concept by KennyVaden at Reddit: reddit.com/r/generat…ga/sun_burst_r_code/

Log in to post a comment.

const rings = 9; //min=1 max=15 step=1
const rMin = 8; //min=5 max=50 step=1
const rMax = 98; //min=51 max=150 step=1
const segments = 75; //min=40 max=80 step=1
const oscillations = 12 //min=3 max=20 step=1
const ampMin = .4; //min=.1 max=1 step=.01
const ampMax = .95; //min=.5 max=2 step=.01
const distortionRandom = .5; //min=0 max=3 step=.1
const distortionFactor = 1; //min=0 max=10 step=.1

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

const ringHeight = (rMax - rMin) / rings
const segmentAngle = 2*Math.PI / segments;

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

for(let i = 1; i < rMin - 1; i += (rMin - 2) / 6) {
    drawTour(turtle, circlePoints(i));
}

// The walk function will be called until it returns false.
function walk(i) {
    const r = rMin + i * ringHeight;
    
    drawTour(turtle, circlePoints(r));
    
    const ringDistortion = distortionRandom * Math.random() * i * segmentAngle;
    
    for(let j = 0; j < segments; j++) {
        const phase = 2*Math.PI * j / segments;
        
        const ampM = (Math.cos(oscillations * phase) + 1) / 2;
        const amp = (ampMin + (ampMax - ampMin) * ampM) * ringHeight;
        
        const bottomPt = (fracFromLeftToRight) => [
            r         *  Math.sin(phase + (fracFromLeftToRight - .5) * segmentAngle),
            r         * -Math.cos(phase + (fracFromLeftToRight - .5) * segmentAngle)
        ];
        
        const leftBottom = bottomPt(0);
        const leftTop = [
            (r + amp) *  Math.sin(phase - (1 + .5 * ampM) * segmentAngle),
            (r + amp) * -Math.cos(phase - (1 + .5 * ampM) * segmentAngle)
        ];
        const rightTop = [
            (r + amp) *  Math.sin(phase - (1 - .5 * ampM) * segmentAngle),
            (r + amp) * -Math.cos(phase - (1 - .5 * ampM) * segmentAngle)
        ];
        const rightBottom = bottomPt(1);
        
        const rot = rot2(i * segmentAngle * distortionFactor + ringDistortion);
        
        turtle.jump(trans2(rot, leftBottom ));
        turtle.goto(trans2(rot, leftTop    ));
        turtle.goto(trans2(rot, rightTop   ));
        turtle.goto(trans2(rot, rightBottom));

        const leftToRight = sub2(rightTop, leftTop);
        
        Array.from({length: Math.random() <.5? 2: 3}).map((v, i, a) => [
            bottomPt((i+1)/(a.length+1)),
            add2(leftTop, scale2(leftToRight, (i+1)/(a.length + 1)))
        ]).forEach(ft => {
            turtle.jump(trans2(rot, ft[0]));
            turtle.goto(trans2(rot, ft[1]));
        });
        
    }
    
    return i < rings - 1;    
}

/// Below is the standard lib I just copy paste under almost all my turtles

function approx1(a,b,delta=0.0001) { return -delta < a-b && a-b < delta }

////////////////////////////////////////////////////////////////
// 2D Vector Math utility code - Created by several Turtletoy users
////////////////////////////////////////////////////////////////
function norm2(a) { return scale2(a, 1/len2(a)); }
function add2(a, b) { return [a[0]+b[0], a[1]+b[1]]; }
function sub2(a, b) { return [a[0]-b[0], a[1]-b[1]]; }
function mul2(a, b) { return [a[0]*b[0], a[1]*b[1]]; }
function scale2(a, s) { return [a[0]*s,a[1]*s]; }
function lerp2(a,b,t) { return [a[0]*(1-t) + b[0]*t, a[1]*(1-t) + b[1]*t]; }
function lenSq2(a) { return a[0]**2+a[1]**2; }
function len2(a) { return Math.sqrt(lenSq2(a)); }
function rot2(a) { return [Math.cos(a), -Math.sin(a), Math.sin(a), Math.cos(a)]; }
function trans2(m, a) { return [m[0]*a[0]+m[2]*a[1], m[1]*a[0]+m[3]*a[1]]; } //Matrix(2x1) x Matrix(2x2)
function dist2(a,b) { return Math.hypot(...sub2(a,b)); }
function dot2(a,b) { return a[0]*b[0]+a[1]*b[1]; }
function cross2(a,b) { return a[0]*b[1] - a[1]*b[0]; }
function multiply2(a2x2, a) { return [(a[0]*a2x2[0])+(a[1]*a2x2[1]),(a[0]*a2x2[2])+(a[1]*a2x2[3])]; } //Matrix(2x2) x Matrix(1x2)
function intersect_info2(as, ad, bs, bd) {
    const d = [bs[0] - as[0], bs[1] - as[1]];
    const det = bd[0] * ad[1] - bd[1] * ad[0];
    if(det === 0) return false;
    const res = [(d[1] * bd[0] - d[0] * bd[1]) / det, (d[1] * ad[0] - d[0] * ad[1]) / det];
    return [...res, add2(as, scale2(ad, res[0]))];
}
function intersect_ray2(a, b, c, d) {
    const i = intersect_info2(a, b, c, d);
    return i === false? i: i[2];
}
function segment_intersect2(a,b,c,d, inclusive = true) {
    const i = intersect_info2(a, sub2(b, a), c, sub2(d, c));
    if(i === false) return false;
    const t = inclusive? 0<=i[0]&&i[0]<=1&&0<=i[1]&&i[1]<=1: 0<i[0]&&i[0]<1&&0<i[1]&&i[1]<1;
    return t?i[2]:false;
}
function approx2(a,b,delta=0.0001) { return len2(sub2(a,b)) < delta }
function eq2(a,b) { return a[0]==b[0]&&a[1]==b[1]; }
function clamp2(a, tl, br) { return [Math.max(Math.min(br[0], a[0]), tl[0]), Math.max(Math.min(br[1], a[1]), tl[1])]; }
function nearSq2(test, near, delta = .0001) {
    return near[0] - delta < test[0] && test[0] < near[0] + delta &&
           near[1] - delta < test[1] && test[1] < near[1] + delta;
}

////////////////////////////////////////////////////////////////
// Start of some path utility code - Created by Jurgen Westerhof 2023
////////////////////////////////////////////////////////////////
function circlePoints(radius, extend = 2 * Math.PI, clockWiseStart = 0, steps = null, includeLast = false) { return [steps == null? (radius*extend+1)|0: steps].map(steps => Array.from({length: steps}).map((v, i, a) => [radius * Math.cos(clockWiseStart + extend*i/(a.length-(includeLast?1:0))), radius * Math.sin(clockWiseStart + extend*i/(a.length-(includeLast?1:0)))])).pop(); }
function pts2Edges(pts) { return pts.map((v, i, a) => [v, a[(i+1)%a.length]]); }
function drawPath(turtle, pts) { return pts.forEach((pt, i) => turtle[i == 0? 'jump':'goto'](pt)); }
function drawTour(turtle, pts) { return drawPath(turtle, pts.concat([pts[0]])); }
function drawPoint(turtle, pt) { return drawTour(turtle, circlePoints(.5).map(p => add2(p, pt))); }
function isInPolygon(edges, pt) { return edges.map(edge => intersect_info2(edge[0], sub2(edge[1], edge[0]), pt, [0, 300])).filter(ii => ii !== false && 0 <= ii[0] && ii[0] <= 1 && 0 < ii[1]).length % 2 == 1; }
function isInVectorTour(vectors, pt) { return vectors.map(v => intersect_info2(...v, pt[0], pt[1])).filter(ii => ii !== false && 0 <= ii[0] && ii[0] < 1 && 0 <= ii[1]).length % 2 == 1; }
function tourToVectors(path) { return path.map((v, i, a) => [v, sub2(a[(i+1)%a.length], v)]); }
function thickLinePaths(from, to, thickness) { return [trans2(rot2(Math.atan2(...sub2(to, from))), [thickness/2, 0])].map(v => [[add2(from, v), add2(to, v)], [sub2(from, v), sub2(to, v)]]).pop();}
function toursIntersect(path1, path2) { return path1.some((pt1, i1) => path2.some((pt2, i2) => segment_intersect2(pt1, path1[(i1 + 1) % path1.length], pt2, path2[(i2 + 1) % path2.length]) !== false)); }

// Fisher-Yates (aka Knuth) Shuffle
// https://stackoverflow.com/questions/2450954/how-to-randomize-shuffle-a-javascript-array#2450976
function shuffle(array) {
  let currentIndex = array.length,  randomIndex;

  // While there remain elements to shuffle.
  while (currentIndex > 0) {

    // Pick a remaining element.
    randomIndex = Math.floor(Math.random() * currentIndex);
    currentIndex--;

    // And swap it with the current element.
    [array[currentIndex], array[randomIndex]] = [
      array[randomIndex], array[currentIndex]];
  }

  return array;
}