Skip to content

Commit

Permalink
Fix a centroid bug with CCW polygons.
Browse files Browse the repository at this point in the history
  • Loading branch information
mbostock committed Oct 7, 2011
1 parent 9edd4bc commit 0e0ba08
Show file tree
Hide file tree
Showing 7 changed files with 17 additions and 13 deletions.
10 changes: 6 additions & 4 deletions d3.geo.js
Expand Up @@ -542,18 +542,20 @@ d3.geo.path = function() {

function polygonCentroid(coordinates) {
var polygon = d3.geom.polygon(coordinates[0].map(projection)), // exterior ring
centroid = polygon.centroid(1),
area = polygon.area(),
centroid = polygon.centroid(area < 0 ? (area *= -1, 1) : -1),
x = centroid[0],
y = centroid[1],
z = Math.abs(polygon.area()),
z = area,
i = 0, // coordinates index
n = coordinates.length;
while (++i < n) {
polygon = d3.geom.polygon(coordinates[i].map(projection)); // holes
centroid = polygon.centroid(1);
area = polygon.area();
centroid = polygon.centroid(area < 0 ? (area *= -1, 1) : -1);
x -= centroid[0];
y -= centroid[1];
z -= Math.abs(polygon.area());
z -= area;
}
return [x, y, 6 * z]; // weighted centroid
}
Expand Down
2 changes: 1 addition & 1 deletion d3.geo.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion d3.js
Expand Up @@ -10,7 +10,7 @@ try {
d3_style_setProperty.call(this, name, value + "", priority);
};
}
d3 = {version: "2.3.3"}; // semver
d3 = {version: "2.3.4"}; // semver
var d3_array = d3_arraySlice; // conversion for NodeLists

function d3_arrayCopy(pseudoarray) {
Expand Down
2 changes: 1 addition & 1 deletion d3.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
@@ -1,6 +1,6 @@
{
"name": "d3",
"version": "2.3.3",
"version": "2.3.4",
"description": "A small, free JavaScript library for manipulating documents based on data.",
"keywords": [
"dom",
Expand Down
2 changes: 1 addition & 1 deletion src/core/core.js
@@ -1 +1 @@
d3 = {version: "2.3.3"}; // semver
d3 = {version: "2.3.4"}; // semver
10 changes: 6 additions & 4 deletions src/geo/path.js
Expand Up @@ -186,18 +186,20 @@ d3.geo.path = function() {

function polygonCentroid(coordinates) {
var polygon = d3.geom.polygon(coordinates[0].map(projection)), // exterior ring
centroid = polygon.centroid(1),
area = polygon.area(),
centroid = polygon.centroid(area < 0 ? (area *= -1, 1) : -1),
x = centroid[0],
y = centroid[1],
z = Math.abs(polygon.area()),
z = area,
i = 0, // coordinates index
n = coordinates.length;
while (++i < n) {
polygon = d3.geom.polygon(coordinates[i].map(projection)); // holes
centroid = polygon.centroid(1);
area = polygon.area();
centroid = polygon.centroid(area < 0 ? (area *= -1, 1) : -1);
x -= centroid[0];
y -= centroid[1];
z -= Math.abs(polygon.area());
z -= area;
}
return [x, y, 6 * z]; // weighted centroid
}
Expand Down

0 comments on commit 0e0ba08

Please sign in to comment.