Forked from "Poisson- Disc Grow Patterns" by @reinder Poisson- Disc Grow Patterns
Added an optional points-array to the Text utility to collect the start positions for the Poisson-Disc.
Log in to post a comment.
// Forked from "Poisson- Disc Grow Patterns" by reinder // https://turtletoy.net/turtle/b5510898dc // Poisson-Disc Grow Patterns. Created by Reinder Nijhoff 2019 - @reindernijhoff // The MIT License // // https://turtletoy.net/turtle/b5510898dc // // Grow pattern based on Poisson-disc Sampling. Based on work by @FogleBird: // https://medium.com/@fogleman/pen-plotter-programming-the-basics-ec0407ab5929 // const randomGrowOrder = 0.75; // min=0, max=1, step=0.01 const loosePacking = 0.1; // min=0, max=1, step=0.01 const growIterations = 4000; // min=100, max=10000, step=1 const objectRadius = 0.55; // min=0.1, max=2, step=0.01 const layers = 1; // min=1, max=10, step=1 // // Code // Canvas.setpenopacity(.75); const turtle = new Turtle(); const startPoints = []; const label = "old stuff"; const textScale = 1.2; const textPos = [-(label.length * 7.5 * textScale), 0]; // kinda works, not very accurate const text = new Text(); turtle.jump(textPos[0], textPos[1]); text.print(turtle, label, textScale, startPoints); let pointList = []; for(let b=0;b<layers;b++) { pointList.push(new PoissonDisc(startPoints, objectRadius).addPoints((growIterations * ((b+1)/layers))|0, 3, loosePacking, randomGrowOrder)); } function walk(i) { for(let points of pointList) { if (points[i] && points[i][2]) { turtle.jump(points[i]); turtle.goto(points[i][2]); } } return i < growIterations; } //////////////////////////////////////////////////////////////// // Poisson-Disc utility code // https://turtletoy.net/turtle/b5510898dc //////////////////////////////////////////////////////////////// function PoissonDisc(startPoints, radius) { class PoissonDiscGrid { constructor(sp, radius) { this.cellSize = 1/Math.sqrt(2)/radius; this.radius2 = radius*radius; this.cells = []; sp.forEach( p => this.insert(p) ); } insert(p) { const x = p[0]*this.cellSize|0, y=p[1]*this.cellSize|0; for (let xi = x-1; xi<=x+1; xi++) { for (let yi = y-1; yi<=y+1; yi++) { const ps = this.cell(xi,yi); for (let i=0; i<ps.length; i++) { if ((ps[i][0]-p[0])**2 + (ps[i][1]-p[1])**2 < this.radius2) { return false; } } } } this.cell(x, y).push(p); return true; } cell(x,y) { const c = this.cells; return (c[x]?c[x]:c[x]=[])[y]?c[x][y]:c[x][y]=[]; } } class PoissonDisc { constructor(sp, radius) { this.result = [...sp]; this.active = [...sp]; this.grid = new PoissonDiscGrid(sp, radius); } addPoints(count, maxTries=16, loosePacking=0, randomGrowOrder=0) { mainLoop: while (this.active.length > 0 && count > 0) { const index = (Math.random() * this.active.length * randomGrowOrder) | 0; const point = this.active[index]; for (let i=0; i < maxTries; i++) { const a = Math.random() * 2 * Math.PI; const d = (Math.random()*loosePacking + 1) * radius; const p = [point[0] + Math.cos(a)*d, point[1] + Math.sin(a)*d, point]; if (this.grid.insert(p)) { this.result.push(p); this.active.push(p); count--; continue mainLoop; } } this.active.splice(index, 1); } return this.result; } } return new PoissonDisc(startPoints, radius); } //////////////////////////////////////////////////////////////// // Text utility code - https://turtletoy.net/turtle/1713ddbe99 //////////////////////////////////////////////////////////////// function Text() { class Text { print (t, str, scale, points) { 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=> { const c = this.rotAdd([s[0]*scale - lt, s[1]*scale], pos, h); t.goto(c); if (points) { points.push(c); } 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(); }