-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRectangle.js
53 lines (45 loc) · 1.26 KB
/
Rectangle.js
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
// wrapper for "class" Rectangle
import {Shape} from './Shape';
class Rectangle extends Shape {
constructor(left, top, width, height) {
super();
this.left = left || 0;
this.top = top || 0;
this.width = width || 0;
this.height = height || 0;
this.right = this.left + this.width;
this.bottom = this.top + this.height;
this.type = 'rectangle';
}
set(left, top, /*optional*/ width = null, /*optional*/ height = null) {
this.left = left;
this.top = top;
this.width = width || this.width;
this.height = height || this.height;
this.right = (this.left + this.width);
this.bottom = (this.top + this.height);
}
within(r) {
return (r.left <= this.left &&
r.right >= this.right &&
r.top <= this.top &&
r.bottom >= this.bottom);
}
overlaps(r) {
return (this.left < r.right &&
r.left < this.right &&
this.top < r.bottom &&
r.top < this.bottom);
}
// rectangle objects { x:, y:, width:, height: }
// return true if the 2 rectangles are colliding
// r1 and r2 are rectangles as defined above
static RectsColliding(r1, r2) {
return !(
r1.x > r2.x + r2.width ||
r1.x + r1.width < r2.x ||
r1.y > r2.y + r2.height ||
r1.y + r1.height < r2.y
);
}
}