Skip to content

Commit

Permalink
Merge branch 'geom-centroid' of https://github.com/jasondavies/d3 int…
Browse files Browse the repository at this point in the history
…o centroid
  • Loading branch information
mbostock committed Oct 11, 2011
2 parents 50ecefd + f9148ed commit 9d9a3f7
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 3 deletions.
2 changes: 1 addition & 1 deletion d3.geom.js
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ d3.geom.polygon = function(coordinates) {
a,
b,
c;
if (!arguments.length) k = 1 / (6 * coordinates.area());
if (!arguments.length) k = -1 / (6 * coordinates.area());
while (++i < n) {
a = coordinates[i];
b = coordinates[i + 1];
Expand Down
2 changes: 1 addition & 1 deletion d3.geom.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src/geom/polygon.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ d3.geom.polygon = function(coordinates) {
a,
b,
c;
if (!arguments.length) k = 1 / (6 * coordinates.area());
if (!arguments.length) k = -1 / (6 * coordinates.area());
while (++i < n) {
a = coordinates[i];
b = coordinates[i + 1];
Expand Down
57 changes: 57 additions & 0 deletions test/geom/polygon-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
require("../env");
require("../../d3");
require("../../d3.geom");

var vows = require("vows"),
assert = require("assert");

var suite = vows.describe("d3.geom.polygon");

suite.addBatch({
"counterclockwise polygon (last point equal to start point)": {
topic: function() {
return d3.geom.polygon([[0, 0], [0, 1], [1, 1], [1, 0], [0, 0]]);
},
"area": function(polygon) {
assert.equal(polygon.area(), 1);
},
"centroid": function(polygon) {
assert.deepEqual(polygon.centroid(), [.5, .5]);
}
},
"counterclockwise polygon (implicitly ending at start point)": {
topic: function() {
return d3.geom.polygon([[0, 0], [0, 1], [1, 1], [1, 0]]);
},
"area": function(polygon) {
assert.equal(polygon.area(), 1);
},
"centroid": function(polygon) {
assert.deepEqual(polygon.centroid(), [.5, .5]);
}
},
"clockwise polygon (last point equal to start point)": {
topic: function() {
return d3.geom.polygon([[0, 0], [1, 0], [1, 1], [0, 1], [0, 0]]);
},
"area": function(polygon) {
assert.equal(polygon.area(), -1);
},
"centroid": function(polygon) {
assert.deepEqual(polygon.centroid(), [.5, .5]);
}
},
"clockwise polygon (implicitly ending at start point)": {
topic: function() {
return d3.geom.polygon([[0, 0], [1, 0], [1, 1], [0, 1]]);
},
"area": function(polygon) {
assert.equal(polygon.area(), -1);
},
"centroid": function(polygon) {
assert.deepEqual(polygon.centroid(), [.5, .5]);
}
}
});

suite.export(module);

0 comments on commit 9d9a3f7

Please sign in to comment.