forked from paperjs/paper.js
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathItem_Contains.js
More file actions
191 lines (164 loc) · 6.67 KB
/
Copy pathItem_Contains.js
File metadata and controls
191 lines (164 loc) · 6.67 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
/*
* 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('Item Contains');
function testPoint(item, point, inside) {
equals(item.contains(point), inside, 'The point ' + point
+ ' should be ' + (inside ? 'inside' : 'outside') + '.');
}
test('Path#contains() (Regular Polygon)', function() {
var path = new Path.RegularPolygon([0, 0], 6, 20);
testPoint(path, new Point(0, -20), true);
testPoint(path, new Point(0, -10), true);
testPoint(path, new Point(0, 0), true);
testPoint(path, new Point(0, 10), true);
testPoint(path, new Point(0, 20), true);
testPoint(path, new Point(-10, -20), false);
testPoint(path, new Point(-10, -10), true);
testPoint(path, new Point(-10, 0), true);
testPoint(path, new Point(-10, 10), true);
testPoint(path, new Point(-10, 20), false);
testPoint(path, new Point(10, -20), false);
testPoint(path, new Point(10, -10), true);
testPoint(path, new Point(10, 0), true);
testPoint(path, new Point(10, 10), true);
testPoint(path, new Point(10, 20), false);
});
test('Path#contains() (Circle Contours)', function() {
var path = new Path.Circle({
center: [100, 100],
radius: 50,
fillColor: 'blue',
});
testPoint(path, path.bounds.topCenter, true);
testPoint(path, path.bounds.leftCenter, true);
testPoint(path, path.bounds.rightCenter, true);
testPoint(path, path.bounds.bottomCenter, true);
testPoint(path, path.bounds.topLeft, false);
testPoint(path, path.bounds.topRight, false);
testPoint(path, path.bounds.bottomLeft, false);
testPoint(path, path.bounds.bottomRight, false);
});
test('Path#contains() (Transformed Circle Contours)', function() {
var path = new Path.Circle({
center: [200, 200],
radius: 50,
fillColor: 'blue',
});
path.translate(100, 100);
testPoint(path, path.bounds.topCenter, true);
testPoint(path, path.bounds.leftCenter, true);
testPoint(path, path.bounds.rightCenter, true);
testPoint(path, path.bounds.bottomCenter, true);
testPoint(path, path.bounds.topLeft, false);
testPoint(path, path.bounds.topRight, false);
testPoint(path, path.bounds.bottomLeft, false);
testPoint(path, path.bounds.bottomRight, false);
});
test('Path#contains() (Round Rectangle)', function() {
var rectangle = new Rectangle({
point: new Point(0, 0),
size: new Size(200, 40)
});
var path = new Path.Rectangle(rectangle, new Size(20, 20));
testPoint(path, new Point(100, 20), true);
});
test('Path#contains() (Open Circle)', function() {
var path = new Path.Circle([100, 100], 100);
path.closed = false;
path.fillColor = '#ff0000';
testPoint(path, new Point(40, 160), false);
});
test('CompoundPath#contains() (Donut)', function() {
var path = new CompoundPath([
new Path.Circle([0, 0], 50),
new Path.Circle([0, 0], 25)
]);
equals(path.contains(new Point(0, 0)), false,
'The center point should be outside the donut.');
equals(path.contains(new Point(-35, 0)), true,
'A vertically centered point on the left side should be inside the donut.');
equals(path.contains(new Point(35, 0)), true,
'A vertically centered point on the right side should be inside the donut.');
equals(path.contains(new Point(0, 49)), true,
'The near bottom center point of the outer circle should be inside the donut.');
equals(path.contains(new Point(0, 50)), true,
'The bottom center point of the outer circle should be inside the donut.');
equals(path.contains(new Point(0, 51)), false,
'The near bottom center point of the outer circle should be outside the donut.');
equals(path.contains(new Point({ length: 50, angle: 30 })), true,
'A random point on the periphery of the outer circle should be inside the donut.');
equals(path.contains(new Point(0, 25)), false,
'The bottom center point of the inner circle should be outside the donut.');
equals(path.contains(new Point({ length: 25, angle: 30 })), false,
'A random point on the periphery of the inner circle should be outside the donut.');
equals(path.contains(new Point(-50, -50)), false,
'The top left point of bounding box should be outside the donut.');
equals(path.contains(new Point(-50, 50)), false,
'The bottom left point of bounding box should be outside the donut.');
equals(path.contains(new Point(-45, 45)), false,
'The near bottom left point of bounding box should be outside the donut.');
});
test('Shape#contains()', function() {
var shape = new Shape.Circle([0, 0], 100);
testPoint(shape, new Point(0, 0), true);
testPoint(shape, new Point(0, -100), true);
testPoint(shape, new Point({ length: 99, angle: 45 }), true);
testPoint(shape, new Point({ length: 100, angle: 45 }), true);
testPoint(shape, new Point({ length: 101, angle: 45 }), false);
var size = new Size(100, 200),
half = size.divide(2),
shape = new Shape.Ellipse(half.negate(), size);
testPoint(shape, new Point(0, 0), true);
testPoint(shape, new Point(0, -1).multiply(half), true);
testPoint(shape, new Point({ length: 0.9, angle: 45 }).multiply(half), true);
testPoint(shape, new Point({ length: 1, angle: 45 }).multiply(half), true);
testPoint(shape, new Point({ length: 1.1, angle: 45 }).multiply(half), false);
var size = new Size(100, 200),
half = size.divide(2),
shape = new Shape.Rectangle(half.negate(), size);
testPoint(shape, new Point(0, 0), true);
testPoint(shape, new Point(0, 0.9).multiply(half), true);
testPoint(shape, new Point(0, 1).multiply(half), true);
testPoint(shape, new Point(0, 1.1).multiply(half), false);
testPoint(shape, new Point(0.9, 0).multiply(half), true);
testPoint(shape, new Point(1, 0).multiply(half), true);
testPoint(shape, new Point(1.1, 0).multiply(half), false);
});
test('Path#contains() (Rectangle Contours)', function() {
var path = new Path.Rectangle(new Point(100, 100), [200, 200]),
curves = path.getCurves();
for (var i = 0; i < curves.length; i++) {
testPoint(path, curves[i].getPoint(0), true);
testPoint(path, curves[i].getPoint(0.5), true);
}
});
test('Path#contains() (Rotated Rectangle Contours)', function() {
var path = new Path.Rectangle(new Point(100, 100), [200, 200]),
curves = path.getCurves();
path.rotate(45);
for (var i = 0; i < curves.length; i++) {
testPoint(path, curves[i].getPoint(0), true);
testPoint(path, curves[i].getPoint(0.5), true);
}
});
test('Path#contains() (touching stationary point with changing orientation)', function() {
var path = new Path({
segments: [
new Segment([100, 100]),
new Segment([200, 200], [-50, 0], [50, 0]),
new Segment([300, 300]),
new Segment([300, 100])
],
closed: true
});
testPoint(path, new Point(200, 200), true);
})