From f76f56445179acd4a0415a83da4fee4de0afff0e Mon Sep 17 00:00:00 2001 From: Mike Bostock Date: Tue, 30 Apr 2013 15:30:56 -0700 Subject: [PATCH] Fix #993; d3.geom.hull is now ccw. --- src/geom/hull.js | 2 +- test/geom/hull-test.js | 16 ++++++++++------ 2 files changed, 11 insertions(+), 7 deletions(-) diff --git a/src/geom/hull.js b/src/geom/hull.js index e831c5b6..280f6af4 100644 --- a/src/geom/hull.js +++ b/src/geom/hull.js @@ -95,7 +95,7 @@ d3.geom.hull = function(vertices) { // construct the hull var poly = []; - for (i = 0; i < sp; ++i) poly.push(data[stack[i]]); + for (i = sp - 1; i >= 0; --i) poly.push(data[stack[i]]); return poly; } diff --git a/test/geom/hull-test.js b/test/geom/hull-test.js index fef988fc..39db7677 100644 --- a/test/geom/hull-test.js +++ b/test/geom/hull-test.js @@ -1,4 +1,5 @@ var vows = require("vows"), + _ = require("../../"), load = require("../load"), assert = require("../assert"); @@ -26,11 +27,14 @@ suite.addBatch({ "of two points is empty": function(h) { assert.deepEqual(h([[200, 200], [760, 300]]), []); }, - "for three points is empty": function(h) { - assert.deepEqual(h([[200, 200], [760, 300], [500, 500]]), [[200, 200], [760, 300], [500, 500]]); + "for three points": function(h) { + assert.deepEqual(h([[200, 200], [760, 300], [500, 500]]), [[500, 500], [760, 300], [200, 200]]); }, "for four points": function(h) { - assert.deepEqual(h([[200, 200], [760, 300], [500, 500], [400, 400]]), [[200, 200], [760, 300], [500, 500]]); + assert.deepEqual(h([[200, 200], [760, 300], [500, 500], [400, 400]]), [[500, 500], [760, 300], [200, 200]]); + }, + "returns a counter-clockwise polygon": function(h) { + assert.greater(_.geom.polygon(h([[200, 200], [760, 300], [500, 500], [400, 400]])).area(), 0); } }, "the hull layout with custom accessors": { @@ -38,7 +42,7 @@ suite.addBatch({ return hull().x(function(d) { return d.x; }).y(function(d) { return d.y; }); }, "of four points": function(h) { - assert.deepEqual(h([{x: 200, y: 200}, {x: 760, y: 300}, {x: 500, y: 500}, {x: 400, y: 400}]), [{x: 200, y: 200}, {x: 760, y: 300}, {x: 500, y: 500}]); + assert.deepEqual(h([{x: 200, y: 200}, {x: 760, y: 300}, {x: 500, y: 500}, {x: 400, y: 400}]), [{x: 500, y: 500}, {x: 760, y: 300}, {x: 200, y: 200}]); } }, "the default hull layout applied directly": { @@ -52,10 +56,10 @@ suite.addBatch({ return h([[200, 200], [760, 300]]); }, "for three points": function(h) { - assert.deepEqual(h([[200, 200], [760, 300], [500, 500]]), [[200, 200], [760, 300], [500, 500]]); + assert.deepEqual(h([[200, 200], [760, 300], [500, 500]]), [[500, 500], [760, 300], [200, 200]]); }, "for four points": function(h) { - assert.deepEqual(h([[200, 200], [760, 300], [500, 500], [400, 400]]), [[200, 200], [760, 300], [500, 500]]); + assert.deepEqual(h([[200, 200], [760, 300], [500, 500], [400, 400]]), [[500, 500], [760, 300], [200, 200]]); } } }