forked from benfred/venn.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
venn_test.html
161 lines (131 loc) · 6.77 KB
/
venn_test.html
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
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>venn.js tests</title>
<link rel="stylesheet" href="http://code.jquery.com/qunit/qunit-1.11.0.css">
<script src="http://code.jquery.com/qunit/qunit-1.11.0.js"></script>
<script src="./venn.js"></script>
<script>
function nearlyEqual(left, right, tolerance, message) {
message = message || "nearlyEqual";
tolerance = tolerance || 1e-5;
ok(Math.abs(left - right) < tolerance,
message + ": " + left + " ~== " + right);
}
test("fmin", function() {
// minimize simple 1 diminesial quadratic
var loss = function(values) { return (values[0] - 10) * (values[0] - 10); }
var solution = venn.fmin(loss, [0], {minErrorDelta:1e-10}).solution;
nearlyEqual(solution[0], 10, 1e-10);
});
test("circleIntegral", function() {
nearlyEqual(circleIntersection.circleIntegral(10, 0), 0, venn.SMALL,
"empty circle test");
nearlyEqual(circleIntersection.circleIntegral(10, 10), Math.PI * 10 * 10 / 2, venn.SMALL,
"half circle test");
});
test("circleArea", function() {
nearlyEqual(circleIntersection.circleArea(10,0), 0, venn.SMALL, "empty circle test");
nearlyEqual(circleIntersection.circleArea(10, 10), Math.PI*10*10/2, venn.SMALL,
"half circle test");
nearlyEqual(circleIntersection.circleArea(10, 20), Math.PI*10*10, venn.SMALL,
"full circle test");
});
test("circleOverlap", function() {
nearlyEqual(circleIntersection.circleOverlap(10, 10, 200), 0, venn.SMALL,
"nonoverlapping circles test");
nearlyEqual(circleIntersection.circleOverlap(10, 10, 0), Math.PI*10*10, venn.SMALL,
"full overlapping circles test");
nearlyEqual(circleIntersection.circleOverlap(10, 5, 5), Math.PI * 5 * 5, venn.SMALL,
"another fully overlapping circles test");
});
test("distanceFromIntersectArea", function() {
function testDistanceFromIntersectArea(r1, r2, overlap) {
var distance = venn.distanceFromIntersectArea(r1, r2,
overlap);
nearlyEqual(circleIntersection.circleOverlap(r1, r2, distance),
overlap,
1e-2);
}
testDistanceFromIntersectArea(1.9544100476116797,
2.256758334191025,
11);
testDistanceFromIntersectArea(111.06512962798197,
113.32348546565727,
1218);
testDistanceFromIntersectArea(44.456564007075,
149.4335753619362,
2799);
testDistanceFromIntersectArea(592.890,
134.75,
56995);
});
test("circleCircleIntersection", function() {
var testIntersection = function(p1, p2, msg) {
var points = circleIntersection.circleCircleIntersection(p1, p2);
// make sure that points are appropiately spaced
for (var i = 0; i < points.length; i++) {
var point = points[i];
nearlyEqual(circleIntersection.distance(point, p1),
p1.radius, venn.SMALL,
msg + ": test distance to p1 for point " + i);
nearlyEqual(circleIntersection.distance(point, p2),
p2.radius, venn.SMALL,
msg + ": test distance to p2 for point " + i );
}
return points;
}
// fully contained
equal(circleIntersection.circleCircleIntersection({x:0, y:3, radius:10},
{x:3, y:0, radius:20}).length,
0, "fully contained test");
// fully disjoint
equal(circleIntersection.circleCircleIntersection({x:0, y:0, radius:10},
{x:21, y:0, radius:10}).length,
0, "fully disjoint test");
// midway between 2 points on y axis
var points = testIntersection({x:0, y:0, radius:10},
{x:10, y:0, radius:10},
"test midway intersection");
equal(points.length, 2);
nearlyEqual(points[0].x, 5, venn.SMALL);
nearlyEqual(points[1].x, 5);
nearlyEqual(points[0].y, -1 * points[1].y);
// failing case from input
var points = testIntersection({radius: 10, x: 15, y: 5},
{radius: 10, x: 20, y: 0},
"test intersection2");
equal(points.length, 2);
});
test("disjointCircles", function() {
// each one of these circles overlaps all the others, but the total overlap is still 0
var circles = [{"x":0.909, "y":0.905, "radius":0.548},
{"x":0.765, "y":0.382, "radius":0.703},
{"x":0.630, "y":0.019, "radius":0.449},
{"x":0.210, "y":0.755, "radius":0.656},
{"x":0.276, "y":0.723, "radius":1.145},
{"x":0.141, "y":0.585, "radius":0.419}];
var area = circleIntersection.intersectionArea(circles);
ok(area == 0);
// no intersection points, but the smallest circle is completely overlapped by each of the others
circles = [{"x":0.426, "y":0.882,"radius":0.944},
{"x":0.240, "y":0.685,"radius":0.992},
{"x":0.010, "y":0.909,"radius":1.161},
{"x":0.540, "y":0.475,"radius":0.410}];
ok(circles[3].radius * circles[3].radius * Math.PI == circleIntersection.intersectionArea(circles));
});
test("randomFailures", function() {
var circles = [{"x":0.501,"y":0.320,"radius":0.629},
{"x":0.945,"y":0.022,"radius":1.015},
{"x":0.021,"y":0.863,"radius":0.261},
{"x":0.528,"y":0.090,"radius":0.676}],
area = circleIntersection.intersectionArea(circles);
ok(Math.abs(area - 0.0008914) < 0.0001, area);
});
</script>
</head>
<body>
<div id="qunit"></div>
</body>
</html>