Skip to content

Commit c26475a

Browse files
committed
Add voronoi.clipExtent; fixes d3#1267.
This deprecates voronoi.size.
1 parent 1221458 commit c26475a

4 files changed

Lines changed: 55 additions & 25 deletions

File tree

d3.js

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4291,7 +4291,7 @@ d3 = function() {
42914291
return triangles;
42924292
};
42934293
d3.geom.voronoi = function(points) {
4294-
var size = null, x = d3_svg_lineX, y = d3_svg_lineY, clip;
4294+
var x = d3_svg_lineX, y = d3_svg_lineY, clipPolygon = null;
42954295
if (arguments.length) return voronoi(points);
42964296
function voronoi(data) {
42974297
var points, polygons = data.map(function() {
@@ -4358,7 +4358,7 @@ d3 = function() {
43584358
}
43594359
}
43604360
});
4361-
if (clip) for (i = 0; i < n; ++i) clip(polygons[i]);
4361+
if (clipPolygon) for (i = 0; i < n; ++i) clipPolygon.clip(polygons[i]);
43624362
for (i = 0; i < n; ++i) polygons[i].point = data[i];
43634363
return polygons;
43644364
}
@@ -4368,16 +4368,18 @@ d3 = function() {
43684368
voronoi.y = function(_) {
43694369
return arguments.length ? (y = _, voronoi) : y;
43704370
};
4371-
voronoi.size = function(_) {
4372-
if (!arguments.length) return size;
4373-
if (_ == null) {
4374-
clip = null;
4375-
} else {
4376-
size = [ +_[0], +_[1] ];
4377-
clip = d3.geom.polygon([ [ 0, 0 ], [ 0, size[1] ], size, [ size[0], 0 ] ]).clip;
4371+
voronoi.clipExtent = function(_) {
4372+
if (!arguments.length) return clipPolygon && [ clipPolygon[0], clipPolygon[2] ];
4373+
if (_ == null) clipPolygon = null; else {
4374+
var x1 = +_[0][0], y1 = +_[0][1], x2 = +_[1][0], y2 = +_[1][1];
4375+
clipPolygon = d3.geom.polygon([ [ x1, y1 ], [ x1, y2 ], [ x2, y2 ], [ x2, y1 ] ]);
43784376
}
43794377
return voronoi;
43804378
};
4379+
voronoi.size = function(_) {
4380+
if (!arguments.length) return clipPolygon && clipPolygon[2];
4381+
return voronoi.clipExtent(_ && [ [ 0, 0 ], _ ]);
4382+
};
43814383
voronoi.links = function(data) {
43824384
var points, graph = data.map(function() {
43834385
return [];

d3.min.js

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

src/geom/voronoi.js

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,9 @@ import "polygon";
2727
* @returns polygons [[[x1, y1], [x2, y2], …], …]
2828
*/
2929
d3.geom.voronoi = function(points) {
30-
var size = null,
31-
x = d3_svg_lineX,
30+
var x = d3_svg_lineX,
3231
y = d3_svg_lineY,
33-
clip;
32+
clipPolygon = null;
3433

3534
// For backwards-compatibility.
3635
if (arguments.length) return voronoi(points);
@@ -124,7 +123,7 @@ d3.geom.voronoi = function(points) {
124123
}
125124
});
126125

127-
if (clip) for (i = 0; i < n; ++i) clip(polygons[i]);
126+
if (clipPolygon) for (i = 0; i < n; ++i) clipPolygon.clip(polygons[i]);
128127
for (i = 0; i < n; ++i) polygons[i].point = data[i];
129128

130129
return polygons;
@@ -138,17 +137,22 @@ d3.geom.voronoi = function(points) {
138137
return arguments.length ? (y = _, voronoi) : y;
139138
};
140139

141-
voronoi.size = function(_) {
142-
if (!arguments.length) return size;
143-
if (_ == null) {
144-
clip = null;
145-
} else {
146-
size = [+_[0], +_[1]];
147-
clip = d3.geom.polygon([[0, 0], [0, size[1]], size, [size[0], 0]]).clip;
140+
voronoi.clipExtent = function(_) {
141+
if (!arguments.length) return clipPolygon && [clipPolygon[0], clipPolygon[2]];
142+
if (_ == null) clipPolygon = null;
143+
else {
144+
var x1 = +_[0][0], y1 = +_[0][1], x2 = +_[1][0], y2 = +_[1][1];
145+
clipPolygon = d3.geom.polygon([[x1, y1], [x1, y2], [x2, y2], [x2, y1]]);
148146
}
149147
return voronoi;
150148
};
151149

150+
// @deprecated; use clipExtent instead
151+
voronoi.size = function(_) {
152+
if (!arguments.length) return clipPolygon && clipPolygon[2];
153+
return voronoi.clipExtent(_ && [[0, 0], _]);
154+
};
155+
152156
voronoi.links = function(data) {
153157
var points,
154158
graph = data.map(function() { return []; }),

test/geom/voronoi-test.js

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,40 @@ suite.addBatch({
1212
topic: function(voronoi) {
1313
return voronoi();
1414
},
15+
"has no defined clip extent": function(v) {
16+
assert.isNull(v.clipExtent());
17+
},
1518
"has no defined size": function(v) {
1619
assert.isNull(v.size());
1720
},
21+
"returns the configured clip extent": function(v) {
22+
try {
23+
assert.deepEqual(v.clipExtent([[1, 2], [3, 4]]).clipExtent(), [[1, 2], [3, 4]]);
24+
} finally {
25+
v.clipExtent(null);
26+
}
27+
},
1828
"returns the configured size": function(v) {
1929
try {
20-
assert.deepEqual(v.size([100, 100]).size(), [100, 100]);
30+
assert.deepEqual(v.size([1, 2]).size(), [1, 2]);
31+
} finally {
32+
v.size(null);
33+
}
34+
},
35+
"size implies a clip extent from [0, 0]": function(v) {
36+
try {
37+
assert.deepEqual(v.size([1, 2]).clipExtent(), [[0, 0], [1, 2]]);
2138
} finally {
2239
v.size(null);
2340
}
2441
},
42+
"clip extent implies a size, assuming [0, 0]": function(v) {
43+
try {
44+
assert.deepEqual(v.clipExtent([[1, 2], [3, 4]]).size(), [3, 4]);
45+
} finally {
46+
v.clipExtent(null);
47+
}
48+
},
2549
"has the default x-accessor, d[0]": function(v) {
2650
assert.strictEqual(v.x()([42, 43]), 42);
2751
},
@@ -133,12 +157,12 @@ suite.addBatch({
133157
}
134158
},
135159

136-
"a voronoi layout with size 960x500": {
160+
"a voronoi layout with clip extent [[0, 0], [960, 500]]": {
137161
topic: function(voronoi) {
138162
return voronoi()
139163
.x(function(d) { return d.x; })
140164
.y(function(d) { return d.y; })
141-
.size([960, 500]);
165+
.clipExtent([[0, 0], [960, 500]]);
142166
},
143167
"of two points": {
144168
topic: function(v) {

0 commit comments

Comments
 (0)