Map: test land polygons against their bounding box first
The sea waves, hills and the far view's islet list test points against every coastline polygon while the page loads: about 0.8 s of a throttled phone's load. A point outside a ring's box is outside the ring, so nearly every test stops there. Waves, hills and the far view's islands come out byte-identical.
This commit is contained in:
parent
94b27eadf3
commit
adc312f27b
2 changed files with 19 additions and 1 deletions
|
|
@ -1,5 +1,8 @@
|
|||
function rng(seed){return()=>{seed|=0;seed=seed+0x6D2B79F5|0;let t=Math.imul(seed^seed>>>15,1|seed);t=t+Math.imul(t^t>>>7,61|t)^t;return((t^t>>>14)>>>0)/4294967296;};}
|
||||
function inPoly(x,y,pts){let ins=false;for(let i=0,j=pts.length-1;i<pts.length;j=i++){const [xi,yi]=pts[i],[xj,yj]=pts[j];if(((yi>y)!==(yj>y))&&(x<(xj-xi)*(y-yi)/(yj-yi)+xi))ins=!ins;}return ins;}
|
||||
// A point outside a ring's bounding box is outside the ring: the box is kept on the ring, so the hundreds of tests
|
||||
// at load (sea waves, hills, which islets the far view keeps) skip nearly every coastline without walking it.
|
||||
const polyBox=pts=>{let b=pts.box;if(!b){let x0=Infinity,y0=Infinity,x1=-Infinity,y1=-Infinity;for(const [x,y] of pts){if(x<x0)x0=x;if(x>x1)x1=x;if(y<y0)y0=y;if(y>y1)y1=y;}b=pts.box=[x0,y0,x1,y1];}return b;};
|
||||
function inPoly(x,y,pts){const b=polyBox(pts);if(x<b[0]||x>b[2]||y<b[1]||y>b[3])return false;let ins=false;for(let i=0,j=pts.length-1;i<pts.length;j=i++){const [xi,yi]=pts[i],[xj,yj]=pts[j];if(((yi>y)!==(yj>y))&&(x<(xj-xi)*(y-yi)/(yj-yi)+xi))ins=!ins;}return ins;}
|
||||
function smoothPath(pts){const p=new Path2D();const n=pts.length;const mid=(a,b)=>[(a[0]+b[0])/2,(a[1]+b[1])/2];let m=mid(pts[n-1],pts[0]);p.moveTo(m[0],m[1]);for(let i=0;i<n;i++){const nx=pts[(i+1)%n];m=mid(pts[i],nx);p.quadraticCurveTo(pts[i][0],pts[i][1],m[0],m[1]);}p.closePath();return p;}
|
||||
const LANDP_ALL=COAST.map(smoothPath);
|
||||
// Zoomed out, hundreds of tiny Arctic and Pacific islets, each with a thick outline, turn into noise.
|
||||
|
|
|
|||
15
tests/load-time.test.js
Normal file
15
tests/load-time.test.js
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
const test = require('node:test');
|
||||
const assert = require('node:assert/strict');
|
||||
const { load } = require('./harness');
|
||||
|
||||
// Work done once when a game loads (the possible-diversity map, link curves, the map's sea and hills) has shortcuts;
|
||||
// each must give exactly what the plain version gives.
|
||||
|
||||
test('land test: the bounding box shortcut agrees with walking every coastline', (t) => {
|
||||
const { ev, dom, errors } = load(); t.after(() => dom.window.close());
|
||||
const bad = ev(`(()=>{const plain=(x,y,pts)=>{let ins=false;for(let i=0,j=pts.length-1;i<pts.length;j=i++){const [xi,yi]=pts[i],[xj,yj]=pts[j];if(((yi>y)!==(yj>y))&&(x<(xj-xi)*(y-yi)/(yj-yi)+xi))ins=!ins;}return ins;};
|
||||
let bad=0;for(let x=0;x<W;x+=7)for(let y=0;y<H;y+=7)for(const p of COAST)if(inPoly(x,y,p)!==plain(x,y,p))bad++;for(const c of CITIES)for(const p of COAST)if(inPoly(c.x,c.y,p)!==plain(c.x,c.y,p))bad++;return bad;})()`);
|
||||
assert.equal(bad, 0);
|
||||
assert.equal(ev('WAVES.length+HILLS.length'), 350);
|
||||
assert.deepEqual(errors, []);
|
||||
});
|
||||
Loading…
Add table
Add a link
Reference in a new issue