UNPKG

@expofp/floorplan

Version:

Interactive floor plan library for expos and events

2 lines (1 loc) 707 B
function i(t,s,p){let r=0,a=1/0,c=1/0;const f=p.reduce((e,n)=>e+n,0);for(let e=0;e<s.length;e++){const n=s[e],o=p[e];if(t.layer&&n.p0.layer!==t.layer){r+=o;continue}const d=n.p1.x-n.p0.x,y=n.p1.y-n.p0.y,h=d*d+y*y;if(h<1e-10){r+=o;continue}const l=((t.x-n.p0.x)*d+(t.y-n.p0.y)*y)/h,u=Math.max(0,Math.min(1,l)),x=n.p0.x+d*u,S=n.p0.y+y*u,m=Math.hypot(t.x-x,t.y-S);m<a&&(a=m,c=r+o*u),r+=o}return{found:c!==1/0,fromStart:c,total:f}}export function normalizeRouteDirection(t,s,p){const r=t.map(o=>Math.hypot(o.p1.x-o.p0.x,o.p1.y-o.p0.y)),a=i(s,t,r),c=a.found?a:i(p,t,r);if(!c.found)return t;const f=c.fromStart,e=c.total-c.fromStart;return(a.found?e<f:f<e)?t.slice().reverse().map(o=>({...o,p0:o.p1,p1:o.p0})):t}