forked from paperjs/paper.js
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRectangle.js
More file actions
272 lines (233 loc) · 7.89 KB
/
Copy pathRectangle.js
File metadata and controls
272 lines (233 loc) · 7.89 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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
/*
* Paper.js - The Swiss Army Knife of Vector Graphics Scripting.
* http://paperjs.org/
*
* Copyright (c) 2011 - 2013, Juerg Lehni & Jonathan Puckey
* http://lehni.org/ & http://jonathanpuckey.com/
*
* Distributed under the MIT license. See LICENSE file for details.
*
* All rights reserved.
*/
module('Rectangle');
test('new Rectangle(new Point(10, 20), new Size(30, 40));', function() {
var rect = new Rectangle(new Point(10, 20), new Size(30, 40));
equals(rect.toString(), '{ x: 10, y: 20, width: 30, height: 40 }');
});
test('new Rectangle({ point: [10, 20], size: [30, 40] });', function() {
var rect = new Rectangle({ point: [10, 20], size: [30, 40] });
equals(rect.toString(), '{ x: 10, y: 20, width: 30, height: 40 }');
});
test('new Rectangle({ point: new Point(10, 20), size: new Size(30, 40)});', function() {
var rect = new Rectangle({ point: new Point(10, 20), size: new Size(30, 40)});
equals(rect.toString(), '{ x: 10, y: 20, width: 30, height: 40 }');
});
test('new Rectangle([10, 20], [30, 40]);', function() {
var rect = new Rectangle([10, 20], [30, 40]);
equals(rect.toString(), '{ x: 10, y: 20, width: 30, height: 40 }');
});
test('new Rectangle({from: [10, 20], to: [30, 40]});', function() {
var rect = new Rectangle({from: [10, 20], to: [30, 40]});
equals(rect.toString(), '{ x: 10, y: 20, width: 20, height: 20 }');
});
test('new Rectangle(new Point(10, 20), new Point(30, 40));', function() {
var rect = new Rectangle(new Point(10, 20), new Point(30, 40));
equals(rect.toString(), '{ x: 10, y: 20, width: 20, height: 20 }');
});
test('new Rectangle(10, 20, 30, 40);', function() {
var rect = new Rectangle(10, 20, 30, 40);
equals(rect.toString(), '{ x: 10, y: 20, width: 30, height: 40 }');
});
test('new Rectangle({x: 10, y: 20, width: 30, height: 40});', function() {
var rect = new Rectangle({x: 10, y: 20, width: 30, height: 40});
equals(rect.toString(), '{ x: 10, y: 20, width: 30, height: 40 }');
});
test('get size', function() {
var rect = new Rectangle(10, 10, 20, 30);
equals(function() {
return rect.size.equals([20, 30]);
}, true);
});
test('set size', function() {
var rect = new Rectangle(10, 10, 20, 20);
rect.size = new Size(30, 30);
equals(rect.toString(), '{ x: 10, y: 10, width: 30, height: 30 }');
});
test('topLeft', function() {
var rect = new Rectangle(10, 10, 20, 20);
var point = rect.topLeft;
equals(point.toString(), '{ x: 10, y: 10 }');
});
test('set topLeft', function() {
var rect = new Rectangle(10, 10, 20, 20);
rect.topLeft = [10, 15];
var point = rect.topLeft;
equals(point.toString(), '{ x: 10, y: 15 }');
});
test('get topRight', function() {
var rect = new Rectangle(10, 10, 20, 20);
var point = rect.topRight;
equals(point.toString(), '{ x: 30, y: 10 }');
});
test('set topRight', function() {
var rect = new Rectangle(10, 10, 20, 20);
rect.topRight = [10, 15];
var point = rect.topRight;
equals(point.toString(), '{ x: 10, y: 15 }');
});
test('get bottomLeft', function() {
var rect = new Rectangle(10, 10, 20, 20);
var point = rect.bottomLeft;
equals(point.toString(), '{ x: 10, y: 30 }');
});
test('set bottomLeft', function() {
var rect = new Rectangle(10, 10, 20, 20);
rect.bottomLeft = [10, 15];
var point = rect.bottomLeft;
equals(point.toString(), '{ x: 10, y: 15 }');
});
test('get bottomRight', function() {
var rect = new Rectangle(10, 10, 20, 20);
var point = rect.bottomRight;
equals(point.toString(), '{ x: 30, y: 30 }');
});
test('set bottomRight', function() {
var rect = new Rectangle(10, 10, 20, 20);
rect.bottomRight = [10, 15];
var point = rect.bottomRight;
equals(point.toString(), '{ x: 10, y: 15 }');
});
test('get bottomCenter', function() {
var rect = new Rectangle(10, 10, 20, 20);
var point = rect.bottomCenter;
equals(point.toString(), '{ x: 20, y: 30 }');
});
test('set bottomCenter', function() {
var rect = new Rectangle(10, 10, 20, 20);
rect.bottomCenter = [10, 15];
var point = rect.bottomCenter;
equals(point.toString(), '{ x: 10, y: 15 }');
});
test('get topCenter', function() {
var rect = new Rectangle(10, 10, 20, 20);
var point = rect.topCenter;
equals(point.toString(), '{ x: 20, y: 10 }');
});
test('set topCenter', function() {
var rect = new Rectangle(10, 10, 20, 20);
rect.topCenter = [10, 15];
var point = rect.topCenter;
equals(point.toString(), '{ x: 10, y: 15 }');
});
test('get leftCenter', function() {
var rect = new Rectangle(10, 10, 20, 20);
var point = rect.leftCenter;
equals(point.toString(), '{ x: 10, y: 20 }');
});
test('set leftCenter', function() {
var rect = new Rectangle(10, 10, 20, 20);
rect.leftCenter = [10, 15];
var point = rect.leftCenter;
equals(point.toString(), '{ x: 10, y: 15 }');
});
test('get rightCenter', function() {
var rect = new Rectangle(10, 10, 20, 20);
var point = rect.rightCenter;
equals(point.toString(), '{ x: 30, y: 20 }');
});
test('set rightCenter', function() {
var rect = new Rectangle(10, 10, 20, 20);
rect.rightCenter = [10, 15];
var point = rect.rightCenter;
equals(point.toString(), '{ x: 10, y: 15 }');
});
test('intersects(rect)', function() {
var rect1 = new Rectangle({ x: 160, y: 270, width: 20, height: 20 });
var rect2 = { x: 195, y: 301, width: 19, height: 19 };
equals(function() {
return rect1.intersects(rect2);
}, false);
rect1 = new Rectangle({ x: 160, y: 270, width: 20, height: 20 });
rect2 = { x: 170.5, y: 280.5, width: 19, height: 19 };
equals(function() {
return rect1.intersects(rect2);
}, true);
});
test('contains(rect)', function() {
var rect1 = new Rectangle({ x: 160, y: 270, width: 20, height: 20 });
var rect2 = { x: 195, y: 301, width: 19, height: 19 };
equals(function() {
return rect1.contains(rect2);
}, false);
rect1 = new Rectangle({ x: 160, y: 270, width: 20, height: 20 });
rect2 = new Rectangle({ x: 170.5, y: 280.5, width: 19, height: 19 });
equals(function() {
return rect1.contains(rect2);
}, false);
rect1 = new Rectangle({ x: 299, y: 161, width: 137, height: 129 });
rect2 = new Rectangle({ x: 340, y: 197, width: 61, height: 61 });
equals(function() {
return rect1.contains(rect2);
}, true);
equals(function() {
return rect2.contains(rect1);
}, false);
});
test('contains(point)', function() {
var rect = new Rectangle({ x: 160, y: 270, width: 20, height: 20 });
var point = new Point(166, 280);
equals(function() {
return rect.contains(point);
}, true);
var point = new Point(30, 30);
equals(function() {
return rect.contains(point);
}, false);
});
test('intersect(rect)', function() {
var rect1 = new Rectangle({ x: 160, y: 270, width: 20, height: 20 });
var rect2 = { x: 170.5, y: 280.5, width: 19, height: 19 };
var intersected = rect1.intersect(rect2);
equals(function() {
return intersected.equals({ x: 170.5, y: 280.5, width: 9.5, height: 9.5 });
}, true);
});
test('unite(rect)', function() {
var rect1 = new Rectangle({ x: 160, y: 270, width: 20, height: 20 });
var rect2 = { x: 170.5, y: 280.5, width: 19, height: 19 };
var united = rect1.unite(rect2);
equals(function() {
return united.equals({ x: 160, y: 270, width: 29.5, height: 29.5 });
}, true);
});
test('include(point)', function() {
var rect1 = new Rectangle({ x: 95, y: 151, width: 20, height: 20 });
var included = rect1.include([50, 50]);
equals(function() {
return included.equals({ x: 50, y: 50, width: 65, height: 121 });
}, true);
});
test('toString()', function() {
var string = new Rectangle(10, 20, 30, 40).toString();
equals(string, '{ x: 10, y: 20, width: 30, height: 40 }');
});
test('new Rectangle(object)', function() {
equals(function() {
return new Rectangle({
center: [50, 100],
size: [100, 200]
}).toString();
}, '{ x: 0, y: 0, width: 100, height: 200 }');
equals(function() {
return new Rectangle({
topLeft: [100, 50],
size: [100, 200]
}).toString();
}, '{ x: 100, y: 50, width: 100, height: 200 }');
equals(function() {
return new Rectangle({
topRight: [200, 50],
size: [100, 200]
}).toString();
}, '{ x: 100, y: -150, width: 100, height: 200 }');
});