/
rectangle.ts
233 lines (193 loc) 路 5.12 KB
/
rectangle.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
import { Point } from './point'
export class Rectangle {
x: number
y: number
width: number
height: number
constructor(x?: number, y?: number, width?: number, height?: number) {
this.x = x == null ? 0 : x
this.y = y == null ? 0 : y
this.width = width == null ? 0 : width
this.height = height == null ? 0 : height
}
getCenterX() {
return this.x + this.width / 2
}
getCenterY() {
return this.y + this.height / 2
}
getCenter() {
return new Point(this.getCenterX(), this.getCenterY())
}
getOrigin() {
return new Point(this.x, this.y)
}
getCorner() {
return new Point(this.x + this.width, this.y + this.height)
}
getTopRight() {
return new Point(this.x + this.width, this.y)
}
getBottomLeft() {
return new Point(this.x, this.y + this.height)
}
round() {
return new Rectangle(
Math.round(this.x),
Math.round(this.y),
Math.round(this.width),
Math.round(this.height),
)
}
update(rect: Rectangle | Rectangle.RectangleLike): void
update(x: number, y: number, width: number, height: number): void
update(
x: number | Rectangle | Rectangle.RectangleLike,
y?: number,
w?: number,
h?: number,
): void {
const b = typeof x === 'number' ? { x, y: y!, width: w!, height: h! } : x
this.x = b.x
this.y = b.y
this.width = b.width
this.height = b.height
}
grow(amount: number): this
grow(width: number, height: number): this
grow(width: number, height?: number) {
const w = width
const h = height != null ? height : width
this.x -= w
this.y -= h
this.width += 2 * w
this.height += 2 * h
return this
}
add(rect: Rectangle | Rectangle.RectangleLike) {
if (rect != null) {
const minX = Math.min(this.x, rect.x)
const minY = Math.min(this.y, rect.y)
const maxX = Math.max(this.x + this.width, rect.x + rect.width)
const maxY = Math.max(this.y + this.height, rect.y + rect.height)
this.x = minX
this.y = minY
this.width = maxX - minX
this.height = maxY - minY
}
return this
}
intersect(rect: Rectangle | Rectangle.RectangleLike) {
if (rect != null) {
const r1 = this.x + this.width
const r2 = rect.x + rect.width
const b1 = this.y + this.height
const b2 = rect.y + rect.height
this.x = Math.max(this.x, rect.x)
this.y = Math.max(this.y, rect.y)
this.width = Math.min(r1, r2) - this.x
this.height = Math.min(b1, b2) - this.y
}
return this
}
rotate90() {
const t = (this.width - this.height) / 2
this.x += t
this.y -= t
const tmp = this.width
this.width = this.height
this.height = tmp
return this
}
containsPoint(x: number, y: number): boolean
containsPoint(point: Point | Point.PointLike): boolean
containsPoint(x: number | Point | Point.PointLike, y?: number): boolean {
const point = typeof x === 'number' ? { x, y: y! } : x
return (
point != null &&
point.x >= this.x &&
point.x <= this.x + this.width &&
point.y >= this.y &&
point.y <= this.y + this.height
)
}
containsRect(x: number, y: number, w: number, h: number): boolean
containsRect(rect: Rectangle | Rectangle.RectangleLike): boolean
containsRect(
x: number | Rectangle | Rectangle.RectangleLike,
y?: number,
w?: number,
h?: number,
) {
const b = typeof x === 'number' ? { x, y: y!, width: w!, height: h! } : x
const x1 = this.x
const y1 = this.y
const w1 = this.width
const h1 = this.height
const x2 = b.x
const y2 = b.y
const w2 = b.width
const h2 = b.height
return x2 >= x1 && y2 >= y1 && x2 + w2 <= x1 + w1 && y2 + h2 <= y1 + h1
}
isIntersectWith(x: number, y: number, w: number, h: number): boolean
isIntersectWith(rect: Rectangle | Rectangle.RectangleLike): boolean
isIntersectWith(
x: number | Rectangle | Rectangle.RectangleLike,
y?: number,
w?: number,
h?: number,
) {
const b = typeof x === 'number' ? { x, y: y!, width: w!, height: h! } : x
let w1 = this.width
let h1 = this.height
let w2 = b.width
let h2 = b.height
if (w2 <= 0 || h2 <= 0 || w1 <= 0 || h1 <= 0) {
return false
}
const x1 = this.x
const y1 = this.y
const x2 = b.x
const y2 = b.y
w2 += x2
h2 += y2
w1 += x1
h1 += y1
return (
(w2 < x2 || w2 > x1) &&
(h2 < y2 || h2 > y1) &&
(w1 < x1 || w1 > x2) &&
(h1 < y1 || h1 > y2)
)
}
equals(rect: Rectangle | Rectangle.RectangleLike) {
return (
rect != null &&
rect.x === this.x &&
rect.y === this.y &&
rect.width === this.width &&
rect.height === this.height
)
}
clone() {
return Rectangle.clone(this)
}
valueOf() {
return [this.x, this.y, this.width, this.height]
}
toString() {
return this.valueOf().join(', ')
}
}
export namespace Rectangle {
export interface RectangleLike {
x: number
y: number
width: number
height: number
}
export function clone(rect: Rectangle | RectangleLike) {
return new Rectangle(rect.x, rect.y, rect.width, rect.height)
}
}