Skip to content

Commit

Permalink
accumulate the lines separately in each polygon's clip function to av…
Browse files Browse the repository at this point in the history
…oid visibility mix-ups
  • Loading branch information
Fil committed Sep 16, 2018
1 parent 306cb33 commit 4ff826f
Showing 1 changed file with 21 additions and 12 deletions.
33 changes: 21 additions & 12 deletions src/clip/polygon.js
Original file line number Diff line number Diff line change
Expand Up @@ -138,11 +138,23 @@ function randsign(i, j) {

function clipLine(segments, pointVisible) {
return function(stream) {
var point0, lambda00, phi00, v00, v0, clean;
var point0, lambda00, phi00, v00, v0, clean, line, lines = [];
return {
lineStart: function() {
point0 = null;
clean = 1;
line = [];
},
lineEnd: function() {
if (v0) lines.push(line);
lines.forEach(function(line) {
stream.lineStart();
line.forEach(function(point) {
stream.point(...point); // can have 4 dimensions
});
stream.lineEnd();
});
lines = [];
},
point: function(lambda, phi, close) {
if (cos(lambda) == -1) lambda -= sign(sin(lambda)) * 1e-5; // move away from -180/180 https://github.com/d3/d3-geo/pull/108#issuecomment-323798937
Expand Down Expand Up @@ -202,25 +214,25 @@ function clipLine(segments, pointVisible) {
intersection = intersections[i];
v = !v;
if (v) {
stream.lineStart();
stream.point(
line = [];
line.push([
intersection[0],
intersection[1],
intersection.index,
intersection.t
);
]);
} else {
stream.point(
line.push([
intersection[0],
intersection[1],
intersection.index,
intersection.t
);
stream.lineEnd();
]);
lines.push(line);
}
}
}
if (v) stream.point(lambda, phi);
if (v) line.push([lambda, phi]);
} else {
for (i = 0, j = 100; i < segments.length && j > 0; ++i) {
s = segments[i];
Expand All @@ -236,13 +248,10 @@ function clipLine(segments, pointVisible) {
}
}
v00 = v = pointVisible((lambda00 = lambda), (phi00 = phi));
if (v) stream.lineStart(), stream.point(lambda, phi);
if (v) line = [], line.push([lambda, phi]);
}
(point0 = point), (v0 = v);
},
lineEnd: function() {
if (v0) stream.lineEnd();
},
// Rejoin first and last segments if there were intersections and the first
// and last points were visible.
clean: function() {
Expand Down

0 comments on commit 4ff826f

Please sign in to comment.