-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrectangle.js
More file actions
213 lines (187 loc) · 5.54 KB
/
Copy pathrectangle.js
File metadata and controls
213 lines (187 loc) · 5.54 KB
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
class Rectangle{
/**
* Create a new rectangle
*
* @param {number} x - The x coordinate of this rectangle
* @param {number} y - The y coordinate of this rectangle
* @param {number} width - The width of this rectangle
* @param {number} height - The width of this rectangle
* @param {String} color = The color of this rectangle
**/
constructor(x, y, width, height, color){
this.x = Math.trunc(x);
this.y = Math.trunc(y);
this.width = Math.trunc(width);
this.height = Math.trunc(height);
this.color = color;
}
/**
* Get the first coordinate of this rectangle
* @return {number}
**/
getX1(){
return this.x
}
/**
* Get the second x coordinate of this rectangle
* @return {number}
**/
getX2(){
return this.x + this.width;
}
/**
* Get the first y coordinate of this rectangle
* @return {number}
**/
getY1(){
return this.y;
}
/**
* Get the second y coordinate of this rectangle
* @return {number}
**/
getY2(){
return this.y + this.height;
}
/**
* Get the color of this rectangle
* @return {String}
**/
getColor(){
return this.color;
}
/**
* Set the x coordinate of this rectangle
**/
setx(newx){
this.x = Math.trunc(newx);
}
/**
* Set the y coordinate of this rectangle
**/
sety(newy){
this.y = Math.trunc(newy);
}
/**
* Set the width of this rectangle
**/
setWidth(newWidth){
this.width = Math.trunc(newWidth);
}
/**
* Set the height of this rectangle
**/
setHeight(newHeight){
this.height = Math.trunc(newHeight);
}
/**
* Check if this rectangle is to the right of the given x coordinates of a rectangle.
*
* @param {number} firstX - The first x coordinte to check
* @param {number} secondX - The second x coordinate to check
*
* @return {boolean}
**/
isRightOf(firstX){
if( this.x > firstX){
return true;
}
return false;
}
/**
* Check if this rectangle is above the given y coordinates of a rectangle.
*
* @param {number} firstY - The first y coordinate to check
* @param {number} secondY - The second y coodinate to check
*
* @return {boolean}
**/
isAbove(firstY){
if(this.y < firstY){
return true;
}
return false;
}
overExtendsHorizontally(firstX, secondX){
if( this.x < firstX && this.getX2() > secondX){
return true;
}
return false;
}
overExtendsVertically(firstY, secondY){
if( this.y < firstY && this.getY2() > secondY){
return true;
}
return false;
}
/**
* Check if this rectangle is contained vertically by another rectangle.
*
* @param {number} firstY - The first y coordinate to check
* @param {number} secondY - The second y coordinate to check
*
* @return (boolean)
**/
isContainedVertically(firstY, secondY){
if( this.y >= firstY && this.getY2() <= secondY ){
return true;
}
return false;
}
/**
* Check if this rectangle is contained horizontally by another rectangle.
*
* @param {number} firstX - The first x coordinate to check
* @param {number} secondX - The second x coordinate to check
*
* @return (boolean)
**/
isContainedHorizontally(firstX, secondX){
if( this.x >= firstX && this.getX2() <= secondX ){
return true;
}
return false;
}
/**
* Check if this rectangle is encase by another rectangle.
*
* @param {number} firstX - The first x coordinate of the given rectangle
* @param {number} secondX - The second x coordinate of the given rectangle
* @param {number} firstY - The first y coordinate of the given rectangle
* @param {number} secondY - The second y coordinate of the given rectangle
*
* @return (boolean)
**/
isEncased(firstX, secondX, firstY, secondY){
if(this.isContainedHorizontally(firstX, secondX) && this.isContainedVertically(firstY, secondY)){
return true;
}
return false;
}
/**
* Draws the rectangle using the class variables. Updates the rectangles coordinate text on the
* page.
*
* @param {String} pName - The paragraph id associated with this rectangle
**/
display(pName){
rect(this.x, this.y, this.width, this.height);
document.getElementById(pName).innerHTML = this.color + " rectangle: X1 = " + this.x + " | Y1 = " + this.y + " | X2 = " + this.getX2() + " | Y2 = " + this.getY2() + " | width = " + this.width + " | height = " + this.height ;
}
/**
* Create a new width, height, x coordinate, and y coordinate all within the constraints of the
* canvas. Set the rectangles values to these values.
**/
updateRectangle(){
//Generate a new height and width within the set minimum and maximum rectnalge sizes.
var newWidth = random(50,250);
var newHeight = random(50,250);
//Create new points, ensure points are withing the canvas
var newX = random(CANVAS_WIDTH - newWidth);
var newY = random(CANVAS_HEIGHT - newHeight);
this.setWidth(newWidth);
this.setHeight(newHeight);
this.setx(newX);
this.sety(newY);
}
}