2011-10-08 20:30:17 +04:00
|
|
|
require("../env");
|
|
|
|
|
|
|
|
var vows = require("vows"),
|
|
|
|
assert = require("assert");
|
|
|
|
|
|
|
|
var suite = vows.describe("d3.geom.polygon");
|
|
|
|
|
|
|
|
suite.addBatch({
|
2012-01-30 02:27:41 +04:00
|
|
|
"closed counterclockwise unit square": {
|
2011-10-08 20:30:17 +04:00
|
|
|
topic: function() {
|
2011-10-08 21:19:01 +04:00
|
|
|
return d3.geom.polygon([[0, 0], [0, 1], [1, 1], [1, 0], [0, 0]]);
|
|
|
|
},
|
2012-01-30 02:27:41 +04:00
|
|
|
"has area 1": function(polygon) {
|
2011-10-08 21:19:01 +04:00
|
|
|
assert.equal(polygon.area(), 1);
|
|
|
|
},
|
2012-01-30 02:27:41 +04:00
|
|
|
"has centroid ⟨.5,.5⟩": function(polygon) {
|
2011-10-08 21:19:01 +04:00
|
|
|
assert.deepEqual(polygon.centroid(), [.5, .5]);
|
|
|
|
}
|
|
|
|
},
|
2012-01-30 02:27:41 +04:00
|
|
|
"closed clockwise unit square": {
|
2011-10-08 21:19:01 +04:00
|
|
|
topic: function() {
|
2012-01-30 02:27:41 +04:00
|
|
|
return d3.geom.polygon([[0, 0], [1, 0], [1, 1], [0, 1], [0, 0]]);
|
2011-10-08 21:19:01 +04:00
|
|
|
},
|
2012-01-30 02:27:41 +04:00
|
|
|
"has area 1": function(polygon) {
|
|
|
|
assert.equal(polygon.area(), -1);
|
2011-10-08 21:19:01 +04:00
|
|
|
},
|
2012-01-30 02:27:41 +04:00
|
|
|
"has centroid ⟨.5,.5⟩": function(polygon) {
|
2011-10-08 21:19:01 +04:00
|
|
|
assert.deepEqual(polygon.centroid(), [.5, .5]);
|
|
|
|
}
|
|
|
|
},
|
2012-01-30 02:27:41 +04:00
|
|
|
"closed clockwise triangle": {
|
2011-10-08 21:19:01 +04:00
|
|
|
topic: function() {
|
2012-01-30 02:27:41 +04:00
|
|
|
return d3.geom.polygon([[1, 1], [3, 2], [2, 3], [1, 1]]);
|
2011-10-08 21:19:01 +04:00
|
|
|
},
|
2012-01-30 02:27:41 +04:00
|
|
|
"has area 1.5": function(polygon) {
|
|
|
|
assert.equal(polygon.area(), -1.5);
|
|
|
|
},
|
|
|
|
"has centroid ⟨2,2⟩": function(polygon) {
|
|
|
|
var centroid = polygon.centroid();
|
|
|
|
assert.inDelta(centroid[0], 2, 1e-6);
|
|
|
|
assert.inDelta(centroid[1], 2, 1e-6);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
"open counterclockwise unit square": {
|
|
|
|
topic: function() {
|
|
|
|
return d3.geom.polygon([[0, 0], [0, 1], [1, 1], [1, 0]]);
|
2011-10-08 21:19:01 +04:00
|
|
|
},
|
2012-01-30 02:27:41 +04:00
|
|
|
"has area 1": function(polygon) {
|
|
|
|
assert.equal(polygon.area(), 1);
|
|
|
|
},
|
|
|
|
"has centroid ⟨.5,.5⟩": function(polygon) {
|
2011-10-08 21:19:01 +04:00
|
|
|
assert.deepEqual(polygon.centroid(), [.5, .5]);
|
|
|
|
}
|
|
|
|
},
|
2012-01-30 02:27:41 +04:00
|
|
|
"open clockwise unit square": {
|
2011-10-08 21:19:01 +04:00
|
|
|
topic: function() {
|
|
|
|
return d3.geom.polygon([[0, 0], [1, 0], [1, 1], [0, 1]]);
|
|
|
|
},
|
2012-01-30 02:27:41 +04:00
|
|
|
"has area 1": function(polygon) {
|
2011-10-08 21:19:01 +04:00
|
|
|
assert.equal(polygon.area(), -1);
|
|
|
|
},
|
2012-01-30 02:27:41 +04:00
|
|
|
"has centroid ⟨.5,.5⟩": function(polygon) {
|
2011-10-08 21:19:01 +04:00
|
|
|
assert.deepEqual(polygon.centroid(), [.5, .5]);
|
2011-10-08 20:30:17 +04:00
|
|
|
}
|
2012-01-30 02:27:41 +04:00
|
|
|
},
|
|
|
|
"open clockwise triangle": {
|
|
|
|
topic: function() {
|
|
|
|
return d3.geom.polygon([[1, 1], [3, 2], [2, 3]]);
|
|
|
|
},
|
|
|
|
"has area 1.5": function(polygon) {
|
|
|
|
assert.equal(polygon.area(), -1.5);
|
|
|
|
},
|
|
|
|
"has centroid ⟨2,2⟩": function(polygon) {
|
|
|
|
var centroid = polygon.centroid();
|
|
|
|
assert.inDelta(centroid[0], 2, 1e-6);
|
|
|
|
assert.inDelta(centroid[1], 2, 1e-6);
|
|
|
|
}
|
2012-12-06 14:40:06 +04:00
|
|
|
},
|
|
|
|
"large square": {
|
|
|
|
topic: function() {
|
|
|
|
var r = 1e8,
|
|
|
|
d = d3.range(0, r, r / 1e4);
|
|
|
|
return d3.geom.polygon(
|
|
|
|
d.map(function(y) { return [0, y]; }).concat(
|
|
|
|
d.map(function(x) { return [x, r]; })).concat(
|
|
|
|
d.map(function(y) { return [r, y]; }).reverse()).concat(
|
|
|
|
d.map(function(x) { return [x, 0]; }).reverse()));
|
|
|
|
},
|
|
|
|
"has area 1e16 - 5e7": function(polygon) {
|
|
|
|
assert.equal(polygon.area(), 1e16 - 5e7);
|
|
|
|
}
|
|
|
|
},
|
2011-10-08 20:30:17 +04:00
|
|
|
});
|
|
|
|
|
|
|
|
suite.export(module);
|