1 | import { Range } from "./Range";
|
2 | export class Rectangle extends Range {
|
3 | constructor(x, y, width, height) {
|
4 | super(x, y);
|
5 | this.size = {
|
6 | height: height,
|
7 | width: width,
|
8 | };
|
9 | }
|
10 | contains(point) {
|
11 | const w = this.size.width;
|
12 | const h = this.size.height;
|
13 | const pos = this.position;
|
14 | return point.x >= pos.x && point.x <= pos.x + w && point.y >= pos.y && point.y <= pos.y + h;
|
15 | }
|
16 | intersects(range) {
|
17 | const rect = range;
|
18 | const circle = range;
|
19 | const w = this.size.width;
|
20 | const h = this.size.height;
|
21 | const pos1 = this.position;
|
22 | const pos2 = range.position;
|
23 | if (circle.radius !== undefined) {
|
24 | return circle.intersects(this);
|
25 | }
|
26 | else if (rect.size !== undefined) {
|
27 | const size2 = rect.size;
|
28 | const w2 = size2.width;
|
29 | const h2 = size2.height;
|
30 | return pos2.x < pos1.x + w && pos2.x + w2 > pos1.x && pos2.y < pos1.y + h && pos2.y + h2 > pos1.y;
|
31 | }
|
32 | return false;
|
33 | }
|
34 | }
|