• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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  height: number = 0;
20  width: number = 0;
21
22  constructor(x: number, y: number, width: number, height: number) {
23    this.x = x;
24    this.y = y;
25    this.height = height;
26    this.width = width;
27  }
28
29  static contains(rectObj: Rect, x: number, y: number): boolean {
30    return rectObj.x <= x && x <= rectObj.x + rectObj.width && rectObj.y <= y && y <= rectObj.y + rectObj.height;
31  }
32
33  static containsWithPadding(
34    rectObj: Rect,
35    x: number,
36    y: number,
37    paddingLeftRight: number,
38    paddingTopBottom: number
39  ): boolean {
40    return (
41      rectObj.x + paddingLeftRight <= x &&
42      x <= rectObj.x + rectObj.width - paddingLeftRight &&
43      rectObj.y + paddingTopBottom <= y &&
44      y <= rectObj.y + rectObj.height - paddingTopBottom
45    );
46  }
47
48  static containsWithMargin(rectObj: Rect, x: number, y: number, t: number, r: number, b: number, l: number): boolean {
49    return (
50      rectObj.x - l <= x &&
51      x <= rectObj.x + rectObj.width + r &&
52      rectObj.y - t <= y &&
53      y <= rectObj.y + rectObj.height + b
54    );
55  }
56
57  static intersect(rectA: Rect | DOMRect, rectB: Rect | DOMRect): boolean {
58    let maxX = Math.max(rectA.x + rectA.width, rectB.x + rectB.width);
59    let maxY = Math.max(rectA.y + rectA.height, rectB.y + rectB.height);
60    let minX = Math.min(rectA.x, rectB.x);
61    let minY = Math.min(rectA.y, rectB.y);
62    return maxX - minX < rectB.width + rectA.width && maxY - minY <= rectA.height + rectB.height;
63  }
64
65  static getIntersect(rectA: DOMRect | Rect, rectB: DOMRect | Rect): Rect {
66    let maxX = Math.max(rectA.x, rectB.x);
67    let maxY = Math.max(rectA.y, rectB.y);
68    let width = Math.abs(Math.min(rectA.x + rectA.width, rectB.x + rectB.width) - Math.max(rectA.x, rectB.x));
69    let height = Math.abs(Math.min(rectA.y + rectA.height, rectB.y + rectB.height) - Math.max(rectA.y, rectB.y));
70    return new Rect(maxX, maxY, width, height);
71  }
72
73  contains(x: number, y: number): boolean {
74    return this.x <= x && x <= this.x + this.width && this.y <= y && y <= this.y + this.height;
75  }
76
77  containsWithPadding(x: number, y: number, paddingLeftOrRight: number, paddingTopOrBottom: number): boolean {
78    return (
79      this.x + paddingLeftOrRight <= x &&
80      x <= this.x + this.width - paddingLeftOrRight &&
81      this.y + paddingTopOrBottom <= y &&
82      y <= this.y + this.height - paddingTopOrBottom
83    );
84  }
85
86  containsWithMargin(x: number, y: number, t: number, r: number, b: number, l: number): boolean {
87    return this.x - l <= x && x <= this.x + this.width + r && this.y - t <= y && y <= this.y + this.height + b;
88  }
89
90  /**
91   * 判断是否相交
92   * @param rectObj
93   */
94  intersect(rectObj: Rect): boolean {
95    let maxX = this.x + this.width >= rectObj.x + rectObj.width ? this.x + this.width : rectObj.x + rectObj.width;
96    let maxY = this.y + this.height >= rectObj.y + rectObj.height ? this.y + this.height : rectObj.y + rectObj.height;
97    let minX = this.x <= rectObj.x ? this.x : rectObj.x;
98    let minY = this.y <= rectObj.y ? this.y : rectObj.y;
99    return maxX - minX <= rectObj.width + this.width && maxY - minY <= this.height + rectObj.height;
100  }
101}
102
103export class Point {
104  x: number = 0;
105  y: number = 0;
106
107  constructor(x: number, y: number) {
108    this.x = x;
109    this.y = y;
110  }
111}
112