diff --git a/src/game/96-map/89-pie-globe.js b/src/game/96-map/89-pie-globe.js index 20c227f..89fe3ed 100644 --- a/src/game/96-map/89-pie-globe.js +++ b/src/game/96-map/89-pie-globe.js @@ -12,12 +12,43 @@ const PG_ST={ // The whole state of the globe. cv is null whenever it is closed, and then nothing below runs. const PG={cv:null,ctx:null,get:null,raf:0,last:0,t:0,yaw:-20,tilt:18,vy:0,drag:null,night:null,anim:true,dirty:true, data:null,dAt:-1e9,sig:'',sm:[],dish:[],fl:[],hi:null,pads:[],lch:[],fly:[],on:null,g:null,frames:0,m:new Float64Array(9)}; -let PG_LAND=null,PG_GRID=null,PG_LIGHT=null,PG_STAR=null; +let PG_LAND=null,PG_LAND_DIR=null,PG_TURN=1,PG_GRID=null,PG_LIGHT=null,PG_STAR=null; const pgNightPref=()=>{if(PG.night===null){try{PG.night=localStorage.getItem('sot-globe')==='night';}catch(e){PG.night=false;}}return PG.night;}; // Unit vectors for the land rings (LANDS_LL), the graticule, city lights and stars: worked out once, on first open. function pgUnit(lo,la,a,i){const b=la*PG_D,c=Math.cos(b),o=lo*PG_D;a[i]=c*Math.cos(o);a[i+1]=c*Math.sin(o);a[i+2]=Math.sin(b);} +// One land ring, clipped to the near side (as map libraries clip). The coast runs to where it meets the horizon; from there the +// fill follows the rim, in the ring's own turning direction (PG_LAND_DIR, from its signed area), to the next point where any +// coast comes back. Only real coast goes into the outline. Pushing hidden points onto the rim made coasts on the edge shiver, +// and a ring bigger than half the rim (Eurasia) filled the whole globe from some angles. +const PG_TAU=2*Math.PI,pgAng=a=>a<0?a+PG_TAU:a; +function pgLandRing(ctx,b,n,dir,cx,cy,R,coast){let s=-1;for(let i=0;i=0){s=i;break;}if(s<0)return; + const X=(h)=>cx+R*h,Y=(v)=>cy-R*v;let all=true;for(let i=0;i{const u=px/(px-x);let eh=ph+(h-ph)*u,ev=pv+(v-pv)*u;const m=Math.hypot(eh,ev)||1;return [eh/m,ev/m];}; + for(let k=1;k<=n;k++){const j=((s+k)%n)*3,x=b[j],h=b[j+1],v=b[j+2],up=x>=0,was=px>=0; + if(was&&!up){const e=cross(x,h,v);if(cur){cur.p.push(e[0],e[1]);cur.out=Math.atan2(e[1],e[0]);runs.push(cur);cur=null;}else{head.push(e[0],e[1]);head.outA=Math.atan2(e[1],e[0]);}} + else if(!was&&up){const e=cross(x,h,v);cur={p:[e[0],e[1],h,v],in:Math.atan2(e[1],e[0])};} + else if(up){if(cur)cur.p.push(h,v);else head.push(h,v);} + px=x;ph=h;pv=v;} + // The walk started at a visible point: the run still open at the end carries on into the first points. + if(cur){cur.p.push(...pre,...head);cur.out=head.outA;runs.push(cur);} + else if(head.outA!==undefined)runs.push({p:[...pre,...head],in:NaN,out:head.outA}); + if(!runs.length)return; + for(const r of runs){coast.moveTo(X(r.p[0]),Y(r.p[1]));for(let i=2;i0);r=best;} + ctx.closePath();}} function pgVecs(){if(PG_LAND)return; PG_LAND=LANDS_LL.map(l=>{const a=new Float32Array(l.ll.length*3);l.ll.forEach((q,i)=>pgUnit(q[0],q[1],a,i*3));return a;}); + PG_LAND_DIR=PG_LAND.map(a=>{const n=a.length/3;let cx=0,cy=0,cz=0,s=0;for(let i=0;i=0?PG_TURN:-PG_TURN;}); PG_GRID=[];for(let lo=-180;lo<180;lo+=30){const a=new Float32Array(31*3);for(let j=0;j<=30;j++)pgUnit(lo,-90+j*6,a,j*3);PG_GRID.push(a);} for(let la=-60;la<=60;la+=30){const a=new Float32Array(61*3);for(let j=0;j<=60;j++)pgUnit(-180+j*6,la,a,j*3);PG_GRID.push(a);} const cs=CITIES.filter(c=>isFinite(c.lat)&&isFinite(c.lon));PG_LIGHT={v:new Float32Array(cs.length*3),r:cs.map(c=>1.1+Math.min(1.6,(c.P||0)/400))};cs.forEach((c,i)=>pgUnit(c.lon,c.lat,PG_LIGHT.v,i*3)); @@ -101,9 +132,8 @@ function pgDraw(ctx,W,H){ for(let i=0;i=0){any=true;break;}if(!any)continue; - for(let i=0;i { + const { ev, errors } = load({ seed: 1 }); + // A pretend canvas that adds up how far each fill runs along the rim. + const r = ev(`(()=>{pgVecs();const rec={arcs:[],moveTo(){},lineTo(){},closePath(){},arc(x,y,R,a,b,ccw){let s=ccw?a-b:b-a;this.arcs.push(s);}}; + const coast=new Path2D();let worst=0,where='',shapes=0; + for(let tilt=-60;tilt<=60;tilt+=15)for(let yaw=0;yaw<360;yaw+=5){PG.yaw=yaw;PG.tilt=tilt;pgCam(); + for(let li=0;lia+Math.abs(b),0);shapes++;if(lap>worst){worst=lap;where=LANDS_LL[li].id+' yaw '+yaw+' tilt '+tilt;}}} + return {worst,where,shapes,dirs:PG_LAND_DIR.length===PG_LAND.length&&PG_LAND_DIR.every(d=>d===1||d===-1)};})()`); + assert.ok(r.dirs, 'every land ring knows which way it turns'); + // A fill that runs all the way round the rim paints the whole globe green (Eurasia from the south used to). + assert.ok(r.worst < 2 * Math.PI - 0.1, 'the longest run along the rim is ' + r.worst.toFixed(2) + ' rad, at ' + r.where); + assert.ok(r.shapes > 1000); + assert.deepEqual(errors, []); +});