Skip to content

Commit ee4c7db

Browse files
committed
Add d3.geom.lengthSink.
1 parent a3ca2ce commit ee4c7db

File tree

4 files changed

+114
-5
lines changed

4 files changed

+114
-5
lines changed

d3.js

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4732,6 +4732,51 @@
47324732
}
47334733
};
47344734
};
4735+
d3.geom.lengthSink = function() {
4736+
var x00, y00, x0, y0, sum = 0;
4737+
var sink = {
4738+
point: d3_noop,
4739+
lineStart: lineStart,
4740+
lineEnd: lineEnd,
4741+
polygonStart: polygonStart,
4742+
polygonEnd: polygonEnd,
4743+
value: value
4744+
};
4745+
function pointLineStart(x, y) {
4746+
x00 = x0 = x;
4747+
y00 = y0 = y;
4748+
sink.point = pointLine;
4749+
}
4750+
function pointLine(x, y) {
4751+
var dx = x - x0, dy = y - y0;
4752+
x0 = x;
4753+
y0 = y;
4754+
sum += Math.sqrt(dx * dx + dy * dy);
4755+
}
4756+
function lineStart() {
4757+
sink.point = pointLineStart;
4758+
}
4759+
function lineEnd() {
4760+
x00 = y00 = x0 = y0 = undefined;
4761+
sink.point = d3_noop;
4762+
}
4763+
function lineEndPolygon() {
4764+
pointLine(x00, y00);
4765+
lineEnd();
4766+
}
4767+
function polygonStart() {
4768+
sink.lineEnd = lineEndPolygon;
4769+
}
4770+
function polygonEnd() {
4771+
sink.lineEnd = lineEnd;
4772+
}
4773+
function value() {
4774+
var value = sum;
4775+
sum = 0;
4776+
return value;
4777+
}
4778+
return sink;
4779+
};
47354780
d3.geom.matrix = function(a, b, c, d, e, f, sink) {
47364781
return {
47374782
polygonStart: function() {

d3.min.js

Lines changed: 5 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/geom/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,6 @@ import "json-source";
99
import "context-sink";
1010
import "path-sink";
1111
import "bounds-sink";
12+
import "length-sink";
1213
import "matrix";
1314
import "clip-extent";

src/geom/length-sink.js

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
import "../core/noop";
2+
import "geom";
3+
4+
d3.geom.lengthSink = function() {
5+
var x00,
6+
y00,
7+
x0,
8+
y0,
9+
sum = 0;
10+
11+
var sink = {
12+
point: d3_noop,
13+
lineStart: lineStart,
14+
lineEnd: lineEnd,
15+
polygonStart: polygonStart,
16+
polygonEnd: polygonEnd,
17+
value: value
18+
};
19+
20+
function pointLineStart(x, y) {
21+
x00 = x0 = x;
22+
y00 = y0 = y;
23+
sink.point = pointLine;
24+
}
25+
26+
function pointLine(x, y) {
27+
var dx = x - x0,
28+
dy = y - y0;
29+
x0 = x;
30+
y0 = y;
31+
sum += Math.sqrt(dx * dx + dy * dy);
32+
}
33+
34+
function lineStart() {
35+
sink.point = pointLineStart;
36+
}
37+
38+
function lineEnd() {
39+
x00 = y00 = x0 = y0 = undefined;
40+
sink.point = d3_noop;
41+
}
42+
43+
function lineEndPolygon() {
44+
pointLine(x00, y00);
45+
lineEnd();
46+
}
47+
48+
function polygonStart() {
49+
sink.lineEnd = lineEndPolygon;
50+
}
51+
52+
function polygonEnd() {
53+
sink.lineEnd = lineEnd;
54+
}
55+
56+
function value() {
57+
var value = sum;
58+
sum = 0;
59+
return value;
60+
}
61+
62+
return sink;
63+
};

0 commit comments

Comments
 (0)