// 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), ]; this.turtle.addTransform(Rotate(Random.range(-0.1,0.2))); this.turtle.addTransform(Scale(scale)); this.turtle.addTransform(Translate(x,y)); const hatching = 1.0 / scale; if (Random.bool()) { this.layers[0].circle(Random.range(10,20), true, hatching); } this.layers[0].star({ inner: Random.range(30,5), outer: 70, angle: Math.PI / 4, corners: 4, hatching: hatching, }); if (Random.bool()) { this.layers[0].circle(Random.range(10,30), true, hatching); } if (Random.bool(.75)) { this.layers[1].star({ inner: Random.range(30,5), outer: Random.range(40,70), angle: Math.PI / 2, corners: 4, hatching: hatching, }); } this.layers[2].circles(Random.range(50,70), Random.range(40,70), Random.bool(), hatching); if (Random.bool(.75)) { const start = Random.range(40,60); this.layers[2].simpleStar({ inner: Random.range(start,5), outer: Random.range(start+1,70), angle: Math.PI*2 * 2/8, corners: Random.or(8, Random.or(360/2,360/4)), hatching: hatching, }); } for(let layer of this.layers) { layer.draw(this.turtle); } const fontSize = Random.range(0.3, 0.5); this.turtle.jump(-4, -80); this.text.print(this.turtle, "N", fontSize); this.turtle.jump(80-4, 0); this.text.print(this.turtle, "E", fontSize); this.turtle.jump(-4, 80); this.text.print(this.turtle, "S", fontSize); this.turtle.jump(-80-4, 0); this.text.print(this.turtle, "W", fontSize); } } class Random { 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(); } } 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); }