Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Comparing changes

Choose two branches to see what's changed or to start a new pull request. If you need to, you can also compare across forks.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also compare across forks.
base fork: mbostock/d3
...
head fork: mbostock/d3
Checking mergeability… Don't worry, you can still create the pull request.
  • 1 commit
  • 4 files changed
  • 0 commit comments
  • 1 contributor
Commits on Oct 02, 2013
@jasondavies jasondavies Add d3.geo.path.distance.
Unfortunately, d3.geo.path.length is unavailable since d3.geo.path()
returns a function, for which the "length" property cannot be
overridden.

Fixes #1556.
cc5d006
Showing with 62 additions and 5 deletions.
  1. +32 −0 d3.js
  2. +5 −5 d3.min.js
  3. +7 −0 src/geo/path.js
  4. +18 −0 test/geo/path-test.js
View
32 d3.js
@@ -3487,6 +3487,33 @@ d3 = function() {
}
return stream;
}
+ var d3_geo_pathDistanceSum, d3_geo_pathDistancePolygon, d3_geo_pathDistance = {
+ point: d3_noop,
+ lineStart: d3_geo_pathDistanceLineStart,
+ lineEnd: d3_noop,
+ polygonStart: function() {
+ d3_geo_pathDistancePolygon = true;
+ },
+ polygonEnd: function() {
+ d3_geo_pathDistancePolygon = false;
+ }
+ };
+ function d3_geo_pathDistanceLineStart() {
+ var x00, y00, x0, y0;
+ d3_geo_pathDistance.point = function(x, y) {
+ d3_geo_pathDistance.point = nextPoint;
+ x00 = x0 = x, y00 = y0 = y;
+ };
+ d3_geo_pathDistance.lineEnd = function() {
+ if (d3_geo_pathDistancePolygon) nextPoint(x00, y00);
+ d3_geo_pathDistance.point = d3_geo_pathDistance.lineEnd = d3_noop;
+ };
+ function nextPoint(x, y) {
+ var dx = x - x0, dy = y - y0;
+ d3_geo_pathDistanceSum += Math.sqrt(dx * dx + dy * dy);
+ x0 = x, y0 = y;
+ }
+ }
function d3_geo_resample(project) {
var δ2 = .5, cosMinDistance = Math.cos(30 * d3_radians), maxDepth = 16;
function resample(stream) {
@@ -3613,6 +3640,11 @@ d3 = function() {
d3.geo.stream(object, projectStream(d3_geo_pathBounds));
return [ [ d3_geo_pathBoundsX0, d3_geo_pathBoundsY0 ], [ d3_geo_pathBoundsX1, d3_geo_pathBoundsY1 ] ];
};
+ path.distance = function(object) {
+ d3_geo_pathDistanceSum = 0;
+ d3.geo.stream(object, projectStream(d3_geo_pathDistance));
+ return d3_geo_pathDistanceSum;
+ };
path.projection = function(_) {
if (!arguments.length) return projection;
projectStream = (projection = _) ? _.stream || d3_geo_pathProjectStream(_) : d3_identity;
View
10 d3.min.js
5 additions, 5 deletions not shown
View
7 src/geo/path.js
@@ -10,6 +10,7 @@ import "path-bounds";
import "path-buffer";
import "path-centroid";
import "path-context";
+import "path-distance";
import "projection";
import "resample";
import "stream";
@@ -55,6 +56,12 @@ d3.geo.path = function() {
return [[d3_geo_pathBoundsX0, d3_geo_pathBoundsY0], [d3_geo_pathBoundsX1, d3_geo_pathBoundsY1]];
};
+ path.distance = function(object) {
+ d3_geo_pathDistanceSum = 0;
+ d3.geo.stream(object, projectStream(d3_geo_pathDistance));
+ return d3_geo_pathDistanceSum;
+ };
+
path.projection = function(_) {
if (!arguments.length) return projection;
projectStream = (projection = _) ? _.stream || d3_geo_pathProjectStream(_) : d3_identity;
View
18 test/geo/path-test.js
@@ -122,6 +122,24 @@ suite.addBatch({
}
},
+ "distance": {
+ topic: function(p) {
+ return p.distance;
+ },
+ "of a point": function(distance) {
+ assert.strictEqual(distance({type: "Point", coordinates: [30, 0]}), 0);
+ },
+ "of a line with one segment": function(distance) {
+ assert.strictEqual(distance({type: "LineString", coordinates: [[30, 0], [0, 0]]}), 150);
+ },
+ "of a line with two segments": function(distance) {
+ assert.strictEqual(distance({type: "LineString", coordinates: [[30, 0], [0, 0], [30, 0]]}), 300);
+ },
+ "of a polygon": function(distance) {
+ assert.strictEqual(distance({type: "Polygon", coordinates: [[[0, 0], [0, 30], [30, 30], [30, 0], [0, 0]]]}), 600);
+ }
+ },
+
"centroid": {
topic: function(p) {
return p.centroid;

No commit comments for this range

Something went wrong with that request. Please try again.