Permalink
Browse files

Fix a centroid bug with CCW polygons.

  • Loading branch information...
1 parent 9edd4bc commit 0e0ba0890091071f55270ae37ffba50c212c6326 @mbostock mbostock committed Oct 7, 2011
Showing with 17 additions and 13 deletions.
  1. +6 −4 d3.geo.js
  2. +1 −1 d3.geo.min.js
  3. +1 −1 d3.js
  4. +1 −1 d3.min.js
  5. +1 −1 package.json
  6. +1 −1 src/core/core.js
  7. +6 −4 src/geo/path.js
View
@@ -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
}
View
Oops, something went wrong.
View
@@ -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) {
View
Oops, something went wrong.
View
@@ -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",
View
@@ -1 +1 @@
-d3 = {version: "2.3.3"}; // semver
+d3 = {version: "2.3.4"}; // semver
View
@@ -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
}

0 comments on commit 0e0ba08

Please sign in to comment.