• 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    // ------------------------修改rangeruler部分鼠标形态根据高度调整-------------------------
79    if (sessionStorage.getItem('expand') === 'true') {
80      //展开
81      this.height = 75;
82    } else if (sessionStorage.getItem('expand') === 'false') {
83      //折叠
84      this.height = 75 - Number(sessionStorage.getItem('foldHeight'));
85    }
86    return (
87      this.x + paddingLeftOrRight <= x &&
88      x <= this.x + this.width - paddingLeftOrRight &&
89      this.y + paddingTopOrBottom <= y &&
90      y <= this.y + this.height - paddingTopOrBottom
91    );
92  }
93
94  containsWithMargin(x: number, y: number, t: number, r: number, b: number, l: number): boolean {
95    return this.x - l <= x && x <= this.x + this.width + r && this.y - t <= y && y <= this.y + this.height + b;
96  }
97
98  /**
99   * 判断是否相交
100   * @param rectObj
101   */
102  intersect(rectObj: Rect): boolean {
103    let maxX = this.x + this.width >= rectObj.x + rectObj.width ? this.x + this.width : rectObj.x + rectObj.width;
104    let maxY = this.y + this.height >= rectObj.y + rectObj.height ? this.y + this.height : rectObj.y + rectObj.height;
105    let minX = this.x <= rectObj.x ? this.x : rectObj.x;
106    let minY = this.y <= rectObj.y ? this.y : rectObj.y;
107    return maxX - minX <= rectObj.width + this.width && maxY - minY <= this.height + rectObj.height;
108  }
109}
110
111export class Point {
112  x: number = 0;
113  y: number = 0;
114
115  constructor(x: number, y: number) {
116    this.x = x;
117    this.y = y;
118  }
119}
120