1/* 2 * Copyright (C) 2022 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16export class Rect { 17 x: number = 0 18 y: number = 0 19 width: number = 0 20 height: number = 0 21 22 constructor(x: number, y: number, width: number, height: number) { 23 this.x = x; 24 this.y = y; 25 this.width = width; 26 this.height = height; 27 } 28 29 static contains(rect: Rect, x: number, y: number): boolean { 30 return rect.x <= x && x <= rect.x + rect.width && rect.y <= y && y <= rect.y + rect.height; 31 } 32 33 static containsWithPadding(rect: Rect, x: number, y: number, paddingLeftRight: number, paddingTopBottom: number): boolean { 34 return rect.x + paddingLeftRight <= x 35 && x <= rect.x + rect.width - paddingLeftRight 36 && rect.y + paddingTopBottom <= y 37 && y <= rect.y + rect.height - paddingTopBottom; 38 } 39 40 static containsWithMargin(rect: Rect, x: number, y: number, t: number, r: number, b: number, l: number): boolean { 41 return rect.x - l <= x 42 && x <= rect.x + rect.width + r 43 && rect.y - t <= y 44 && y <= rect.y + rect.height + b; 45 } 46 47 static intersect(r1: Rect, rect: Rect): boolean { 48 let maxX = r1.x + r1.width > rect.x + rect.width ? r1.x + r1.width : rect.x + rect.width; 49 let maxY = r1.y + r1.height > rect.y + rect.height ? r1.y + r1.height : rect.y + rect.height; 50 let minX = r1.x < rect.x ? r1.x : rect.x; 51 let minY = r1.y < rect.y ? r1.y : rect.y; 52 if (maxX - minX < rect.width + r1.width && maxY - minY < r1.height + rect.height) { 53 return true; 54 } else { 55 return false; 56 } 57 } 58 59 contains(x: number, y: number): boolean { 60 return this.x <= x && x <= this.x + this.width && this.y <= y && y <= this.y + this.height; 61 } 62 63 containsWithPadding(x: number, y: number, paddingLeftRight: number, paddingTopBottom: number): boolean { 64 return this.x + paddingLeftRight <= x 65 && x <= this.x + this.width - paddingLeftRight 66 && this.y + paddingTopBottom <= y 67 && y <= this.y + this.height - paddingTopBottom; 68 } 69 70 containsWithMargin(x: number, y: number, t: number, r: number, b: number, l: number): boolean { 71 return this.x - l <= x 72 && x <= this.x + this.width + r 73 && this.y - t <= y 74 && y <= this.y + this.height + b; 75 } 76 77 /** 78 * 判断是否相交 79 * @param rect 80 */ 81 intersect(rect: Rect): boolean { 82 let maxX = this.x + this.width >= rect.x + rect.width ? this.x + this.width : rect.x + rect.width; 83 let maxY = this.y + this.height >= rect.y + rect.height ? this.y + this.height : rect.y + rect.height; 84 let minX = this.x <= rect.x ? this.x : rect.x; 85 let minY = this.y <= rect.y ? this.y : rect.y; 86 if (maxX - minX <= rect.width + this.width && maxY - minY <= this.height + rect.height) { 87 return true; 88 } else { 89 return false; 90 } 91 } 92} 93 94export class Point { 95 x: number = 0 96 y: number = 0 97 98 constructor(x: number, y: number) { 99 this.x = x; 100 this.y = y; 101 } 102} 103