-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrect.js
More file actions
48 lines (40 loc) · 865 Bytes
/
rect.js
File metadata and controls
48 lines (40 loc) · 865 Bytes
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
"use strict";
// 2D rectangle defined by 2 points: top-left and bottom-right.
function Rect(a, b, c, d)
{
if (arguments.length === 2) {
this.topLeft = a;
this.bottomRight = b;
} else {
this.topLeft = new V(a, b);
this.bottomRight = new V(c, d);
}
}
Rect.prototype =
{
add: function(p)
{
return new Rect(this.topLeft.add(p), this.bottomRight.add(p));
},
width: function()
{
return this.bottomRight.x - this.topLeft.x;
},
height: function()
{
return this.bottomRight.y - this.topLeft.y;
},
size: function()
{
return new V(this.bottomRight.y - this.topLeft.y, this.bottomRight.y - this.topLeft.y);
},
contains: function(p)
{
return this.topLeft.x <= p.x && p.x <= this.bottomRight.x
&& this.topLeft.y <= p.y && p.y <= this.bottomRight.y;
},
clone: function()
{
return new Rect(this.topLeft, this.bottomRight);
}
};