From 186a6c7300f8365882e3a052a01a651abf3df15b Mon Sep 17 00:00:00 2001 From: Andreas Herd Date: Wed, 13 Dec 2023 21:13:25 +0200 Subject: [PATCH] optimize algo --- .../{index-611b97ba.js => index-3150b0a9.js} | 2 +- docs/assets/worker-21e242e5.js | 5 ---- docs/assets/worker-5821772f.js | 5 ++++ docs/index.html | 2 +- src/layout/position-pictures.ts | 28 +++++++++++-------- src/layout/to-random-tree.ts | 2 +- src/layout/worker.ts | 3 ++ src/utils/average.spec.ts | 8 ------ src/utils/average.ts | 8 ------ src/utils/size-variation.ts | 3 +- 10 files changed, 28 insertions(+), 38 deletions(-) rename docs/assets/{index-611b97ba.js => index-3150b0a9.js} (99%) delete mode 100644 docs/assets/worker-21e242e5.js create mode 100644 docs/assets/worker-5821772f.js delete mode 100644 src/utils/average.spec.ts delete mode 100644 src/utils/average.ts diff --git a/docs/assets/index-611b97ba.js b/docs/assets/index-3150b0a9.js similarity index 99% rename from docs/assets/index-611b97ba.js rename to docs/assets/index-3150b0a9.js index b145898..3c3a6b0 100644 --- a/docs/assets/index-611b97ba.js +++ b/docs/assets/index-3150b0a9.js @@ -81,7 +81,7 @@ Error generating stack: `+o.message+` * @license * Copyright 2019 Google LLC * SPDX-License-Identifier: Apache-2.0 - */const vp=Symbol("Comlink.proxy"),Wv=Symbol("Comlink.endpoint"),Hv=Symbol("Comlink.releaseProxy"),xl=Symbol("Comlink.finalizer"),Di=Symbol("Comlink.thrown"),wp=e=>typeof e=="object"&&e!==null||typeof e=="function",Vv={canHandle:e=>wp(e)&&e[vp],serialize(e){const{port1:t,port2:n}=new MessageChannel;return kp(e,t),[n,[n]]},deserialize(e){return e.start(),Ep(e)}},Qv={canHandle:e=>wp(e)&&Di in e,serialize({value:e}){let t;return e instanceof Error?t={isError:!0,value:{message:e.message,name:e.name,stack:e.stack}}:t={isError:!1,value:e},[t,[]]},deserialize(e){throw e.isError?Object.assign(new Error(e.value.message),e.value):e.value}},Sp=new Map([["proxy",Vv],["throw",Qv]]);function Yv(e,t){for(const n of e)if(t===n||n==="*"||n instanceof RegExp&&n.test(t))return!0;return!1}function kp(e,t=globalThis,n=["*"]){t.addEventListener("message",function r(i){if(!i||!i.data)return;if(!Yv(n,i.origin)){console.warn(`Invalid origin '${i.origin}' for comlink proxy`);return}const{id:o,type:l,path:u}=Object.assign({path:[]},i.data),s=(i.data.argumentList||[]).map(Xt);let a;try{const h=u.slice(0,-1).reduce((p,y)=>p[y],e),m=u.reduce((p,y)=>p[y],e);switch(l){case"GET":a=m;break;case"SET":h[u.slice(-1)[0]]=Xt(i.data.value),a=!0;break;case"APPLY":a=m.apply(h,s);break;case"CONSTRUCT":{const p=new m(...s);a=Zv(p)}break;case"ENDPOINT":{const{port1:p,port2:y}=new MessageChannel;kp(e,y),a=Jv(p,[p])}break;case"RELEASE":a=void 0;break;default:return}}catch(h){a={value:h,[Di]:0}}Promise.resolve(a).catch(h=>({value:h,[Di]:0})).then(h=>{const[m,p]=vo(h);t.postMessage(Object.assign(Object.assign({},m),{id:o}),p),l==="RELEASE"&&(t.removeEventListener("message",r),_p(t),xl in e&&typeof e[xl]=="function"&&e[xl]())}).catch(h=>{const[m,p]=vo({value:new TypeError("Unserializable return value"),[Di]:0});t.postMessage(Object.assign(Object.assign({},m),{id:o}),p)})}),t.start&&t.start()}function Gv(e){return e.constructor.name==="MessagePort"}function _p(e){Gv(e)&&e.close()}function Ep(e,t){return zu(e,[],t)}function Si(e){if(e)throw new Error("Proxy has been released and is not useable")}function xp(e){return mn(e,{type:"RELEASE"}).then(()=>{_p(e)})}const go=new WeakMap,yo="FinalizationRegistry"in globalThis&&new FinalizationRegistry(e=>{const t=(go.get(e)||0)-1;go.set(e,t),t===0&&xp(e)});function Kv(e,t){const n=(go.get(t)||0)+1;go.set(t,n),yo&&yo.register(e,t,e)}function Xv(e){yo&&yo.unregister(e)}function zu(e,t=[],n=function(){}){let r=!1;const i=new Proxy(n,{get(o,l){if(Si(r),l===Hv)return()=>{Xv(i),xp(e),r=!0};if(l==="then"){if(t.length===0)return{then:()=>i};const u=mn(e,{type:"GET",path:t.map(s=>s.toString())}).then(Xt);return u.then.bind(u)}return zu(e,[...t,l])},set(o,l,u){Si(r);const[s,a]=vo(u);return mn(e,{type:"SET",path:[...t,l].map(h=>h.toString()),value:s},a).then(Xt)},apply(o,l,u){Si(r);const s=t[t.length-1];if(s===Wv)return mn(e,{type:"ENDPOINT"}).then(Xt);if(s==="bind")return zu(e,t.slice(0,-1));const[a,h]=wc(u);return mn(e,{type:"APPLY",path:t.map(m=>m.toString()),argumentList:a},h).then(Xt)},construct(o,l){Si(r);const[u,s]=wc(l);return mn(e,{type:"CONSTRUCT",path:t.map(a=>a.toString()),argumentList:u},s).then(Xt)}});return Kv(i,e),i}function qv(e){return Array.prototype.concat.apply([],e)}function wc(e){const t=e.map(vo);return[t.map(n=>n[0]),qv(t.map(n=>n[1]))]}const Cp=new WeakMap;function Jv(e,t){return Cp.set(e,t),e}function Zv(e){return Object.assign(e,{[vp]:!0})}function vo(e){for(const[t,n]of Sp)if(n.canHandle(e)){const[r,i]=n.serialize(e);return[{type:"HANDLER",name:t,value:r},i]}return[{type:"RAW",value:e},Cp.get(e)||[]]}function Xt(e){switch(e.type){case"HANDLER":return Sp.get(e.name).deserialize(e.value);case"RAW":return e.value}}function mn(e,t,n){return new Promise(r=>{const i=bv();e.addEventListener("message",function o(l){!l.data||!l.data.id||l.data.id!==i||(e.removeEventListener("message",o),r(l.data))}),e.start&&e.start(),e.postMessage(Object.assign({id:i},t),n)})}function bv(){return new Array(4).fill(0).map(()=>Math.floor(Math.random()*Number.MAX_SAFE_INTEGER).toString(16)).join("-")}const e0=Ep(new Worker(new URL("/diorama-2023/assets/worker-21e242e5.js",self.location))),t0=(e,t,n)=>e0.findSolution(e,t,n),n0=(e,t)=>{const[n,r]=F.useState(0),{subscribe:i}=F.useContext(Wo),[o,l]=Mv(async()=>t0(e,t),[e.length,t.width,t.height,n]);return F.useLayoutEffect(()=>{t.width>0&&t.height>0&&l()},[l,e.length,t.width,t.height,n]),js(()=>i(Yr.refresh,()=>r(Xd))),o},Sc=ty(cy(Kd)),r0=e=>{const{subscribe:t}=F.useContext(Wo),[n,r]=F.useState(Kd(wy,e));return js(()=>{const i=t(Yr.addImage,()=>r(tc(rc,Xd,Sc(e)))),o=t(Yr.removeImage,()=>r(tc(rc,ry,Ng(1),Sc(e))));return()=>{i(),o()}}),n},i0=({picture:{position:e,dimension:t,url:n},scaleX:r,scaleY:i})=>A.jsx("li",{style:{backgroundImage:`url(${n})`,left:e.x*r,top:e.y*i,width:t.width*r,height:t.height*i}}),o0=(e,t)=>{let n;return(...r)=>{n&&clearTimeout(n),n=setTimeout(()=>{t(...r)},e)}},l0=e=>({width:e.clientWidth,height:e.clientHeight}),u0=()=>{const e=F.useRef(null),[t,n]=F.useState({width:0,height:0});return F.useLayoutEffect(()=>{var l;const r=(l=e.current)==null?void 0:l.parentElement;if(!r)return;const i=o0(50,()=>{const u=l0(r);F.startTransition(()=>{n(s=>Hd(s,u)?s:u)})}),o=new ResizeObserver(i);return o.observe(r),()=>o.disconnect()},[e]),[e,t]},s0=Bo.ol` + */const vp=Symbol("Comlink.proxy"),Wv=Symbol("Comlink.endpoint"),Hv=Symbol("Comlink.releaseProxy"),xl=Symbol("Comlink.finalizer"),Di=Symbol("Comlink.thrown"),wp=e=>typeof e=="object"&&e!==null||typeof e=="function",Vv={canHandle:e=>wp(e)&&e[vp],serialize(e){const{port1:t,port2:n}=new MessageChannel;return kp(e,t),[n,[n]]},deserialize(e){return e.start(),Ep(e)}},Qv={canHandle:e=>wp(e)&&Di in e,serialize({value:e}){let t;return e instanceof Error?t={isError:!0,value:{message:e.message,name:e.name,stack:e.stack}}:t={isError:!1,value:e},[t,[]]},deserialize(e){throw e.isError?Object.assign(new Error(e.value.message),e.value):e.value}},Sp=new Map([["proxy",Vv],["throw",Qv]]);function Yv(e,t){for(const n of e)if(t===n||n==="*"||n instanceof RegExp&&n.test(t))return!0;return!1}function kp(e,t=globalThis,n=["*"]){t.addEventListener("message",function r(i){if(!i||!i.data)return;if(!Yv(n,i.origin)){console.warn(`Invalid origin '${i.origin}' for comlink proxy`);return}const{id:o,type:l,path:u}=Object.assign({path:[]},i.data),s=(i.data.argumentList||[]).map(Xt);let a;try{const h=u.slice(0,-1).reduce((p,y)=>p[y],e),m=u.reduce((p,y)=>p[y],e);switch(l){case"GET":a=m;break;case"SET":h[u.slice(-1)[0]]=Xt(i.data.value),a=!0;break;case"APPLY":a=m.apply(h,s);break;case"CONSTRUCT":{const p=new m(...s);a=Zv(p)}break;case"ENDPOINT":{const{port1:p,port2:y}=new MessageChannel;kp(e,y),a=Jv(p,[p])}break;case"RELEASE":a=void 0;break;default:return}}catch(h){a={value:h,[Di]:0}}Promise.resolve(a).catch(h=>({value:h,[Di]:0})).then(h=>{const[m,p]=vo(h);t.postMessage(Object.assign(Object.assign({},m),{id:o}),p),l==="RELEASE"&&(t.removeEventListener("message",r),_p(t),xl in e&&typeof e[xl]=="function"&&e[xl]())}).catch(h=>{const[m,p]=vo({value:new TypeError("Unserializable return value"),[Di]:0});t.postMessage(Object.assign(Object.assign({},m),{id:o}),p)})}),t.start&&t.start()}function Gv(e){return e.constructor.name==="MessagePort"}function _p(e){Gv(e)&&e.close()}function Ep(e,t){return zu(e,[],t)}function Si(e){if(e)throw new Error("Proxy has been released and is not useable")}function xp(e){return mn(e,{type:"RELEASE"}).then(()=>{_p(e)})}const go=new WeakMap,yo="FinalizationRegistry"in globalThis&&new FinalizationRegistry(e=>{const t=(go.get(e)||0)-1;go.set(e,t),t===0&&xp(e)});function Kv(e,t){const n=(go.get(t)||0)+1;go.set(t,n),yo&&yo.register(e,t,e)}function Xv(e){yo&&yo.unregister(e)}function zu(e,t=[],n=function(){}){let r=!1;const i=new Proxy(n,{get(o,l){if(Si(r),l===Hv)return()=>{Xv(i),xp(e),r=!0};if(l==="then"){if(t.length===0)return{then:()=>i};const u=mn(e,{type:"GET",path:t.map(s=>s.toString())}).then(Xt);return u.then.bind(u)}return zu(e,[...t,l])},set(o,l,u){Si(r);const[s,a]=vo(u);return mn(e,{type:"SET",path:[...t,l].map(h=>h.toString()),value:s},a).then(Xt)},apply(o,l,u){Si(r);const s=t[t.length-1];if(s===Wv)return mn(e,{type:"ENDPOINT"}).then(Xt);if(s==="bind")return zu(e,t.slice(0,-1));const[a,h]=wc(u);return mn(e,{type:"APPLY",path:t.map(m=>m.toString()),argumentList:a},h).then(Xt)},construct(o,l){Si(r);const[u,s]=wc(l);return mn(e,{type:"CONSTRUCT",path:t.map(a=>a.toString()),argumentList:u},s).then(Xt)}});return Kv(i,e),i}function qv(e){return Array.prototype.concat.apply([],e)}function wc(e){const t=e.map(vo);return[t.map(n=>n[0]),qv(t.map(n=>n[1]))]}const Cp=new WeakMap;function Jv(e,t){return Cp.set(e,t),e}function Zv(e){return Object.assign(e,{[vp]:!0})}function vo(e){for(const[t,n]of Sp)if(n.canHandle(e)){const[r,i]=n.serialize(e);return[{type:"HANDLER",name:t,value:r},i]}return[{type:"RAW",value:e},Cp.get(e)||[]]}function Xt(e){switch(e.type){case"HANDLER":return Sp.get(e.name).deserialize(e.value);case"RAW":return e.value}}function mn(e,t,n){return new Promise(r=>{const i=bv();e.addEventListener("message",function o(l){!l.data||!l.data.id||l.data.id!==i||(e.removeEventListener("message",o),r(l.data))}),e.start&&e.start(),e.postMessage(Object.assign({id:i},t),n)})}function bv(){return new Array(4).fill(0).map(()=>Math.floor(Math.random()*Number.MAX_SAFE_INTEGER).toString(16)).join("-")}const e0=Ep(new Worker(new URL("/diorama-2023/assets/worker-5821772f.js",self.location))),t0=(e,t,n)=>e0.findSolution(e,t,n),n0=(e,t)=>{const[n,r]=F.useState(0),{subscribe:i}=F.useContext(Wo),[o,l]=Mv(async()=>t0(e,t),[e.length,t.width,t.height,n]);return F.useLayoutEffect(()=>{t.width>0&&t.height>0&&l()},[l,e.length,t.width,t.height,n]),js(()=>i(Yr.refresh,()=>r(Xd))),o},Sc=ty(cy(Kd)),r0=e=>{const{subscribe:t}=F.useContext(Wo),[n,r]=F.useState(Kd(wy,e));return js(()=>{const i=t(Yr.addImage,()=>r(tc(rc,Xd,Sc(e)))),o=t(Yr.removeImage,()=>r(tc(rc,ry,Ng(1),Sc(e))));return()=>{i(),o()}}),n},i0=({picture:{position:e,dimension:t,url:n},scaleX:r,scaleY:i})=>A.jsx("li",{style:{backgroundImage:`url(${n})`,left:e.x*r,top:e.y*i,width:t.width*r,height:t.height*i}}),o0=(e,t)=>{let n;return(...r)=>{n&&clearTimeout(n),n=setTimeout(()=>{t(...r)},e)}},l0=e=>({width:e.clientWidth,height:e.clientHeight}),u0=()=>{const e=F.useRef(null),[t,n]=F.useState({width:0,height:0});return F.useLayoutEffect(()=>{var l;const r=(l=e.current)==null?void 0:l.parentElement;if(!r)return;const i=o0(50,()=>{const u=l0(r);F.startTransition(()=>{n(s=>Hd(s,u)?s:u)})}),o=new ResizeObserver(i);return o.observe(r),()=>o.disconnect()},[e]),[e,t]},s0=Bo.ol` position: relative; overflow: hidden; width: 100%; diff --git a/docs/assets/worker-21e242e5.js b/docs/assets/worker-21e242e5.js deleted file mode 100644 index 9655150..0000000 --- a/docs/assets/worker-21e242e5.js +++ /dev/null @@ -1,5 +0,0 @@ -var fr=Object.defineProperty;var lr=(m,d,v)=>d in m?fr(m,d,{enumerable:!0,configurable:!0,writable:!0,value:v}):m[d]=v;var vt=(m,d,v)=>(lr(m,typeof d!="symbol"?d+"":d,v),v);(function(){"use strict";/** - * @license - * Copyright 2019 Google LLC - * SPDX-License-Identifier: Apache-2.0 - */const m=Symbol("Comlink.proxy"),d=Symbol("Comlink.endpoint"),v=Symbol("Comlink.releaseProxy"),P=Symbol("Comlink.finalizer"),T=Symbol("Comlink.thrown"),W=e=>typeof e=="object"&&e!==null||typeof e=="function",wt={canHandle:e=>W(e)&&e[m],serialize(e){const{port1:t,port2:r}=new MessageChannel;return z(e,t),[r,[r]]},deserialize(e){return e.start(),At(e)}},St={canHandle:e=>W(e)&&T in e,serialize({value:e}){let t;return e instanceof Error?t={isError:!0,value:{message:e.message,name:e.name,stack:e.stack}}:t={isError:!1,value:e},[t,[]]},deserialize(e){throw e.isError?Object.assign(new Error(e.value.message),e.value):e.value}},V=new Map([["proxy",wt],["throw",St]]);function _t(e,t){for(const r of e)if(t===r||r==="*"||r instanceof RegExp&&r.test(t))return!0;return!1}function z(e,t=globalThis,r=["*"]){t.addEventListener("message",function n(a){if(!a||!a.data)return;if(!_t(r,a.origin)){console.warn(`Invalid origin '${a.origin}' for comlink proxy`);return}const{id:u,type:o,path:i}=Object.assign({path:[]},a.data),s=(a.data.argumentList||[]).map(w);let c;try{const g=i.slice(0,-1).reduce((y,R)=>y[R],e),h=i.reduce((y,R)=>y[R],e);switch(o){case"GET":c=h;break;case"SET":g[i.slice(-1)[0]]=w(a.data.value),c=!0;break;case"APPLY":c=h.apply(g,s);break;case"CONSTRUCT":{const y=new h(...s);c=Tt(y)}break;case"ENDPOINT":{const{port1:y,port2:R}=new MessageChannel;z(e,R),c=Rt(y,[y])}break;case"RELEASE":c=void 0;break;default:return}}catch(g){c={value:g,[T]:0}}Promise.resolve(c).catch(g=>({value:g,[T]:0})).then(g=>{const[h,y]=I(g);t.postMessage(Object.assign(Object.assign({},h),{id:u}),y),o==="RELEASE"&&(t.removeEventListener("message",n),X(t),P in e&&typeof e[P]=="function"&&e[P]())}).catch(g=>{const[h,y]=I({value:new TypeError("Unserializable return value"),[T]:0});t.postMessage(Object.assign(Object.assign({},h),{id:u}),y)})}),t.start&&t.start()}function Ot(e){return e.constructor.name==="MessagePort"}function X(e){Ot(e)&&e.close()}function At(e,t){return x(e,[],t)}function M(e){if(e)throw new Error("Proxy has been released and is not useable")}function G(e){return S(e,{type:"RELEASE"}).then(()=>{X(e)})}const $=new WeakMap,k="FinalizationRegistry"in globalThis&&new FinalizationRegistry(e=>{const t=($.get(e)||0)-1;$.set(e,t),t===0&&G(e)});function jt(e,t){const r=($.get(t)||0)+1;$.set(t,r),k&&k.register(e,t,e)}function Et(e){k&&k.unregister(e)}function x(e,t=[],r=function(){}){let n=!1;const a=new Proxy(r,{get(u,o){if(M(n),o===v)return()=>{Et(a),G(e),n=!0};if(o==="then"){if(t.length===0)return{then:()=>a};const i=S(e,{type:"GET",path:t.map(s=>s.toString())}).then(w);return i.then.bind(i)}return x(e,[...t,o])},set(u,o,i){M(n);const[s,c]=I(i);return S(e,{type:"SET",path:[...t,o].map(g=>g.toString()),value:s},c).then(w)},apply(u,o,i){M(n);const s=t[t.length-1];if(s===d)return S(e,{type:"ENDPOINT"}).then(w);if(s==="bind")return x(e,t.slice(0,-1));const[c,g]=Y(i);return S(e,{type:"APPLY",path:t.map(h=>h.toString()),argumentList:c},g).then(w)},construct(u,o){M(n);const[i,s]=Y(o);return S(e,{type:"CONSTRUCT",path:t.map(c=>c.toString()),argumentList:i},s).then(w)}});return jt(a,e),a}function bt(e){return Array.prototype.concat.apply([],e)}function Y(e){const t=e.map(I);return[t.map(r=>r[0]),bt(t.map(r=>r[1]))]}const Z=new WeakMap;function Rt(e,t){return Z.set(e,t),e}function Tt(e){return Object.assign(e,{[m]:!0})}function I(e){for(const[t,r]of V)if(r.canHandle(e)){const[n,a]=r.serialize(e);return[{type:"HANDLER",name:t,value:n},a]}return[{type:"RAW",value:e},Z.get(e)||[]]}function w(e){switch(e.type){case"HANDLER":return V.get(e.name).deserialize(e.value);case"RAW":return e.value}}function S(e,t,r){return new Promise(n=>{const a=Mt();e.addEventListener("message",function u(o){!o.data||!o.data.id||o.data.id!==a||(e.removeEventListener("message",u),n(o.data))}),e.start&&e.start(),e.postMessage(Object.assign({id:a},t),r)})}function Mt(){return new Array(4).fill(0).map(()=>Math.floor(Math.random()*Number.MAX_SAFE_INTEGER).toString(16)).join("-")}function f(e){return e!=null&&typeof e=="object"&&e["@@functional/placeholder"]===!0}function p(e){return function t(r){return arguments.length===0||f(r)?t:e.apply(this,arguments)}}function l(e){return function t(r,n){switch(arguments.length){case 0:return t;case 1:return f(r)?t:p(function(a){return e(r,a)});default:return f(r)&&f(n)?t:f(r)?p(function(a){return e(a,n)}):f(n)?p(function(a){return e(r,a)}):e(r,n)}}}var $t=l(function(t,r){return Number(t)+Number(r)}),kt=$t;function L(e,t){switch(e){case 0:return function(){return t.apply(this,arguments)};case 1:return function(r){return t.apply(this,arguments)};case 2:return function(r,n){return t.apply(this,arguments)};case 3:return function(r,n,a){return t.apply(this,arguments)};case 4:return function(r,n,a,u){return t.apply(this,arguments)};case 5:return function(r,n,a,u,o){return t.apply(this,arguments)};case 6:return function(r,n,a,u,o,i){return t.apply(this,arguments)};case 7:return function(r,n,a,u,o,i,s){return t.apply(this,arguments)};case 8:return function(r,n,a,u,o,i,s,c){return t.apply(this,arguments)};case 9:return function(r,n,a,u,o,i,s,c,g){return t.apply(this,arguments)};case 10:return function(r,n,a,u,o,i,s,c,g,h){return t.apply(this,arguments)};default:throw new Error("First argument to _arity must be a non-negative integer no greater than ten")}}function J(e,t,r){return function(){for(var n=[],a=0,u=e,o=0,i=!1;o=arguments.length)?s=t[o]:(s=arguments[a],a+=1),n[o]=s,f(s)?i=!0:u-=1,o+=1}return!i&&u<=0?r.apply(this,n):L(Math.max(0,u),J(e,n,r))}}var It=l(function(t,r){return t===1?p(r):L(t,J(t,[],r))}),Q=It;function Ct(e){return function t(r,n,a){switch(arguments.length){case 0:return t;case 1:return f(r)?t:l(function(u,o){return e(r,u,o)});case 2:return f(r)&&f(n)?t:f(r)?l(function(u,o){return e(u,n,o)}):f(n)?l(function(u,o){return e(r,u,o)}):p(function(u){return e(r,n,u)});default:return f(r)&&f(n)&&f(a)?t:f(r)&&f(n)?l(function(u,o){return e(u,o,a)}):f(r)&&f(a)?l(function(u,o){return e(u,n,o)}):f(n)&&f(a)?l(function(u,o){return e(r,u,o)}):f(r)?p(function(u){return e(u,n,a)}):f(n)?p(function(u){return e(r,u,a)}):f(a)?p(function(u){return e(r,n,u)}):e(r,n,a)}}}var O=Array.isArray||function(t){return t!=null&&t.length>=0&&Object.prototype.toString.call(t)==="[object Array]"};function Nt(e){return e!=null&&typeof e["@@transducer/step"]=="function"}function K(e,t,r){return function(){if(arguments.length===0)return r();var n=arguments[arguments.length-1];if(!O(n)){for(var a=0;a=0;)r=nt[n],A(r,t)&&!Ft(a,r)&&(a[a.length]=r),n-=1;return a}),_=Ut,Ht=p(function(t){return t===null?"Null":t===void 0?"Undefined":Object.prototype.toString.call(t).slice(8,-1)}),ut=Ht;function ot(e,t,r,n){var a=tt(e),u=tt(t);function o(i,s){return F(i,s,r.slice(),n.slice())}return!et(function(i,s){return!et(o,s,i)},u,a)}function F(e,t,r,n){if(q(e,t))return!0;var a=ut(e);if(a!==ut(t))return!1;if(typeof e["fantasy-land/equals"]=="function"||typeof t["fantasy-land/equals"]=="function")return typeof e["fantasy-land/equals"]=="function"&&e["fantasy-land/equals"](t)&&typeof t["fantasy-land/equals"]=="function"&&t["fantasy-land/equals"](e);if(typeof e.equals=="function"||typeof t.equals=="function")return typeof e.equals=="function"&&e.equals(t)&&typeof t.equals=="function"&&t.equals(e);switch(a){case"Arguments":case"Array":case"Object":if(typeof e.constructor=="function"&&Pt(e.constructor)==="Promise")return e===t;break;case"Boolean":case"Number":case"String":if(!(typeof e==typeof t&&q(e.valueOf(),t.valueOf())))return!1;break;case"Date":if(!q(e.valueOf(),t.valueOf()))return!1;break;case"Error":return e.name===t.name&&e.message===t.message;case"RegExp":if(!(e.source===t.source&&e.global===t.global&&e.ignoreCase===t.ignoreCase&&e.multiline===t.multiline&&e.sticky===t.sticky&&e.unicode===t.unicode))return!1;break}for(var u=r.length-1;u>=0;){if(r[u]===e)return n[u]===t;u-=1}switch(a){case"Map":return e.size!==t.size?!1:ot(e.entries(),t.entries(),r.concat([e]),n.concat([t]));case"Set":return e.size!==t.size?!1:ot(e.values(),t.values(),r.concat([e]),n.concat([t]));case"Arguments":case"Array":case"Object":case"Boolean":case"Number":case"String":case"Date":case"Error":case"RegExp":case"Int8Array":case"Uint8Array":case"Uint8ClampedArray":case"Int16Array":case"Uint16Array":case"Int32Array":case"Uint32Array":case"Float32Array":case"Float64Array":case"ArrayBuffer":break;default:return!1}var o=_(e);if(o.length!==_(t).length)return!1;var i=r.concat([e]),s=n.concat([t]);for(u=o.length-1;u>=0;){var c=o[u];if(!(A(c,t)&&F(t[c],e[c],i,s)))return!1;u-=1}return!0}var Bt=l(function(t,r){return F(t,r,[],[])}),Dt=Bt;function Wt(e,t,r){var n,a;if(typeof e.indexOf=="function")switch(typeof t){case"number":if(t===0){for(n=1/t;r=0}function j(e,t){for(var r=0,n=t.length,a=Array(n);r":ct(o,i)},n=function(u,o){return j(function(i){return U(i)+": "+r(u[i])},o.slice().sort())};switch(Object.prototype.toString.call(e)){case"[object Arguments]":return"(function() { return arguments; }("+j(r,e).join(", ")+"))";case"[object Array]":return"["+j(r,e).concat(n(e,re(function(u){return/^\d+$/.test(u)},_(e)))).join(", ")+"]";case"[object Boolean]":return typeof e=="object"?"new Boolean("+r(e.valueOf())+")":e.toString();case"[object Date]":return"new Date("+(isNaN(e.valueOf())?r(NaN):U(Gt(e)))+")";case"[object Map]":return"new Map("+r(Array.from(e))+")";case"[object Null]":return"null";case"[object Number]":return typeof e=="object"?"new Number("+r(e.valueOf())+")":1/e===-1/0?"-0":e.toString(10);case"[object Set]":return"new Set("+r(Array.from(e).sort())+")";case"[object String]":return typeof e=="object"?"new String("+r(e.valueOf())+")":U(e);case"[object Undefined]":return"undefined";default:if(typeof e.toString=="function"){var a=e.toString();if(a!=="[object Object]")return a}return"{"+n(e,_(e)).join(", ")+"}"}}var ne=p(function(t){return ct(t,[])}),b=ne,ae=l(function(t,r){if(t===r)return r;function n(s,c){if(s>c!=c>s)return c>s?c:s}var a=n(t,r);if(a!==void 0)return a;var u=n(typeof t,typeof r);if(u!==void 0)return u===typeof t?t:r;var o=b(t),i=n(o,b(r));return i!==void 0&&i===o?t:r}),ue=ae,oe=function(){function e(t,r){this.xf=r,this.f=t}return e.prototype["@@transducer/init"]=C.init,e.prototype["@@transducer/result"]=C.result,e.prototype["@@transducer/step"]=function(t,r){return this.xf["@@transducer/step"](t,this.f(r))},e}(),ie=function(t){return function(r){return new oe(t,r)}},se=ie,ce=l(K(["fantasy-land/map","map"],se,function(t,r){switch(Object.prototype.toString.call(r)){case"[object Function]":return Q(r.length,function(){return t.call(this,r.apply(this,arguments))});case"[object Object]":return it(function(n,a){return n[a]=t(r[a]),n},{},_(r));default:return j(t,r)}})),fe=ce,le=Number.isInteger||function(t){return t<<0===t};function N(e){return Object.prototype.toString.call(e)==="[object String]"}var ge=l(function(t,r){var n=t<0?r.length+t:t;return N(r)?r.charAt(n):r[n]}),ft=ge,pe=l(function(t,r){if(r!=null)return le(t)?ft(t,r):r[t]}),lt=pe,he=l(function(t,r){return fe(lt(t),r)}),ye=he,de=p(function(t){return O(t)?!0:!t||typeof t!="object"||N(t)?!1:t.length===0?!0:t.length>0?t.hasOwnProperty(0)&&t.hasOwnProperty(t.length-1):!1}),me=de,gt=typeof Symbol<"u"?Symbol.iterator:"@@iterator";function ve(e,t,r){return function(a,u,o){if(me(o))return e(a,u,o);if(o==null)return u;if(typeof o["fantasy-land/reduce"]=="function")return t(a,u,o,"fantasy-land/reduce");if(o[gt]!=null)return r(a,u,o[gt]());if(typeof o.next=="function")return r(a,u,o);if(typeof o.reduce=="function")return t(a,u,o,"reduce");throw new TypeError("reduce: list must be array or iterable")}}function we(e,t,r){for(var n=0,a=r.length;no?1:0})}),yt=Ye;Ge(1,18).map(e=>`./images/${e}.jpg`);const Ze=600,Je=1e4,Qe=.98,Ke=4,tr=e=>e.length>0,er=(e,t)=>{const r=e.height*t;if(r<=e.width)return{width:r,height:e.height};{const n=e.width/t;return{width:e.width,height:n}}},rr=(e,t)=>{const n=yt(u=>(u.aspectRatioDelta+u.sizeHomogeneity*t.sizeHomogeneity)/(t.sizeHomogeneity+1),e),a=ze(n);return Le({pictures:yt(lt("url"))},a)},nr=e=>"url"in e,B=(e,t,r)=>{if(nr(r))return[{position:e,dimension:t,url:r.url}];const n=r.horizontal?t.height*r.first.aspectRatio:t.width/r.first.aspectRatio,a=r.horizontal?{x:e.x+n,y:e.y}:{x:e.x,y:e.y+n},u=r.horizontal?{width:n,height:t.height}:{width:t.width,height:n},o=r.horizontal?{width:Math.abs(t.width-n),height:t.height}:{width:t.width,height:Math.abs(t.height-n)};return $e(B(e,u,r.first),B(a,o,r.second))},ar=Ie(Ne,[De,He]),ur=e=>{const t=Math.max(...e.map(u=>u.width)),r=Math.max(...e.map(u=>u.height)),n=Math.min(...e.map(u=>u.width)),a=Math.min(...e.map(u=>u.height));return ar([n/t,a/r])},or=(e,t,r)=>{const n=B({x:0,y:0},e,r),a=ur(n.map(u=>u.dimension));return{dimension:e,aspectRatioDelta:t,sizeHomogeneity:a,pictures:n}},D=(e,t)=>e+Math.random()*(t-e+1)|0;class ir{constructor(t,r,n){vt(this,"aspectRatio",0);this.horizontal=t,this.first=r,this.second=n;const a=r.aspectRatio+n.aspectRatio;this.aspectRatio=t?a:r.aspectRatio*n.aspectRatio/a}}const dt=(e,t)=>new ir(!!D(0,1),e,t),mt=e=>{const t=e.length-1;if(t===0)return e[0];if(t===1)return dt(e[0],e[1]);const r=D(0,t);var n;do n=D(0,t);while(r===n);const a=dt(e[r],e[n]);return mt(e.with(r,a).toSpliced(n,1))},sr={aspectRatioThreshold:Qe,maxComputationTime:Ze,minImages:Ke,sizeHomogeneity:Je};var cr=Object.freeze({__proto__:null,findSolution:(e,t,r)=>{const n=Ve(r??{},sr),a=Date.now(),u=t.width/t.height,o=[];for(;Date.now()-an.aspectRatioThreshold){const g=er(t,i.aspectRatio),h=or(g,c,i);o.push(h)}}if(!tr(o))throw new Error("No solution");return rr(o,n)}});z(cr)})(); diff --git a/docs/assets/worker-5821772f.js b/docs/assets/worker-5821772f.js new file mode 100644 index 0000000..134de1c --- /dev/null +++ b/docs/assets/worker-5821772f.js @@ -0,0 +1,5 @@ +var Te=Object.defineProperty;var Re=(y,p,m)=>p in y?Te(y,p,{enumerable:!0,configurable:!0,writable:!0,value:m}):y[p]=m;var lt=(y,p,m)=>(Re(y,typeof p!="symbol"?p+"":p,m),m);(function(){"use strict";/** + * @license + * Copyright 2019 Google LLC + * SPDX-License-Identifier: Apache-2.0 + */const y=Symbol("Comlink.proxy"),p=Symbol("Comlink.endpoint"),m=Symbol("Comlink.releaseProxy"),I=Symbol("Comlink.finalizer"),j=Symbol("Comlink.thrown"),F=t=>typeof t=="object"&&t!==null||typeof t=="function",ft={canHandle:t=>F(t)&&t[y],serialize(t){const{port1:e,port2:n}=new MessageChannel;return M(t,e),[n,[n]]},deserialize(t){return t.start(),yt(t)}},gt={canHandle:t=>F(t)&&j in t,serialize({value:t}){let e;return t instanceof Error?e={isError:!0,value:{message:t.message,name:t.name,stack:t.stack}}:e={isError:!1,value:t},[e,[]]},deserialize(t){throw t.isError?Object.assign(new Error(t.value.message),t.value):t.value}},D=new Map([["proxy",ft],["throw",gt]]);function ht(t,e){for(const n of t)if(e===n||n==="*"||n instanceof RegExp&&n.test(e))return!0;return!1}function M(t,e=globalThis,n=["*"]){e.addEventListener("message",function r(o){if(!o||!o.data)return;if(!ht(n,o.origin)){console.warn(`Invalid origin '${o.origin}' for comlink proxy`);return}const{id:a,type:i,path:s}=Object.assign({path:[]},o.data),u=(o.data.argumentList||[]).map(d);let c;try{const l=s.slice(0,-1).reduce((f,w)=>f[w],t),g=s.reduce((f,w)=>f[w],t);switch(i){case"GET":c=g;break;case"SET":l[s.slice(-1)[0]]=d(o.data.value),c=!0;break;case"APPLY":c=g.apply(l,u);break;case"CONSTRUCT":{const f=new g(...u);c=St(f)}break;case"ENDPOINT":{const{port1:f,port2:w}=new MessageChannel;M(t,w),c=wt(f,[f])}break;case"RELEASE":c=void 0;break;default:return}}catch(l){c={value:l,[j]:0}}Promise.resolve(c).catch(l=>({value:l,[j]:0})).then(l=>{const[g,f]=z(l);e.postMessage(Object.assign(Object.assign({},g),{id:a}),f),i==="RELEASE"&&(e.removeEventListener("message",r),V(e),I in t&&typeof t[I]=="function"&&t[I]())}).catch(l=>{const[g,f]=z({value:new TypeError("Unserializable return value"),[j]:0});e.postMessage(Object.assign(Object.assign({},g),{id:a}),f)})}),e.start&&e.start()}function pt(t){return t.constructor.name==="MessagePort"}function V(t){pt(t)&&t.close()}function yt(t,e){return P(t,[],e)}function _(t){if(t)throw new Error("Proxy has been released and is not useable")}function B(t){return S(t,{type:"RELEASE"}).then(()=>{V(t)})}const T=new WeakMap,R="FinalizationRegistry"in globalThis&&new FinalizationRegistry(t=>{const e=(T.get(t)||0)-1;T.set(t,e),e===0&&B(t)});function mt(t,e){const n=(T.get(e)||0)+1;T.set(e,n),R&&R.register(t,e,t)}function dt(t){R&&R.unregister(t)}function P(t,e=[],n=function(){}){let r=!1;const o=new Proxy(n,{get(a,i){if(_(r),i===m)return()=>{dt(o),B(t),r=!0};if(i==="then"){if(e.length===0)return{then:()=>o};const s=S(t,{type:"GET",path:e.map(u=>u.toString())}).then(d);return s.then.bind(s)}return P(t,[...e,i])},set(a,i,s){_(r);const[u,c]=z(s);return S(t,{type:"SET",path:[...e,i].map(l=>l.toString()),value:u},c).then(d)},apply(a,i,s){_(r);const u=e[e.length-1];if(u===p)return S(t,{type:"ENDPOINT"}).then(d);if(u==="bind")return P(t,e.slice(0,-1));const[c,l]=W(s);return S(t,{type:"APPLY",path:e.map(g=>g.toString()),argumentList:c},l).then(d)},construct(a,i){_(r);const[s,u]=W(i);return S(t,{type:"CONSTRUCT",path:e.map(c=>c.toString()),argumentList:s},u).then(d)}});return mt(o,t),o}function vt(t){return Array.prototype.concat.apply([],t)}function W(t){const e=t.map(z);return[e.map(n=>n[0]),vt(e.map(n=>n[1]))]}const G=new WeakMap;function wt(t,e){return G.set(t,e),t}function St(t){return Object.assign(t,{[y]:!0})}function z(t){for(const[e,n]of D)if(n.canHandle(t)){const[r,o]=n.serialize(t);return[{type:"HANDLER",name:e,value:r},o]}return[{type:"RAW",value:t},G.get(t)||[]]}function d(t){switch(t.type){case"HANDLER":return D.get(t.name).deserialize(t.value);case"RAW":return t.value}}function S(t,e,n){return new Promise(r=>{const o=Ot();t.addEventListener("message",function a(i){!i.data||!i.data.id||i.data.id!==o||(t.removeEventListener("message",a),r(i.data))}),t.start&&t.start(),t.postMessage(Object.assign({id:o},e),n)})}function Ot(){return new Array(4).fill(0).map(()=>Math.floor(Math.random()*Number.MAX_SAFE_INTEGER).toString(16)).join("-")}function O(t){return t!=null&&typeof t=="object"&&t["@@functional/placeholder"]===!0}function v(t){return function e(n){return arguments.length===0||O(n)?e:t.apply(this,arguments)}}function h(t){return function e(n,r){switch(arguments.length){case 0:return e;case 1:return O(n)?e:v(function(o){return t(n,o)});default:return O(n)&&O(r)?e:O(n)?v(function(o){return t(o,r)}):O(r)?v(function(o){return t(n,o)}):t(n,r)}}}var C=Array.isArray||function(e){return e!=null&&e.length>=0&&Object.prototype.toString.call(e)==="[object Array]"};function At(t){return t!=null&&typeof t["@@transducer/step"]=="function"}function Et(t,e,n){return function(){if(arguments.length===0)return n();var r=arguments[arguments.length-1];if(!C(r)){for(var o=0;o=0;)n=Q[r],A(n,e)&&!zt(o,n)&&(o[o.length]=n),r-=1;return o}),E=Ct,It=v(function(e){return e===null?"Null":e===void 0?"Undefined":Object.prototype.toString.call(e).slice(8,-1)}),tt=It;function et(t,e,n,r){var o=Y(t),a=Y(e);function i(s,u){return N(s,u,n.slice(),r.slice())}return!Z(function(s,u){return!Z(i,u,s)},a,o)}function N(t,e,n,r){if(k(t,e))return!0;var o=tt(t);if(o!==tt(e))return!1;if(typeof t["fantasy-land/equals"]=="function"||typeof e["fantasy-land/equals"]=="function")return typeof t["fantasy-land/equals"]=="function"&&t["fantasy-land/equals"](e)&&typeof e["fantasy-land/equals"]=="function"&&e["fantasy-land/equals"](t);if(typeof t.equals=="function"||typeof e.equals=="function")return typeof t.equals=="function"&&t.equals(e)&&typeof e.equals=="function"&&e.equals(t);switch(o){case"Arguments":case"Array":case"Object":if(typeof t.constructor=="function"&&bt(t.constructor)==="Promise")return t===e;break;case"Boolean":case"Number":case"String":if(!(typeof t==typeof e&&k(t.valueOf(),e.valueOf())))return!1;break;case"Date":if(!k(t.valueOf(),e.valueOf()))return!1;break;case"Error":return t.name===e.name&&t.message===e.message;case"RegExp":if(!(t.source===e.source&&t.global===e.global&&t.ignoreCase===e.ignoreCase&&t.multiline===e.multiline&&t.sticky===e.sticky&&t.unicode===e.unicode))return!1;break}for(var a=n.length-1;a>=0;){if(n[a]===t)return r[a]===e;a-=1}switch(o){case"Map":return t.size!==e.size?!1:et(t.entries(),e.entries(),n.concat([t]),r.concat([e]));case"Set":return t.size!==e.size?!1:et(t.values(),e.values(),n.concat([t]),r.concat([e]));case"Arguments":case"Array":case"Object":case"Boolean":case"Number":case"String":case"Date":case"Error":case"RegExp":case"Int8Array":case"Uint8Array":case"Uint8ClampedArray":case"Int16Array":case"Uint16Array":case"Int32Array":case"Uint32Array":case"Float32Array":case"Float64Array":case"ArrayBuffer":break;default:return!1}var i=E(t);if(i.length!==E(e).length)return!1;var s=n.concat([t]),u=r.concat([e]);for(a=i.length-1;a>=0;){var c=i[a];if(!(A(c,e)&&N(e[c],t[c],s,u)))return!1;a-=1}return!0}var Mt=h(function(e,n){return N(e,n,[],[])}),Pt=Mt;function kt(t,e,n){var r,o;if(typeof t.indexOf=="function")switch(typeof e){case"number":if(e===0){for(r=1/e;n=0}function H(t,e){for(var n=0,r=e.length,o=Array(r);n":rt(i,s)},r=function(a,i){return H(function(s){return $(s)+": "+n(a[s])},i.slice().sort())};switch(Object.prototype.toString.call(t)){case"[object Arguments]":return"(function() { return arguments; }("+H(n,t).join(", ")+"))";case"[object Array]":return"["+H(n,t).concat(r(t,Wt(function(a){return/^\d+$/.test(a)},E(t)))).join(", ")+"]";case"[object Boolean]":return typeof t=="object"?"new Boolean("+n(t.valueOf())+")":t.toString();case"[object Date]":return"new Date("+(isNaN(t.valueOf())?n(NaN):$($t(t)))+")";case"[object Map]":return"new Map("+n(Array.from(t))+")";case"[object Null]":return"null";case"[object Number]":return typeof t=="object"?"new Number("+n(t.valueOf())+")":1/t===-1/0?"-0":t.toString(10);case"[object Set]":return"new Set("+n(Array.from(t).sort())+")";case"[object String]":return typeof t=="object"?"new String("+n(t.valueOf())+")":$(t);case"[object Undefined]":return"undefined";default:if(typeof t.toString=="function"){var o=t.toString();if(o!=="[object Object]")return o}return"{"+r(t,E(t)).join(", ")+"}"}}var Gt=v(function(e){return rt(e,[])}),q=Gt,Xt=Number.isInteger||function(e){return e<<0===e};function L(t){return Object.prototype.toString.call(t)==="[object String]"}var Yt=h(function(e,n){var r=e<0?n.length+e:e;return L(n)?n.charAt(r):n[r]}),ot=Yt,Zt=h(function(e,n){if(n!=null)return Xt(e)?ot(e,n):n[e]}),Jt=Zt;function at(t){var e=Object.prototype.toString.call(t);return e==="[object Function]"||e==="[object AsyncFunction]"||e==="[object GeneratorFunction]"||e==="[object AsyncGeneratorFunction]"}var Qt=h(function(e,n){if(C(e)){if(C(n))return e.concat(n);throw new TypeError(q(n)+" is not an array")}if(L(e)){if(L(n))return e+n;throw new TypeError(q(n)+" is not a string")}if(e!=null&&at(e["fantasy-land/concat"]))return e["fantasy-land/concat"](n);if(e!=null&&at(e.concat))return e.concat(n);throw new TypeError(q(e)+' does not have a method named "concat" or "fantasy-land/concat"')}),Kt=Qt,te=ot(-1),ee=te,ne=h(function t(e,n){if(!nt(n)&&!C(n))return n;var r=n instanceof Array?[]:{},o,a,i;for(a in n)o=e[a],i=typeof o,r[a]=i==="function"?o(n[a]):o&&i==="object"?t(o,n[a]):n[a];return r}),re=ne;function oe(t){if(t==null)throw new TypeError("Cannot convert undefined or null to object");for(var e=Object(t),n=1,r=arguments.length;ni?1:0})}),st=le;ce(1,18).map(t=>`./images/${t}.jpg`);const fe=600,ge=1e4,he=.98,pe=4,ye=t=>t.length>0,me=(t,e)=>{const n=t.height*e;if(n<=t.width)return{width:n,height:t.height};{const r=t.width/e;return{width:t.width,height:r}}},de=(t,e)=>{const r=st(a=>(a.aspectRatioDelta+a.sizeHomogeneity*e.sizeHomogeneity)/(e.sizeHomogeneity+1),t),o=ee(r);return re({pictures:st(Jt("url"))},o)},ve=t=>"url"in t,x=(t,e,n)=>{if(ve(n))return[{position:t,dimension:e,url:n.url}];const r=e.height*n.first.aspectRatio,o=e.width/n.first.aspectRatio,a=n.horizontal?r:o,i={x:t.x+a,y:t.y},s={x:t.x,y:t.y+a},u={width:a,height:e.height},c={width:e.width,height:a},l=e.width-a,g=e.height-a,f={width:l,height:e.height},w={width:e.width,height:g},be=n.horizontal?i:s,je=n.horizontal?u:c,_e=n.horizontal?f:w;return Kt(x(t,je,n.first),x(be,_e,n.second))},we=t=>{const e=Math.max(...t.map(a=>a.width)),n=Math.max(...t.map(a=>a.height)),r=Math.min(...t.map(a=>a.width)),o=Math.min(...t.map(a=>a.height));return(r/e+o/n)/2},Se=(t,e,n)=>{const r=x({x:0,y:0},t,n),o=we(r.map(a=>a.dimension));return{dimension:t,aspectRatioDelta:e,sizeHomogeneity:o,pictures:r}},U=(t,e)=>t+Math.random()*(e-t+1)|0;class Oe{constructor(e,n,r){lt(this,"aspectRatio",0);this.horizontal=e,this.first=n,this.second=r;const o=n.aspectRatio+r.aspectRatio;this.aspectRatio=e?o:n.aspectRatio*r.aspectRatio/o}}const ut=(t,e)=>new Oe(!!U(0,1),t,e),ct=t=>{const e=t.length-1;if(e===0)return t[0];if(e===1)return ut(t[0],t[1]);const n=U(0,e);let r;do r=U(0,e);while(n===r);const o=ut(t[n],t[r]);return ct(t.with(n,o).toSpliced(r,1))},Ae={aspectRatioThreshold:he,maxComputationTime:fe,minImages:pe,sizeHomogeneity:ge};var Ee=Object.freeze({__proto__:null,findSolution:(t,e,n)=>{const r=se(n??{},Ae),o=Date.now(),a=e.width/e.height,i=[];let s=0;for(;Date.now()-or.aspectRatioThreshold){const g=me(e,u.aspectRatio),f=Se(g,l,u);i.push(f)}s++}if(console.log("cycles ran:",s),!ye(i))throw new Error("No solution");return de(i,r)}});M(Ee)})(); diff --git a/docs/index.html b/docs/index.html index 72d6e5e..2ec068c 100644 --- a/docs/index.html +++ b/docs/index.html @@ -6,7 +6,7 @@ - + diff --git a/src/layout/position-pictures.ts b/src/layout/position-pictures.ts index 684c0ce..f4c2240 100644 --- a/src/layout/position-pictures.ts +++ b/src/layout/position-pictures.ts @@ -12,21 +12,25 @@ export const positionPictures = ( return [{ position, dimension, url: rect.url }] } - const length: number = rect.horizontal - ? dimension.height * rect.first.aspectRatio - : dimension.width / rect.first.aspectRatio + const lengthHorizontal = dimension.height * rect.first.aspectRatio + const lengthVertical = dimension.width / rect.first.aspectRatio - const position2: Position = rect.horizontal - ? { x: position.x + length, y: position.y } - : { x: position.x, y: position.y + length } + const length = rect.horizontal ? lengthHorizontal : lengthVertical - const dimension1: Dimension = rect.horizontal - ? { width: length, height: dimension.height } - : { width: dimension.width, height: length } + const positionHorizontal = { x: position.x + length, y: position.y } + const positionVertical = { x: position.x, y: position.y + length } - const dimension2: Dimension = rect.horizontal - ? { width: Math.abs(dimension.width - length), height: dimension.height } - : { width: dimension.width, height: Math.abs(dimension.height - length) } + const dimensionHorizontal = { width: length, height: dimension.height } + const dimensionVertical = { width: dimension.width, height: length } + + const remainingWidth = dimension.width - length + const remainingHeight = dimension.height - length + const dimension2Horizontal = { width: remainingWidth, height: dimension.height } + const dimension2Vertical = { width: dimension.width, height: remainingHeight } + + const position2 = rect.horizontal ? positionHorizontal : positionVertical + const dimension1 = rect.horizontal ? dimensionHorizontal : dimensionVertical + const dimension2 = rect.horizontal ? dimension2Horizontal : dimension2Vertical return concat( positionPictures(position, dimension1, rect.first), diff --git a/src/layout/to-random-tree.ts b/src/layout/to-random-tree.ts index 0ecf459..be242e8 100644 --- a/src/layout/to-random-tree.ts +++ b/src/layout/to-random-tree.ts @@ -15,7 +15,7 @@ export const toRandomTree = (compositions: Rect[]): Composition => { } const index1 = randomInt(0, max) // eslint-disable-next-line no-var - var index2 + let index2 do { index2 = randomInt(0, max) } while(index1 === index2) diff --git a/src/layout/worker.ts b/src/layout/worker.ts index 45bb81d..eb9defb 100644 --- a/src/layout/worker.ts +++ b/src/layout/worker.ts @@ -23,6 +23,7 @@ export const findSolution = ( const start = Date.now() const arTarget = targetDimension.width / targetDimension.height const solutions: Solution[] = [] + let cycles = 0 // search possible solutions for a limited amount of time while (Date.now() - start < config.maxComputationTime) { @@ -36,7 +37,9 @@ export const findSolution = ( const finalLayout = positionSolution(actualDimensions, score, root) solutions.push(finalLayout) } + cycles++ } + console.log('cycles ran:', cycles) if (!isNotEmpty(solutions)) { throw new Error('No solution') } diff --git a/src/utils/average.spec.ts b/src/utils/average.spec.ts deleted file mode 100644 index 810c8a6..0000000 --- a/src/utils/average.spec.ts +++ /dev/null @@ -1,8 +0,0 @@ -import { average } from './average' - -describe('average', () => { - it('computes an average value', () => { - const arr = [2, 4, 6] - expect(average(arr)).toEqual(4) - }) -}) diff --git a/src/utils/average.ts b/src/utils/average.ts deleted file mode 100644 index b42c8b2..0000000 --- a/src/utils/average.ts +++ /dev/null @@ -1,8 +0,0 @@ -import { converge, divide, sum, length } from 'ramda' - -type NumFn = (arg: number[]) => T - -export const average: (arr: number[]) => number = converge, NumFn]>( - divide, - [sum, length] -) diff --git a/src/utils/size-variation.ts b/src/utils/size-variation.ts index f720b41..1878046 100644 --- a/src/utils/size-variation.ts +++ b/src/utils/size-variation.ts @@ -1,10 +1,9 @@ import type { Dimension } from '../types/types' -import { average } from './average' export const sizeVariation = (rects: Dimension[]): number => { const maxWidth = Math.max(...rects.map(r => r.width)) const maxHeight = Math.max(...rects.map(r => r.height)) const minWidth = Math.min(...rects.map(r => r.width)) const minHeight = Math.min(...rects.map(r => r.height)) - return average([minWidth / maxWidth, minHeight / maxHeight]) + return (minWidth / maxWidth + minHeight / maxHeight) / 2 }