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 RectF { 17 left: number; 18 top: number; 19 right: number; 20 bottom: number; 21 22 constructor() { 23 this.left = 0; 24 this.top = 0; 25 this.right = 0; 26 this.bottom = 0; 27 } 28 29 set(left: number, top: number, right: number, bottom: number) { 30 this.left = left; 31 this.top = top; 32 this.right = right; 33 this.bottom = bottom; 34 } 35 36 getWidth(): number { 37 return (this.right - this.left); 38 } 39 40 getHeight(): number { 41 return (this.bottom - this.top); 42 } 43 44 getDiagonal(): number { 45 return Math.hypot(this.getWidth(), this.getHeight()); 46 } 47 48 getCenterX(): number { 49 return (this.left + this.getWidth() / 2); 50 } 51 52 getCenterY(): number { 53 return (this.top + this.getHeight() / 2); 54 } 55 56 isInRect(x: number, y: number): boolean { 57 return (x >= this.left && x <= this.right && y >= this.top && y <= this.bottom); 58 } 59 60 scale(scale: number) { 61 this.left *= scale; 62 this.right *= scale; 63 this.top *= scale; 64 this.bottom *= scale; 65 } 66 67 move(offsetX: number, offsetY: number) { 68 this.left += offsetX; 69 this.right += offsetX; 70 this.top += offsetY; 71 this.bottom += offsetY; 72 } 73}