Commit 0e0ba089 authored by Mike Bostock's avatar Mike Bostock
Browse files

Fix a centroid bug with CCW polygons.

parent 9edd4bc0
......@@ -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
}
......
This diff is collapsed.
......@@ -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) {
......
This diff is collapsed.
{
"name": "d3",
"version": "2.3.3",
"version": "2.3.4",
"description": "A small, free JavaScript library for manipulating documents based on data.",
"keywords": [
"dom",
......
d3 = {version: "2.3.3"}; // semver
d3 = {version: "2.3.4"}; // semver
......@@ -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
}
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment