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