-
Notifications
You must be signed in to change notification settings - Fork 0
/
rectangle.js
91 lines (65 loc) · 2.24 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
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
var Rectangle = (function () {
function Rectangle (left, bottom, width, height) {
this.left = left;
this.bottom = bottom;
this.width = width;
this.height = height;
}
Rectangle.prototype.getRight = function () {
return this.left + this.width;
};
Rectangle.prototype.getTop = function () {
return this.bottom + this.height;
};
Rectangle.prototype.setRight = function (right) {
this.width = right - this.left;
return this;
};
Rectangle.prototype.setTop = function (top) {
this.height = top - this.bottom;
return this;
};
Rectangle.prototype.clone = function () {
return new Rectangle(this.left, this.bottom, this.width, this.height);
};
Rectangle.prototype.includeRectangle = function (rectangle) {
var newRight = Math.max(this.getRight(), rectangle.getRight());
var newTop = Math.max(this.getTop(), rectangle.getTop());
this.left = Math.min(this.left, rectangle.left);
this.bottom = Math.min(this.bottom, rectangle.bottom);
this.setRight(newRight);
this.setTop(newTop);
return this;
};
Rectangle.prototype.intersectRectangle = function (rectangle) {
var newRight = Math.min(this.getRight(), rectangle.getRight());
var newTop = Math.min(this.getTop(), rectangle.getTop());
this.left = Math.max(this.left, rectangle.left);
this.bottom = Math.max(this.bottom, rectangle.bottom);
this.setRight(newRight);
this.setTop(newTop);
return this;
};
Rectangle.prototype.translate = function (x, y) {
this.left += x;
this.bottom += y;
return this;
};
Rectangle.prototype.scale = function (x, y) {
this.left *= x;
this.bottom *= y;
this.width *= x;
this.height *= y;
return this;
};
Rectangle.prototype.round = function () {
this.left = Math.round(this.left);
this.bottom = Math.round(this.bottom);
this.width = Math.round(this.width);
this.height = Math.round(this.height);
};
Rectangle.prototype.getArea = function () {
return this.width * this.height;
};
return Rectangle;
}());