1*90c8c64dSAndroid Build Coastguard Worker/* 2*90c8c64dSAndroid Build Coastguard Worker * Copyright (C) 2024 The Android Open Source Project 3*90c8c64dSAndroid Build Coastguard Worker * 4*90c8c64dSAndroid Build Coastguard Worker * Licensed under the Apache License, Version 2.0 (the "License"); 5*90c8c64dSAndroid Build Coastguard Worker * you may not use this file except in compliance with the License. 6*90c8c64dSAndroid Build Coastguard Worker * You may obtain a copy of the License at 7*90c8c64dSAndroid Build Coastguard Worker * 8*90c8c64dSAndroid Build Coastguard Worker * http://www.apache.org/licenses/LICENSE-2.0 9*90c8c64dSAndroid Build Coastguard Worker * 10*90c8c64dSAndroid Build Coastguard Worker * Unless required by applicable law or agreed to in writing, software 11*90c8c64dSAndroid Build Coastguard Worker * distributed under the License is distributed on an "AS IS" BASIS, 12*90c8c64dSAndroid Build Coastguard Worker * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13*90c8c64dSAndroid Build Coastguard Worker * See the License for the specific language governing permissions and 14*90c8c64dSAndroid Build Coastguard Worker * limitations under the License. 15*90c8c64dSAndroid Build Coastguard Worker */ 16*90c8c64dSAndroid Build Coastguard Worker 17*90c8c64dSAndroid Build Coastguard Workerimport {Point} from 'common/geometry/point'; 18*90c8c64dSAndroid Build Coastguard Worker 19*90c8c64dSAndroid Build Coastguard Workerexport class Rect { 20*90c8c64dSAndroid Build Coastguard Worker constructor( 21*90c8c64dSAndroid Build Coastguard Worker readonly x: number, 22*90c8c64dSAndroid Build Coastguard Worker readonly y: number, 23*90c8c64dSAndroid Build Coastguard Worker readonly w: number, 24*90c8c64dSAndroid Build Coastguard Worker readonly h: number, 25*90c8c64dSAndroid Build Coastguard Worker ) {} 26*90c8c64dSAndroid Build Coastguard Worker 27*90c8c64dSAndroid Build Coastguard Worker isAlmostEqual(other: Rect, eps: number): boolean { 28*90c8c64dSAndroid Build Coastguard Worker const isClose = (a: number, b: number) => Math.abs(a - b) <= eps; 29*90c8c64dSAndroid Build Coastguard Worker return ( 30*90c8c64dSAndroid Build Coastguard Worker isClose(this.x, other.x) && 31*90c8c64dSAndroid Build Coastguard Worker isClose(this.y, other.y) && 32*90c8c64dSAndroid Build Coastguard Worker isClose(this.w, other.w) && 33*90c8c64dSAndroid Build Coastguard Worker isClose(this.h, other.h) 34*90c8c64dSAndroid Build Coastguard Worker ); 35*90c8c64dSAndroid Build Coastguard Worker } 36*90c8c64dSAndroid Build Coastguard Worker 37*90c8c64dSAndroid Build Coastguard Worker containsPoint(point: Point): boolean { 38*90c8c64dSAndroid Build Coastguard Worker return ( 39*90c8c64dSAndroid Build Coastguard Worker this.x <= point.x && 40*90c8c64dSAndroid Build Coastguard Worker point.x <= this.x + this.w && 41*90c8c64dSAndroid Build Coastguard Worker this.y <= point.y && 42*90c8c64dSAndroid Build Coastguard Worker point.y <= this.y + this.h 43*90c8c64dSAndroid Build Coastguard Worker ); 44*90c8c64dSAndroid Build Coastguard Worker } 45*90c8c64dSAndroid Build Coastguard Worker 46*90c8c64dSAndroid Build Coastguard Worker cropRect(other: Rect): Rect { 47*90c8c64dSAndroid Build Coastguard Worker const maxLeft = Math.max(this.x, other.x); 48*90c8c64dSAndroid Build Coastguard Worker const minRight = Math.min(this.x + this.w, other.x + other.w); 49*90c8c64dSAndroid Build Coastguard Worker const maxTop = Math.max(this.y, other.y); 50*90c8c64dSAndroid Build Coastguard Worker const minBottom = Math.min(this.y + this.h, other.y + other.h); 51*90c8c64dSAndroid Build Coastguard Worker return new Rect(maxLeft, maxTop, minRight - maxLeft, minBottom - maxTop); 52*90c8c64dSAndroid Build Coastguard Worker } 53*90c8c64dSAndroid Build Coastguard Worker 54*90c8c64dSAndroid Build Coastguard Worker containsRect(other: Rect): boolean { 55*90c8c64dSAndroid Build Coastguard Worker return ( 56*90c8c64dSAndroid Build Coastguard Worker this.w > 0 && 57*90c8c64dSAndroid Build Coastguard Worker this.h > 0 && 58*90c8c64dSAndroid Build Coastguard Worker this.x <= other.x && 59*90c8c64dSAndroid Build Coastguard Worker this.y <= other.y && 60*90c8c64dSAndroid Build Coastguard Worker this.x + this.w >= other.x + other.w && 61*90c8c64dSAndroid Build Coastguard Worker this.y + this.h >= other.y + other.h 62*90c8c64dSAndroid Build Coastguard Worker ); 63*90c8c64dSAndroid Build Coastguard Worker } 64*90c8c64dSAndroid Build Coastguard Worker 65*90c8c64dSAndroid Build Coastguard Worker intersectsRect(other: Rect): boolean { 66*90c8c64dSAndroid Build Coastguard Worker if ( 67*90c8c64dSAndroid Build Coastguard Worker this.x < other.x + other.w && 68*90c8c64dSAndroid Build Coastguard Worker other.x < this.x + this.w && 69*90c8c64dSAndroid Build Coastguard Worker this.y <= other.y + other.h && 70*90c8c64dSAndroid Build Coastguard Worker other.y <= this.y + this.h 71*90c8c64dSAndroid Build Coastguard Worker ) { 72*90c8c64dSAndroid Build Coastguard Worker let [x, y, w, h] = [this.x, this.y, this.w, this.h]; 73*90c8c64dSAndroid Build Coastguard Worker 74*90c8c64dSAndroid Build Coastguard Worker if (this.x < other.x) { 75*90c8c64dSAndroid Build Coastguard Worker x = other.x; 76*90c8c64dSAndroid Build Coastguard Worker } 77*90c8c64dSAndroid Build Coastguard Worker if (this.y < other.y) { 78*90c8c64dSAndroid Build Coastguard Worker y = other.y; 79*90c8c64dSAndroid Build Coastguard Worker } 80*90c8c64dSAndroid Build Coastguard Worker if (this.x + this.w > other.x + other.w) { 81*90c8c64dSAndroid Build Coastguard Worker w = other.w; 82*90c8c64dSAndroid Build Coastguard Worker } 83*90c8c64dSAndroid Build Coastguard Worker if (this.y + this.h > other.y + other.h) { 84*90c8c64dSAndroid Build Coastguard Worker h = other.h; 85*90c8c64dSAndroid Build Coastguard Worker } 86*90c8c64dSAndroid Build Coastguard Worker 87*90c8c64dSAndroid Build Coastguard Worker return !new Rect(x, y, w, h).isEmpty(); 88*90c8c64dSAndroid Build Coastguard Worker } 89*90c8c64dSAndroid Build Coastguard Worker 90*90c8c64dSAndroid Build Coastguard Worker return false; 91*90c8c64dSAndroid Build Coastguard Worker } 92*90c8c64dSAndroid Build Coastguard Worker 93*90c8c64dSAndroid Build Coastguard Worker isEmpty(): boolean { 94*90c8c64dSAndroid Build Coastguard Worker const [x, y, w, h] = [this.x, this.y, this.w, this.h]; 95*90c8c64dSAndroid Build Coastguard Worker const nullValuePresent = 96*90c8c64dSAndroid Build Coastguard Worker x === -1 || y === -1 || x + w === -1 || y + h === -1; 97*90c8c64dSAndroid Build Coastguard Worker const nullHeightOrWidth = w <= 0 || h <= 0; 98*90c8c64dSAndroid Build Coastguard Worker return nullValuePresent || nullHeightOrWidth; 99*90c8c64dSAndroid Build Coastguard Worker } 100*90c8c64dSAndroid Build Coastguard Worker} 101