Time to revisit some old ugly code and replace it by new ugly code!
A group of turtles is called a bale.
Replacing: Smooth hatching 🌀
Log in to post a comment.
const shades = 8; //min=1 max=30 step=1 const distance10 = 2.5; //min=.1 max=5 step=.01 const hatchAngle = 57; //min=0 max=360 step=1 const draw = 1; //min=-1 max=1 step=2 (White on black, Black on white) const sceneOpacity = 1/shades; const distance = distance10 / 10; // You can find the Turtle API reference here: https://turtletoy.net/syntax Canvas.setpenopacity(draw * sceneOpacity); // Global code will be evaluated once. const polygons = new Polygons(); const bale = new Bale(sceneOpacity); // The walk function will be called until it returns false. function walk(i) { bale.setOpacity(1 - i * sceneOpacity); const p = polygons.create(); p.addPoints(...[[-.5,-50], [.5,-50], [.5,50], [-.5,50]].map(pt => [100/shades-100 + i * 200/shades + pt[0]*180/shades, pt[1]])); p.addHatching(2 * Math.PI * hatchAngle / 360, distance); polygons.draw(bale, p); return i < shades - 1; } //////////////////////////////////////////////////////////////// // Bale utility code 2.0 - Created by Jurgen Westerhof 2025 // https://turtletoy.net/turtle/7b8c486a30 // (Ab)using the opacity, usage: // Canvas.setpenopacity(sceneOpacity); // const bale = new Bale(sceneOpacity); // Then use bale wherever you would use a turtle object to 'draw' // in 'opacity' x (i.e Polygon hatching with a bale object and // .25 interspacing), or: // bale.setOpacity(.5); // bale.jump(0,0); // bale.goto(40,0); //////////////////////////////////////////////////////////////// function Bale(sceneOpacity, turtleClass = Turtle) { class Turtle { #useTurtleCount = 0; #turtles = []; #sceneOpacity = 0; constructor(sceneOpacity, turtleClass) { this.#sceneOpacity = sceneOpacity; this.#turtles = Array.from({length: Math.max(1, this.getTurtleCountForOpacity(1))}, (e,i) => new turtleClass()); this.#useTurtleCount = this.#turtles.length; } setOpacity(opacity) { if(opacity < 0 || 1 < opacity) { throw new Error(`Out of bounds opacity. 0 <= opacity <= 1, got ${opacity}`); } this.#useTurtleCount = this.getTurtleCountForOpacity(opacity); } getProp(prop) { return this.#turtles[0][prop]; } invokeProp(action, a, b, c, d, e, f, g) { return this.#turtles.reduce((acc, turtle, i) => turtle[action == 'goto' && this.#useTurtleCount <= i? 'jump': action](a, b, c, d, e, f, g), 0); } getTurtleCountForOpacity(targetOpacity) { if (targetOpacity <= 0) return 0; if (targetOpacity >= .5 && this.#turtles.length == 1) return 1; return Math.round(Math.log(1 - Math.min(.99, targetOpacity)) / Math.log(1 - this.#sceneOpacity)); } } const TurtleProxy = { get(target, prop, receiver) { switch(prop) { case 'setOpacity': return (opacity) => target.setOpacity(opacity); case 'clone': throw new Error('Cloning not implemented'); case 'position': //for faster processing, known functions case 'pos': //of a turtle that don't change the case 'xcor': //the state of a turtle but just tell case 'x': //the state of a turtle, there's no need case 'ycor': //to iterate over the whole bale but case 'y': //only return the value from the first case 'heading': //turtle in the bale. That's where these case 'h': //9 cases are an exception to the rest case 'isdown': //of a turtle's functions return target.getProp(prop); default: return (a, b, c, d, e, f, g) => target.invokeProp(prop, a, b, c, d, e, f, g); } }, }; return new Proxy(new Turtle(sceneOpacity, turtleClass), TurtleProxy); } 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) {if(typeof t == 'object') return t.hatch(n, this);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])]}};const y=function(n,j=[]){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]||j.includes(s)||(h[s]=1)})}}return Array.from(Object.keys(h),s=>t[s])};return{list:()=>t,create:()=>new n,draw:(n,h,o=!0)=>{rpl=y(h.aabb, this.dei === undefined? []: Array.from({length: t.length - this.dei}).map((e, i) => this.dsi + i));for(let t=0;t<rpl.length&&h.boolean(rpl[t]);t++);const td=n.isdown();if(this.dsi!==undefined&&this.dei===undefined)n.pu();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);if(td)n.pd();},startDeferSession:()=>{if(this.dei!==undefined)throw new Error('Finalize deferring before starting new session');this.dsi=t.length;},stopDeferring:()=>{if(this.dsi === undefined)throw new Error('Start deferring before stopping');this.dei=t.length;},finalizeDeferSession:(n)=>{if(this.dei===undefined)throw new Error('Stop deferring before finalizing');for(let i=this.dsi;i<this.dei;i++) {rpl = y(t[i].aabb,Array.from({length:this.dei-this.dsi+1}).map((e,j)=>i+j));for(let j=0;j<rpl.length&&t[i].boolean(rpl[j]);j++);t[i].draw(n);}this.dsi=undefined;this.dei=undefined;}}}