Fork: Compass generator 🧭

With adjustable parameters

Inspired by watabou.itch.io/compass-rose-generator

Log in to post a comment.

// Forked from "Compass generator 🧭" by markknol
// https://turtletoy.net/turtle/5ef089d251

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

function walk() {
    // new Compass(x, y, scale)
    new Compass(0, 0, 1.0);
}

class Compass {
    constructor(x = 0, y = 0, scale = 1.0) {
        this.turtle = new Tortoise();
        this.text = new Text();
        this.polygons = new Polygons();
        this.layers = [
            new Layer(this.polygons),
            new Layer(this.polygons),
            new Layer(this.polygons),
        ];
        
        Random.init();
        
        let rotation = -12; // min=-180, max=180, step=1
        this.turtle.addTransform(Rotate(-rotation/180*Math.PI));
        this.turtle.addTransform(Scale(scale));
        this.turtle.addTransform(Translate(x,y));

        const hatching = 1.0 / scale;
        let circle0 = 1;
        if (circle0) {
            let circle0radius = 10; // min=0, max=20, step=1
            this.layers[0].circle(circle0radius, true, hatching);
        }
        this.layers[0].star({
            inner: Random.range(30,5),
            outer: 70,
            angle: Math.PI / 4,
            corners: 4,
            hatching: hatching,
        });
        let circle1radius = 30; // min=10, max=50, step=1
        if (Random.bool()) {
            this.layers[0].circle(circle1radius, true, hatching); //Random.range(10,30)
        }
        
        let L1starCorners = 4; // min=0, max=20, step=4
        if (Random.bool(.75)) {
            this.layers[1].star({
                inner: Random.range(30,5),
                outer: Random.range(40,70),
                angle: Math.PI / 2,
                corners: L1starCorners,
                hatching: hatching,
            });
            
        }
    
        this.layers[2].circles(Random.range(50,70), Random.range(40,70), Random.bool(), hatching);
        
        let L2starCorners = 8; // min=0, max=180, step=4
        if (Random.bool(.75)) {
            const start = Random.range(40,60);
            let L2starRadius = 26; // min=1, max=70, step=1
            this.layers[2].simpleStar({
                inner: Random.range(start,5),
                outer: start+L2starRadius, // Random.range(start+1,70)
                angle: Math.PI*2 * 2/8,
                corners: L2starCorners, // Random.or(8, Random.or(360/2,360/4))
                hatching: hatching,
            });
        }
        
        let fontSize = 0.39; // min=0.1, max=0.5, step=0.01
       
        for(let layer of this.layers) {
            layer.draw(this.turtle);
        }
        
        const xPositionFont = -fontSize*10-0.15;
        this.turtle.jump(xPositionFont, -80);
        this.text.print(this.turtle, "N", fontSize);
        this.turtle.jump(80+xPositionFont, 0);
        this.text.print(this.turtle, "E", fontSize);
        this.turtle.jump(xPositionFont, 80);
        this.text.print(this.turtle, "S", fontSize);
        this.turtle.jump(-80+xPositionFont, 0);
        this.text.print(this.turtle, "W", fontSize);
    }
}

class Random {
    static init() {
        this.random=[0.8039512153843181, 0.19088835847761176, 0.31269755914276454, 0.4255099006565972, 0.3591293258205701, 0.17971601548288252, 0.30221227819678353, 0.8467156425264916, 0.0183103558754395, 0.4383106968347066, 0.6880849392477522, 0.6118388681979074, 0.22173751397538577, 0.8228975529521321, 0.4185202902841392, 0.8471798189214016, 0.35158244721258647, 0.14713072373332692, 0.7687857045954077, 0.8683702160674036, 0.6403423104822813, 0.05846498993955984, 0.18929955525153963, 0.42535325090455334, 0.7909091035166822, 0.682697095663108, 0.2974166625268283, 0.6006004065204986, 0.12485596562402679, 0.39032347813675916, 0.48362104691253704, 0.19339623995973865, 0.26366365319063423, 0.4576299540238642, 0.6850247641304239, 0.05482397026990826, 0.9577774592107352, 0.8616522041253362, 0.8092582915022611, 0.2395199146689646, 0.19129256275001094, 0.002649151516067816, 0.0006395307854201171, 0.3732830982179215, 0.8095510718544106, 0.5722571106904702, 0.23571229461654053, 0.3450977090415148, 0.6112395816744617, 0.4262357537595207, 0.42143018513046215, 0.837089868863798, 0.8557104988028927, 0.7172666391744748, 0.36587337097425987, 0.5312056985016979, 0.034669321277641774, 0.14126588000341178, 0.5422984606380098, 0.02889180981024897, 0.24802218210009241, 0.9777537209240897, 0.4681298720891168, 0.9579239394883706, 0.7473517135743845, 0.4151997813296118, 0.6079040102895488, 0.8461988368889264, 0.8957401159479002, 0.6150029465322822, 0.4335953657037699, 0.5194956302650221, 0.6729604654824501, 0.02398815492247741, 0.41824183766511436, 0.007488725893154413, 0.14781706951213214, 0.7484310315318042, 0.6554833524912157, 0.1344200902967868, 0.7567219439635576, 0.9916789183547933, 0.7184462046018925, 0.505676371651458, 0.1841186556587977, 0.14104756126027707, 0.3262806222834651, 0.4986452809422295, 0.12422351791188391, 0.9227431265136933, 0.27171043145973817, 0.9618205977468317, 0.13828495867391, 0.4532349406750924, 0.8005984748342083, 0.16048577933354513, 0.3624990558772556, 0.942203452130125, 0.6194327464930215, 0.9106683075914366, 0.4478541881173802, 0.9813243388548237, 0.7439111841528656, 0.05526338324225233, 0.19494481726336554, 0.9876315525916132, 0.922706822867008, 0.8308177247787744, 0.015923892410240836, 0.9867108931940505, 0.3220351683954199, 0.9965996007297946, 0.4851592720962722, 0.29683069423747455, 0.6196261101770827, 0.38971092064824475, 0.18504713571512976, 0.20571292639194727, 0.8278908130320585, 0.1867002760066978, 0.29372884317189185, 0.21706813842249428, 0.6259905443059535, 0.6028568655957423, 0.787635665408623, 0.4181985733413852, 0.844533401244636, 0.863068761314012, 0.5930988510191363, 0.6062228527721094, 0.2869939977647944, 0.6724432927244512, 0.10930923660860126, 0.07674526428996731, 0.6934582352516031, 0.14550893590890746, 0.4463677523731403, 0.9377707445216951, 0.7377679017559046, 0.9081898122420213, 0.025785476118384998, 0.48140876863342497, 0.06144369148102258, 0.5830894865368896, 0.8265310757318923, 0.9045515648511777, 0.9369068320499903, 0.26056035255516574, 0.8865043220080638, 0.1939140712953601, 0.5803125028723006, 0.8928173996479548, 0.37007693240990724, 0.16300751585282536, 0.3553200652245907, 0.9888503398207358, 0.83826724683328, 0.6696003979711574, 0.7881846665570718, 0.5759761882082006, 0.09775358488597652, 0.13832567523597616, 0.12226165833875002, 0.8898870031485859, 0.8328812063351461, 0.5545533415201866, 0.7168721206278421, 0.3733969844972337, 0.5588736071733222, 0.06944293755469078, 0.3551700614552815, 0.2165677034389608, 0.7376778568204467, 0.7468000907892204, 0.40690481663357514, 0.8975413100599982, 0.6096299127550587, 0.3055915245198657, 0.8288285817560385, 0.2889982473270766, 0.6802686186895109, 0.6699591102162725, 0.15840039352396917, 0.6923644766706916, 0.6859921664600098, 0.3129690192556128, 0.7185414684753438, 0.6143274378538939, 0.24610952776309425, 0.9246483200237333, 0.9534248708771785, 0.4707254147013473, 0.5464121534464436, 0.7066195443447769, 0.3849935608292243, 0.7409630689933845, 0.3916279810283555, 0.6266718824099731, 0.022577883517719166, 0.35039985844061583, 0.02272148360758064, 0.7965482851122743, 0.9987720574593337, 0.3364838010563257, 0.4836815257069612, 0.8281311931231332, 0.646967206125936, 0.22693118454786732, 0.7090737506465895, 0.24651658275437138, 0.34525742974481777, 0.12800115917937815, 0.17168005269553077, 0.35362368681960377, 0.5375207951811145, 0.9174737189734757, 0.10666118611549757, 0.8489969788655414, 0.12147464025046972, 0.5096473265177907, 0.04598675104758421, 0.1756988826885173, 0.4105042124645327, 0.10211605874791962, 0.26176476246313984, 0.7594404579571439, 0.2135553754595898, 0.5467702046568256, 0.7843118784000694, 0.3617844303487351, 0.05824248005404753, 0.6964179779979534, 0.26390284664917296, 0.1530885704760384, 0.5072602421097308, 0.054214706990628025, 0.49168771247112875, 0.039639643641404376, 0.02845251759594325, 0.726359546790055, 0.24266382923737329, 0.3506604098386821, 0.7649449576275172, 0.5033954541583684, 0.31176182526879115, 0.284963000178339, 0.3543379302843568, 0.9109619961358354, 0.740025368043386, 0.2376882826604777, 0.8561402645934562, 0.5324979464974907, 0.735118174996569, 0.44275217894026686, 0.950113372761285, 0.020622192337274692, 0.6222693695398153, 0.11455517674857707, 0.03264071068010366, 0.34908787766227867, 0.7235486695123341, 0.748303463097891, 0.7278824725761736, 0.8999173270421322, 0.7782661822139056, 0.25284925586645923, 0.4934822627494794, 0.5686547713769242, 0.03198339896314062, 0.019741288933906365, 0.563101286492597, 0.579524761202239, 0.7514110346875079, 0.9182706778092824, 0.3026879938665211, 0.7522282737361924, 0.10082505031799638, 0.6639869140572593, 0.31845377100882954, 0.4344894640470942, 0.6952828216347167, 0.0900319821773915, 0.665644287770593, 0.0020859440187415146, 0.9873486736130779, 0.7134567314787041, 0.2035367672319297, 0.6714926733971267, 0.28753282700597427, 0.8238454235869187, 0.06953055117028362, 0.5320404484494232, 0.6542510081360788, 0.2786069034122336, 0.7820878239463825, 0.5350587254180532, 0.34855928417869475, 0.22133974857062555, 0.999468962899039, 0.64768218927681, 0.07496319887120462, 0.02491092870571432, 0.1997771768766935, 0.09027029950086152, 0.8291925810762367, 0.9221516800978146, 0.9373486660668674, 0.7001209597258208, 0.029044329756161558, 0.9625844251795358, 0.9348807862019562, 0.6416263598232996, 0.8493173460231487, 0.2748835516519784, 0.3551296594592128, 0.40585257640790406, 0.12797414572487742, 0.09313162701962963, 0.7407179487992004, 0.021109944268374203, 0.27448713543992387, 0.7994501348945933, 0.6490062727514784, 0.15714374741728154, 0.40049277216476975, 0.3473925617529027, 0.5687227854718124, 0.7756288707345869, 0.20988754204113413, 0.5737238027816647, 0.4885465711415028, 0.6682387590487623, 0.44381916619559436, 0.7965564118195071, 0.5218774983209751, 0.09914106599008454, 0.9442942311461947, 0.6155590446056304, 0.4167788246778701, 0.880469373593768, 0.6501171985435604, 0.29547999716418394, 0.3754237825999379, 0.7673856617030379, 0.3384437281849265, 0.15874434016376615, 0.11120927352878074, 0.44452122361480484, 0.5135244831601241, 0.9192750755703137, 0.02920062441575444, 0.725073418504279, 0.47836870757807215, 0.18685791699700305, 0.4858634951779578, 0.3428065906003941, 0.7682076433209988, 0.4228164466781962, 0.337198248205548, 0.7328696759526859, 0.6247611124071123, 0.24923045588475712, 0.3181117422369397, 0.48603278238757763, 0.9300999946802423, 0.3549455183814195, 0.028306949691234595, 0.3526290670041303, 0.030122716250704062, 0.9464210395118297, 0.15871631844636214, 0.2776742955392192, 0.7957593242118304, 0.6587313933375869, 0.6005871743059502, 0.8284640592990339, 0.6224183619299734, 0.7745815534160173, 0.3636128729792182, 0.48445181652420644, 0.5634931984612981, 0.3532772294811303, 0.31628891281021776, 0.4085772031400037, 0.024016748593659765, 0.09543467497783276, 0.07453279295225868, 0.01760470434704653, 0.731367479797006, 0.1256309226553558, 0.544173619135161, 0.686723153858773, 0.34146044763965155, 0.933843205410175, 0.4560649056372936, 0.01897205365447585, 0.41746485109608755, 0.6082749202215002, 0.9626577772194498, 0.1711743564071393, 0.7894023618706318, 0.9835700617925264, 0.6697818766922976, 0.6881936707754112, 0.7234153871593051, 0.7810200020603716, 0.3474700987379684, 0.40198576454211765, 0.06104202661693903, 0.24100757208238688, 0.6834938642697638, 0.15122789617812926, 0.23973768950616126, 0.22967937838859653, 0.17689274659020005, 0.12939673466189117, 0.020298300735209884, 0.6580285487670453, 0.6974022817607364, 0.8906012541184978, 0.32100881535070436, 0.4473073612961521, 0.602712790167046, 0.5304059372482274, 0.7668417123187579, 0.3949311759640628, 0.8698276446304997, 0.5075733575388715, 0.920991894886325, 0.5373431099730392, 0.16932775225336083, 0.5518938389893283, 0.0068751746053687945, 0.9364520627912497, 0.4844007612314454, 0.925688467369813, 0.7301548959645022, 0.6998371777594283, 0.4222396468614297, 0.560923949923399, 0.9653262884044687, 0.6415007664988671, 0.6844750760154436, 0.23203997785648556, 0.408661676794412, 0.4640611923205359, 0.21650493515704705, 0.4806069019985313, 0.38042460705827685, 0.007376953438584466, 0.26089998755369426, 0.6023915144334389, 0.16386666046499943, 0.8613224406261681, 0.1322473721178019, 0.7647009963774506, 0.022815858284805035, 0.7496490085300376, 0.8825782907251869, 0.8668455428143207, 0.9287325311173693, 0.5301579650100036, 0.9113644644745531, 0.9541455087942711, 0.3199790847781834, 0.9694344537324302, 0.4412904500344881, 0.58588320916473, 0.4395269766419657, 0.5862980999397011, 0.7865132684895696, 0.6258388130084127, 0.9508713874354657, 0.16385575788065931, 0.24347650110364583, 0.4801052517717518, 0.41781549828126974, 0.24267640775509292, 0.42548440082919514, 0.7381994481694283, 0.2933046508030406, 0.5909366602606971, 0.2276051547444352, 0.4962753093624518, 0.7997588773214207, 0.24736038520797654, 0.29945448928927765, 0.44056405594302794, 0.24681162153766434, 0.10196316161731833];
        let seed=0; // min=0, max=100, step=1
        this.seed=seed;
    }
    
    static range(from, to) {
        return from + Random.next() * (to-from);
    }
    static bool(change = 0.5) {
        return Random.next() < change;
    }
    static or(a, b, change = 0.5) {
        return Random.bool(change) ? a : b;
    }
    static next() {
        //return Math.random();
        return this.random[this.seed++%100];
    }
}

class Layer {
    constructor(polygons) {
        this.hatchingAngle = -Math.PI/4;
        this.polygons = polygons;
        this.shapes = [];
    }
    
    draw(turtle) {
        for(let shape of this.shapes) {
            this.polygons.draw(turtle, shape, true);
        }
    }
    
    create() {
        const shape = this.polygons.create();
        this.shapes.push(shape);
        return shape;
    }
    
    star(config) {
        const odd = Random.bool();
        const superOdd = Random.bool();
        
        const a = this.create();
        PolygonsUtil.starParts(a, true, 0, 0, config.outer, config.inner, config.corners, config.angle);
        if (odd && Random.bool(.75)) a.addHatching(this.hatchingAngle, config.hatching);
        else if(superOdd && Random.bool(.75)) a.addHatching(this.hatchingAngle*-1, config.hatching);
        a.addOutline();
        
        const b = this.create();
        PolygonsUtil.starParts(b, false, 0, 0, config.outer, config.inner, config.corners, config.angle);
        if (!odd && Random.bool(.75)) b.addHatching(this.hatchingAngle, config.hatching);
        b.addOutline();
    }
    
    simpleStar(config) {
       
        const c = this.create();
        PolygonsUtil.circle(c, 0, 0, Random.range(5, 50))
        c.addOutline();
        
        const s = this.create();
        PolygonsUtil.star(s, 0, 0, config.outer, config.inner, config.corners, config.angle);
        
        if (Random.bool()) if (config.corners < 100) s.addHatching(this.hatchingAngle, config.hatching);
        s.addOutline();
        
        
        
    }
    circles(radius1, radius2, outline, hatching) {
        const c = this.create();
        if (Random.bool()) PolygonsUtil.circle(c, 0, 0, radius1/1.5);
        if (Random.bool()) PolygonsUtil.circle(c, 0, 0, radius1/2);
        PolygonsUtil.circle(c, 0, 0, radius2);
        PolygonsUtil.circle(c, 0, 0, radius1);
        
        if (outline) c.addOutline();
        if (Random.bool() || !outline) c.addHatching(this.hatchingAngle, Random.or(1,2) * hatching); 
    }
    
    circle(radius, outline, hatching) {
        const c = this.create();
        PolygonsUtil.circle(c, 0, 0, radius);
        if (outline) c.addOutline();
        c.addHatching(this.hatchingAngle, Random.or(1,2) * hatching); 
    }
}

////////////////////////////////////////////////////////////////
// Polygon helper utility code - Created by Mark Knol 2019
// https://turtletoy.net/turtle/5ef089d251
////////////////////////////////////////////////////////////////

class PolygonsUtil {
    static circle(polygon, x, y, radius = 10, segments = 45) {
        for (let ii = 0; ii <= segments; ii++) {
            const a = ii/ segments * Math.PI * 2;
            polygon.addPoints([x + Math.cos(a) * radius, y + Math.sin(a) * radius]);
        }
    }
    static rect(polygon, x, y, width, height) {
        polygon.addPoints([x, y], [x + width, y], [x + width, y + height], [x, y + height]);
    }
    static star(polygon, x, y, radiusInner = 30, radiusOuter = radiusInner * 0.3, corners = 4, rotation = 0.0) {
        const segments = corners *2;
        for (let ii = 0; ii <= segments; ii++) {
            const a = ii / segments * Math.PI * 2;
            const radius = ii & 1 ? radiusInner : radiusOuter;
            polygon.addPoints([x + Math.cos(rotation + a) * radius, y + Math.sin(rotation +a) * radius]);
        }
    }
    static starParts(polygon, odd, x, y, radiusInner = 30, radiusOuter = radiusInner * 0.3, corners = 4, rotation = 0.0) {
        const segments = corners * 2;
        const start = 0;
        for (let ii = 0; ii <= segments; ii++) {
            const a = ii / segments * Math.PI * 2;
            
            let radius = ii % 2 == 1 ? radiusInner : radiusOuter;
            if (radius == radiusInner && !odd) polygon.addPoints([x, y]);
            polygon.addPoints([x + Math.cos(rotation + a) * radius, y + Math.sin(rotation +a) * radius]);
            if (radius == radiusInner && odd) polygon.addPoints([x, y]);
            
        }
    }
}


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




////////////////////////////////////////////////////////////////
// Text utility code
////////////////////////////////////////////////////////////////
// Created by Reinder Nijhoff 2019 
// @reindernijhoff 
// source: https://turtletoy.net/turtle/1713ddbe99
function Text() {
    class Text {
        print (t, str, scale) {
            t.radians();
            let pos = [t.x(), t.y()], h = t.h(), o = pos;
            str.split('').map(c => {
                const i = c.charCodeAt(0) - 32;
                if (i < 0 ) {
                    pos = o = this.rotAdd([0, 48*scale], o, h);
                } else if (i > 96 ) {
                    pos = this.rotAdd([16*scale, 0], o, h);
                } else {
                    const d = dat[i], lt = d[0]*scale, rt = d[1]*scale, paths = d[2];
                    paths.map( p => {
                        t.up();
                    	p.map( s=> {
                        	t.goto(this.rotAdd([s[0]*scale - lt, s[1]*scale], pos, h));
                        	t.down();
                        });
                    });
                    pos = this.rotAdd([rt - lt, 0], pos, h);
                }
            });
        }
        rotAdd (a, b, h) {
            return [Math.cos(h)*a[0] - Math.sin(h)*a[1] + b[0], 
                    Math.cos(h)*a[1] + Math.sin(h)*a[0] + b[1]];
        }
    }
    
const dat = ('br>eoj^jl<jqirjskrjq>brf^fe<n^ne>`ukZdz<qZjz<dgrg<cmqm>`thZhw<lZlw<qao_l^h^e_caccdeefg'+
'gmiojpkqmqporlshsercp>^vs^as<f^h`hbgdeeceacaab_d^f^h_k`n`q_s^<olmmlolqnspsrrspsnqlol>]wtgtfsereqfph'+
'nmlpjrhsdsbraq`o`makbjifjekckaj_h^f_eaecffhimporqssstrtq>eoj`i_j^k_kajcid>cqnZl\\j_hcghglhqjulxnz>c'+
'qfZh\\j_lcmhmllqjuhxfz>brjdjp<egom<ogem>]wjajs<ajsj>fnkojpiojnkokqis>]wajsj>fnjniojpkojn>_usZaz>`ti'+
'^f_dbcgcjdofrisksnrpoqjqgpbn_k^i^>`tfbhak^ks>`tdcdbe`f_h^l^n_o`pbpdofmicsqs>`te^p^jfmfogphqkqmppnrk'+
'shserdqco>`tm^clrl<m^ms>`to^e^dgefhekenfphqkqmppnrkshserdqco>`tpao_l^j^g_ebdgdlepgrjsksnrppqmqlping'+
'kfjfggeidl>`tq^gs<c^q^>`th^e_dadceegfkgnhpjqlqopqorlshserdqcocldjfhigmfoepcpao_l^h^>`tpeohmjjkikfjd'+
'hcecddaf_i^j^m_oapepjoomrjshserdp>fnjgihjikhjg<jniojpkojn>fnjgihjikhjg<kojpiojnkokqis>^vrabjrs>]wag'+
'sg<amsm>^vbarjbs>asdcdbe`f_h^l^n_o`pbpdofngjijl<jqirjskrjq>]xofndlcicgdfeehekfmhnknmmnk<icgefhfkgmh'+
'n<ocnknmpnrntluiugtdsbq`o_l^i^f_d`bbad`g`jambodqfrislsorqqrp<pcokompn>asj^bs<j^rs<elol>_tc^cs<c^l^o'+
'_p`qbqdpfoglh<chlhoipjqlqopqorlscs>`urcqao_m^i^g_eadccfckdnepgrismsorqprn>_tc^cs<c^j^m_oapcqfqkpnop'+
'mrjscs>`sd^ds<d^q^<dhlh<dsqs>`rd^ds<d^q^<dhlh>`urcqao_m^i^g_eadccfckdnepgrismsorqprnrk<mkrk>_uc^cs<'+
'q^qs<chqh>fnj^js>brn^nnmqlrjshsfreqdndl>_tc^cs<q^cl<hgqs>`qd^ds<dsps>^vb^bs<b^js<r^js<r^rs>_uc^cs<c'+
'^qs<q^qs>_uh^f_daccbfbkcndpfrhslsnrppqnrkrfqcpan_l^h^>_tc^cs<c^l^o_p`qbqepgohlici>_uh^f_daccbfbkcnd'+
'pfrhslsnrppqnrkrfqcpan_l^h^<koqu>_tc^cs<c^l^o_p`qbqdpfoglhch<jhqs>`tqao_l^h^e_caccdeefggmiojpkqmqpo'+
'rlshsercp>brj^js<c^q^>_uc^cmdpfrisksnrppqmq^>asb^js<r^js>^v`^es<j^es<j^os<t^os>`tc^qs<q^cs>asb^jhjs'+
'<r^jh>`tq^cs<c^q^<csqs>cqgZgz<hZhz<gZnZ<gznz>cqc^qv>cqlZlz<mZmz<fZmZ<fzmz>brj\\bj<j\\rj>asazsz>fnkc'+
'ieigjhkgjfig>atpeps<phnfleiegfehdkdmepgrislsnrpp>`sd^ds<dhffhekemfohpkpmopmrkshsfrdp>asphnfleiegfeh'+
'dkdmepgrislsnrpp>atp^ps<phnfleiegfehdkdmepgrislsnrpp>asdkpkpiognfleiegfehdkdmepgrislsnrpp>eqo^m^k_j'+
'bjs<gene>atpepuoxnylzizgy<phnfleiegfehdkdmepgrislsnrpp>ate^es<eihfjemeofpips>fni^j_k^j]i^<jejs>eoj^'+
'k_l^k]j^<kekvjyhzfz>are^es<oeeo<ikps>fnj^js>[y_e_s<_ibfdegeifjijs<jimfoeretfuius>ateees<eihfjemeofp'+
'ips>atiegfehdkdmepgrislsnrppqmqkphnfleie>`sdedz<dhffhekemfohpkpmopmrkshsfrdp>atpepz<phnfleiegfehdkd'+
'mepgrislsnrpp>cpgegs<gkhhjfleoe>bsphofleieffehfjhkmlompopporlsisfrep>eqj^jokrmsos<gene>ateeeofrhsks'+
'mrpo<peps>brdejs<pejs>_ubefs<jefs<jens<rens>bseeps<pees>brdejs<pejshwfydzcz>bspees<eepe<esps>cqlZj['+
'i\\h^h`ibjckekgii<j[i]i_jakbldlfkhgjkllnlpkrjsiuiwjy<ikkmkojqirhthvixjylz>fnjZjz>cqhZj[k\\l^l`kbjci'+
'eigki<j[k]k_jaibhdhfihmjilhnhpirjskukwjy<kkimiojqkrltlvkxjyhz>^vamakbhdgfghhlknlplrksi<akbidhfhhill'+
'nmpmrlsisg>brb^bscsc^d^dsese^f^fsgsg^h^hsisi^j^jsksk^l^lsmsm^n^nsoso^p^psqsq^r^rs').split('>').map(
    r=> { return [r.charCodeAt(0)-106,r.charCodeAt(1)-106, r.substr(2).split('<').map(a => {const ret 
    = []; for (let i=0; i<a.length; i+=2) {ret.push(a.substr(i, 2).split('').map(b => b.charCodeAt(0)
    -106));} return ret; })]; });

    return new Text();
}


function Translate(x,y) { return p => [p[0]+x, p[1]+y]; }
function Rotate(a) { return p => [p[0]*Math.cos(a)+p[1]*Math.sin(a), p[1]*Math.cos(a)-p[0]*Math.sin(a)]; }
function Scale(s) { return p => [p[0]*s, p[1]*s]; }


////////////////////////////////////////////////////////////////
// Tortoise utility code (Minimal Turtle and Transforms)
// https://turtletoy.net/turtle/102cbd7c4d
////////////////////////////////////////////////////////////////

function Tortoise(x, y) {
    class Tortoise extends Turtle {
        constructor(x, y) {
            super(x, y);
            this.ps = Array.isArray(x) ? [...x] : [x || 0, y || 0];
            this.transforms = [];
        }
        addTransform(t) {
            this.transforms.push(t);
            this.jump(this.ps);
            return this;
        }
        applyTransforms(p) {
            if (!this.transforms) return p;
            let pt = [...p];
            this.transforms.map(t => { pt = t(pt); });
            return pt;
        }
        goto(x, y) {
            const p = Array.isArray(x) ? [...x] : [x, y];
            const pt = this.applyTransforms(p);
            if (this.isdown() && (this.pt[0]-pt[0])**2 + (this.pt[1]-pt[1])**2 > 4) {
               this.goto((this.ps[0]+p[0])/2, (this.ps[1]+p[1])/2);
               this.goto(p);
            } else {
                super.goto(pt);
                this.ps = p;
                this.pt = pt;
            }
        }
        position() { return this.ps; }
    }
    return new Tortoise(x,y);
}