@expofp/floorplan
Version:
Interactive floor plan library for expos and events
2 lines (1 loc) • 2.04 kB
JavaScript
export default class n{x1;x2;y1;y2;get h(){return Math.abs(this.y2-this.y1)}get w(){return Math.abs(this.x2-this.x1)}get cx(){return(this.x1+this.x2)/2}get cy(){return(this.y1+this.y2)/2}constructor(t,i,s,h){if(t>s){const r=t;t=s,s=r}if(i>h){const r=i;i=h,h=r}if(t>s||i>h)throw new Error(`Invalid rect ${t} ${s} ${i} ${h}`);this.x1=t,this.y1=i,this.x2=s,this.y2=h}static fromX1y1x2y2(t,i,s,h){return new n(t,i,s,h)}static fromXywh(t,i,s,h){return this.fromX1y1x2y2(t,i,t+s,i+h)}static fromCxcywh(t,i,s,h){return this.fromX1y1x2y2(t-s/2,i-h/2,t+s/2,i+h/2)}static fromXywhRect(t){return this.fromX1y1x2y2(t.x,t.y,t.x+t.w,t.y+t.h)}static fromSvgRectElement(t){return this.fromXywh(t.x.baseVal.value,t.y.baseVal.value,t.width.baseVal.value,t.height.baseVal.value)}static fromMultiple(t){return n.fromX1y1x2y2(Math.min(...t.map(i=>i.x1)),Math.min(...t.map(i=>i.y1)),Math.max(...t.map(i=>i.x2)),Math.max(...t.map(i=>i.y2)))}contains(t){return t.x1>=this.x1&&t.x2<=this.x2&&t.y1>=this.y1&&t.y2<=this.y2}intersects(t){const i=Math.max(this.x1,t.x1),s=Math.min(this.x2,t.x2),h=Math.max(this.y1,t.y1),r=Math.min(this.y2,t.y2);return s>=i&&r>=h}getIntersection(t){const i=Math.max(this.x1,t.x1),s=Math.min(this.x2,t.x2),h=Math.max(this.y1,t.y1),r=Math.min(this.y2,t.y2);return s>=i&&r>=h?n.fromX1y1x2y2(i,h,s,r):n.fromX1y1x2y2(0,0,0,0)}getRotated90(){return n.fromCxcywh(this.cx,this.cy,this.h,this.w)}normalize(t,i){const s=this.x1/t,h=this.x2/t,r=this.y1/i,y=this.y2/i;return n.fromX1y1x2y2(s,r,h,y)}withPadding(t,i=t){return this.w<t*2||this.h<i*2?this:n.fromCxcywh(this.cx,this.cy,this.w-t*2,this.h-i*2)}scale(t){return n.fromX1y1x2y2(this.x1*t,this.y1*t,this.x2*t,this.y2*t)}getArea(){return this.w*this.h}clone(){return new n(this.x1,this.y1,this.x2,this.y2)}translate(t,i){return this.x1+=t,this.x2+=t,this.y1+=i,this.y2+=i,this}equals(t){return t.x1===this.x1&&t.x2===this.x2&&t.y1===this.y1&&t.y2===this.y2}toString(){return this.x1+","+this.y1+","+this.w+","+this.h}containsPoint(t,i){return t>=this.x1&&t<=this.x2&&i>=this.y1&&i<=this.y2}}