1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 | 1× 1× 1× 1× 1× 1× 527× 1× 5× 1× 1× 1× 1× 1× 428× 1× 2× 3152× 42× 42× 42× 42× 42× 3110× 3110× 3110× 3110× 1× 7× 37299× 1× 7× 21411× 1× 1× 1× 99× 1× 2× 1× 4× 1× 10× 1× 20514× 1× 407× 1× 538× 1× 2× 1× 1× 1× | /* * @license * Copyright Hôpitaux Universitaires de Genève. All Rights Reserved. * * Use of this source code is governed by an Apache-2.0 license that can be * found in the LICENSE file at https://github.com/DSI-HUG/dejajs-components/blob/master/LICENSE */ import { Position } from './position'; export enum RectOverlapDirection { horizontal, vertical, } export interface IRectOverlapInfos { area: number; width: number; height: number; direction: RectOverlapDirection; } export class Rect { public left: number; public top: number; public width: number; public height: number; public static equals(r1: Rect, r2: Rect) { return r1 && r2 && r1.left === r2.left && r1.top === r2.top && r1.width === r2.width && r1.height === r2.height; } public static union(r1: Rect, r2: Rect) { return Rect.fromLTRB(Math.min(r1.left, r2.left), Math.min(r1.top, r2.top), Math.max(r1.left + r1.width, r2.left + r2.width), Math.max(r1.top + r1.height, r2.top + r2.height)); } public static overlapInfos(rect1: Rect, rect2: Rect): IRectOverlapInfos { const x = Math.max(0, Math.min(rect1.right, rect2.right) - Math.max(rect1.left, rect2.left)); const y = Math.max(0, Math.min(rect1.bottom, rect2.bottom) - Math.max(rect1.top, rect2.top)); return { area: x * y, width: x, height: y, direction: x > y ? RectOverlapDirection.horizontal : RectOverlapDirection.vertical, }; } public static fromLTRB(left: number, top: number, right: number, bottom: number): Rect { return new Rect(left, top, right - left, bottom - top); } public static fromPoints(p1: Position, p2: Position) { return Rect.fromLTRB(Math.min(p1.left, p2.left), Math.min(p1.top, p2.top), Math.max(p1.left, p2.left), Math.max(p1.top, p2.top)); } constructor(left?: number | Object, top?: number, width?: number, height?: number) { if (typeof left === 'object') { const bounds = left as any || {}; this.left = bounds.left || 0; this.top = bounds.top || 0; this.width = bounds.right !== undefined ? bounds.right - this.left : bounds.width || 0; this.height = bounds.bottom !== undefined ? bounds.bottom - this.top : bounds.height || 0; } else { this.left = left || 0; this.top = top || 0; this.width = width || 0; this.height = height || 0; } } public set right(value: number) { this.width = value - this.left; } public get right() { return this.left + this.width; } public set bottom(value: number) { this.height = value - this.top; } public get bottom() { return this.top + this.height; } public get position() { return new Position(this.left, this.top); } public offset(x: number, y: number): Rect { return new Rect( this.left + x, this.top + y, this.width, this.height, ); } public adjacent(bounds: Rect) { return bounds.left <= this.right && bounds.right >= this.left && bounds.top <= this.bottom && bounds.bottom >= this.top; } public contains(bounds: Rect) { return bounds.left >= this.left && bounds.right <= this.right && bounds.top >= this.top && bounds.bottom <= this.bottom; } public containsPoint(point: Position) { return point.left >= this.left && point.left <= this.right && point.top >= this.top && point.top <= this.bottom; } public intersectWith(bounds: Rect) { return bounds.left < this.right && bounds.right > this.left && bounds.top < this.bottom && bounds.bottom > this.top; } public isEmpty() { return !this.width || !this.height; } public clone() { return new Rect(this.left, this.top, this.width, this.height); } public toClientRect() { return { left: this.left, top: this.top, bottom: this.bottom, right: this.right, width: this.width, height: this.height, } as ClientRect; } /** * @deprecated use toClientRect instead */ public toRectStruct() { return this.toClientRect(); } } |