Enforce built-in precision for outline, too.

This commit is contained in:
Mike Bostock 2012-10-01 18:20:14 -07:00
Родитель fb449fca26
Коммит 95c88ebd06
4 изменённых файлов: 154 добавлений и 38 удалений

37
d3.v2.js поставляемый
Просмотреть файл

@ -1971,6 +1971,22 @@
function d3_geo_equirectangular(λ, φ) {
return [ λ, φ ];
}
function d3_geo_graticuleX(y0, y1) {
var y = d3.range(y0, y1 - ε, d3_geo_graticulePrecision).concat(y1);
return function(x) {
return y.map(function(y) {
return [ x, y ];
});
};
}
function d3_geo_graticuleY(x0, x1) {
var x = d3.range(x0, x1 - ε, d3_geo_graticulePrecision).concat(x1);
return function(y) {
return x.map(function(x) {
return [ x, y ];
});
};
}
function d3_geo_greatArcSource(d) {
return d.source;
}
@ -6315,18 +6331,9 @@
geometries: graticule.lines()
};
}
var x1 = 180 - ε, x0 = -x1, y1 = 90 - ε, y0 = -y1, dx = 22.5, dy = dx, δx = 2, δy = 2;
var x1, x0, y1, y0, dx = 22.5, dy = dx, x, y;
graticule.lines = function() {
var xSteps = d3.range(x0, x1 - ε, δx).concat(x1), ySteps = d3.range(y0, y1 - ε, δy).concat(y1), xLines = d3.range(Math.ceil(x0 / dx) * dx, x1, dx).map(function(x) {
return ySteps.map(function(y) {
return [ x, y ];
});
}), yLines = d3.range(Math.ceil(y0 / dy) * dy, y1, dy).map(function(y) {
return xSteps.map(function(x) {
return [ x, y ];
});
});
return xLines.concat(yLines).map(function(coordinates) {
return d3.range(Math.ceil(x0 / dx) * dx, x1, dx).map(x).concat(d3.range(Math.ceil(y0 / dy) * dy, y1, dy).map(y)).map(function(coordinates) {
return {
type: "LineString",
coordinates: coordinates
@ -6334,10 +6341,9 @@
});
};
graticule.outline = function() {
var x2 = (x0 + x1) / 2;
return {
type: "Polygon",
coordinates: [ [ [ x0, y1 ], [ x2, y1 ], [ x1, y1 ], [ x1, y0 ], [ x2, y0 ], [ x0, y0 ], [ x0, y1 ] ] ]
coordinates: [ x(x0).concat(y(y1).slice(1), x(x1).reverse().slice(1), y(y0).reverse().slice(1)) ]
};
};
graticule.extent = function(_) {
@ -6346,6 +6352,8 @@
y0 = +_[0][1], y1 = +_[1][1];
if (x0 > x1) _ = x0, x0 = x1, x1 = _;
if (y0 > y1) _ = y0, y0 = y1, y1 = _;
x = d3_geo_graticuleX(y0, y1);
y = d3_geo_graticuleY(x0, x1);
return graticule;
};
graticule.step = function(_) {
@ -6353,8 +6361,9 @@
dx = +_[0], dy = +_[1];
return graticule;
};
return graticule;
return graticule.extent([ [ -180 + ε, -90 + ε ], [ 180 - ε, 90 - ε ] ]);
};
var d3_geo_graticulePrecision = 3;
d3.geo.greatArc = function() {
function greatArc() {
var d = greatArc.distance.apply(this, arguments), t = 0, dt = precision / d, coordinates = [ p0 ];

8
d3.v2.min.js поставляемый

Различия файлов скрыты, потому что одна или несколько строк слишком длинны

Просмотреть файл

@ -1,8 +1,8 @@
d3.geo.graticule = function() {
var x1 = 180 - ε, x0 = -x1,
y1 = 90 - ε, y0 = -y1,
var x1, x0,
y1, y0,
dx = 22.5, dy = dx,
δx = 2, δy = 2;
x, y;
function graticule() {
return {
@ -12,27 +12,25 @@ d3.geo.graticule = function() {
}
graticule.lines = function() {
var xSteps = d3.range(x0, x1 - ε, δx).concat(x1),
ySteps = d3.range(y0, y1 - ε, δy).concat(y1),
xLines = d3.range(Math.ceil(x0 / dx) * dx, x1, dx).map(function(x) { return ySteps.map(function(y) { return [x, y]; }); }),
yLines = d3.range(Math.ceil(y0 / dy) * dy, y1, dy).map(function(y) { return xSteps.map(function(x) { return [x, y]; }); });
return xLines.concat(yLines).map(function(coordinates) {
return {
type: "LineString",
coordinates: coordinates
};
});
return d3.range(Math.ceil(x0 / dx) * dx, x1, dx).map(x)
.concat(d3.range(Math.ceil(y0 / dy) * dy, y1, dy).map(y))
.map(function(coordinates) {
return {
type: "LineString",
coordinates: coordinates
};
});
}
graticule.outline = function() {
var x2 = (x0 + x1) / 2;
return {
type: "Polygon",
coordinates: [[
[x0, y1], [x2, y1], [x1, y1],
[x1, y0], [x2, y0], [x0, y0],
[x0, y1]
]]
coordinates: [
x(x0)
.concat(y(y1).slice(1),
x(x1).reverse().slice(1),
y(y0).reverse().slice(1))
]
};
};
@ -42,6 +40,8 @@ d3.geo.graticule = function() {
y0 = +_[0][1], y1 = +_[1][1];
if (x0 > x1) _ = x0, x0 = x1, x1 = _;
if (y0 > y1) _ = y0, y0 = y1, y1 = _;
x = d3_geo_graticuleX(y0, y1);
y = d3_geo_graticuleY(x0, x1);
return graticule;
};
@ -51,5 +51,25 @@ d3.geo.graticule = function() {
return graticule;
};
return graticule;
return graticule.extent([[-180 + ε, -90 + ε], [180 - ε, 90 - ε]]);
};
var d3_geo_graticulePrecision = 3;
function d3_geo_graticuleX(y0, y1) {
var y = d3.range(y0, y1 - ε, d3_geo_graticulePrecision).concat(y1);
return function(x) {
return y.map(function(y) {
return [x, y];
});
};
}
function d3_geo_graticuleY(x0, x1) {
var x = d3.range(x0, x1 - ε, d3_geo_graticulePrecision).concat(x1);
return function(y) {
return x.map(function(x) {
return [x, y];
});
};
}

Просмотреть файл

@ -0,0 +1,87 @@
require("../env");
var vows = require("vows"),
assert = require("assert");
var suite = vows.describe("d3.geo.graticule");
var ε = 1e-6;
suite.addBatch({
"graticule": {
topic: function() {
return d3.geo.graticule()
.extent([[-90, -45], [90, 45]])
.step([45, 45]);
},
"extent": {
"defaults to just inside –180º, -90º to +180º, +90º": function() {
assert.deepEqual(d3.geo.graticule().extent(), [[-180 + ε, -90 + ε], [180 - ε, 90 - ε]]);
},
"coerces input values to numbers": function() {
var graticule = d3.geo.graticule().extent([["-90", "-45"], ["+90", "+45"]]),
extent = graticule.extent();
assert.strictEqual(extent[0][0], -90);
assert.strictEqual(extent[0][1], -45);
assert.strictEqual(extent[1][0], +90);
assert.strictEqual(extent[1][1], +45);
}
},
"returns a GeometryCollection of LineStrings": function(graticule) {
assert.deepEqual(graticule(), {
type: "GeometryCollection",
geometries: [
{type: "LineString", coordinates: [[-90,-45],[-90,-42],[-90,-39],[-90,-36],[-90,-33],[-90,-30],[-90,-27],[-90,-24],[-90,-21],[-90,-18],[-90,-15],[-90,-12],[-90,-9],[-90,-6],[-90,-3],[-90,0],[-90,3],[-90,6],[-90,9],[-90,12],[-90,15],[-90,18],[-90,21],[-90,24],[-90,27],[-90,30],[-90,33],[-90,36],[-90,39],[-90,42],[-90,45]]},
{type: "LineString", coordinates: [[-45,-45],[-45,-42],[-45,-39],[-45,-36],[-45,-33],[-45,-30],[-45,-27],[-45,-24],[-45,-21],[-45,-18],[-45,-15],[-45,-12],[-45,-9],[-45,-6],[-45,-3],[-45,0],[-45,3],[-45,6],[-45,9],[-45,12],[-45,15],[-45,18],[-45,21],[-45,24],[-45,27],[-45,30],[-45,33],[-45,36],[-45,39],[-45,42],[-45,45]]},
{type: "LineString", coordinates: [[0,-45],[0,-42],[0,-39],[0,-36],[0,-33],[0,-30],[0,-27],[0,-24],[0,-21],[0,-18],[0,-15],[0,-12],[0,-9],[0,-6],[0,-3],[0,0],[0,3],[0,6],[0,9],[0,12],[0,15],[0,18],[0,21],[0,24],[0,27],[0,30],[0,33],[0,36],[0,39],[0,42],[0,45]]},
{type: "LineString", coordinates: [[45,-45],[45,-42],[45,-39],[45,-36],[45,-33],[45,-30],[45,-27],[45,-24],[45,-21],[45,-18],[45,-15],[45,-12],[45,-9],[45,-6],[45,-3],[45,0],[45,3],[45,6],[45,9],[45,12],[45,15],[45,18],[45,21],[45,24],[45,27],[45,30],[45,33],[45,36],[45,39],[45,42],[45,45]]},
{type: "LineString", coordinates: [[-90,-45],[-87,-45],[-84,-45],[-81,-45],[-78,-45],[-75,-45],[-72,-45],[-69,-45],[-66,-45],[-63,-45],[-60,-45],[-57,-45],[-54,-45],[-51,-45],[-48,-45],[-45,-45],[-42,-45],[-39,-45],[-36,-45],[-33,-45],[-30,-45],[-27,-45],[-24,-45],[-21,-45],[-18,-45],[-15,-45],[-12,-45],[-9,-45],[-6,-45],[-3,-45],[0,-45],[3,-45],[6,-45],[9,-45],[12,-45],[15,-45],[18,-45],[21,-45],[24,-45],[27,-45],[30,-45],[33,-45],[36,-45],[39,-45],[42,-45],[45,-45],[48,-45],[51,-45],[54,-45],[57,-45],[60,-45],[63,-45],[66,-45],[69,-45],[72,-45],[75,-45],[78,-45],[81,-45],[84,-45],[87,-45],[90,-45]]},
{type: "LineString", coordinates: [[-90,0],[-87,0],[-84,0],[-81,0],[-78,0],[-75,0],[-72,0],[-69,0],[-66,0],[-63,0],[-60,0],[-57,0],[-54,0],[-51,0],[-48,0],[-45,0],[-42,0],[-39,0],[-36,0],[-33,0],[-30,0],[-27,0],[-24,0],[-21,0],[-18,0],[-15,0],[-12,0],[-9,0],[-6,0],[-3,0],[0,0],[3,0],[6,0],[9,0],[12,0],[15,0],[18,0],[21,0],[24,0],[27,0],[30,0],[33,0],[36,0],[39,0],[42,0],[45,0],[48,0],[51,0],[54,0],[57,0],[60,0],[63,0],[66,0],[69,0],[72,0],[75,0],[78,0],[81,0],[84,0],[87,0],[90,0]]}
]
});
},
"step": {
"defaults to 22.5º, 22.5º": function(graticule) {
assert.deepEqual(d3.geo.graticule().step(), [22.5, 22.5]);
},
"coerces input values to numbers": function() {
var graticule = d3.geo.graticule().step(["45", "11.25"]),
step = graticule.step();
assert.strictEqual(step[0], 45);
assert.strictEqual(step[1], 11.25);
}
},
"outline": {
"returns a Polygon": function(graticule) {
assert.deepEqual(graticule.outline(), {
type: "Polygon",
coordinates: [[
[-90,-45],[-90,-42],[-90,-39],[-90,-36],[-90,-33],[-90,-30],[-90,-27],[-90,-24],[-90,-21],[-90,-18],[-90,-15],[-90,-12],[-90,-9],[-90,-6],[-90,-3],[-90,0],[-90,3],[-90,6],[-90,9],[-90,12],[-90,15],[-90,18],[-90,21],[-90,24],[-90,27],[-90,30],[-90,33],[-90,36],[-90,39],[-90,42],[-90,45],
[-87,45],[-84,45],[-81,45],[-78,45],[-75,45],[-72,45],[-69,45],[-66,45],[-63,45],[-60,45],[-57,45],[-54,45],[-51,45],[-48,45],[-45,45],[-42,45],[-39,45],[-36,45],[-33,45],[-30,45],[-27,45],[-24,45],[-21,45],[-18,45],[-15,45],[-12,45],[-9,45],[-6,45],[-3,45],[0,45],[3,45],[6,45],[9,45],[12,45],[15,45],[18,45],[21,45],[24,45],[27,45],[30,45],[33,45],[36,45],[39,45],[42,45],[45,45],[48,45],[51,45],[54,45],[57,45],[60,45],[63,45],[66,45],[69,45],[72,45],[75,45],[78,45],[81,45],[84,45],[87,45],[90,45],
[90,42],[90,39],[90,36],[90,33],[90,30],[90,27],[90,24],[90,21],[90,18],[90,15],[90,12],[90,9],[90,6],[90,3],[90,0],[90,-3],[90,-6],[90,-9],[90,-12],[90,-15],[90,-18],[90,-21],[90,-24],[90,-27],[90,-30],[90,-33],[90,-36],[90,-39],[90,-42],[90,-45],
[87,-45],[84,-45],[81,-45],[78,-45],[75,-45],[72,-45],[69,-45],[66,-45],[63,-45],[60,-45],[57,-45],[54,-45],[51,-45],[48,-45],[45,-45],[42,-45],[39,-45],[36,-45],[33,-45],[30,-45],[27,-45],[24,-45],[21,-45],[18,-45],[15,-45],[12,-45],[9,-45],[6,-45],[3,-45],[0,-45],[-3,-45],[-6,-45],[-9,-45],[-12,-45],[-15,-45],[-18,-45],[-21,-45],[-24,-45],[-27,-45],[-30,-45],[-33,-45],[-36,-45],[-39,-45],[-42,-45],[-45,-45],[-48,-45],[-51,-45],[-54,-45],[-57,-45],[-60,-45],[-63,-45],[-66,-45],[-69,-45],[-72,-45],[-75,-45],[-78,-45],[-81,-45],[-84,-45],[-87,-45],[-90,-45]
]]
});
}
},
"line": {
"returns an array of LineStrings": function(graticule) {
assert.deepEqual(graticule.lines(), [
{type: "LineString", coordinates: [[-90,-45],[-90,-42],[-90,-39],[-90,-36],[-90,-33],[-90,-30],[-90,-27],[-90,-24],[-90,-21],[-90,-18],[-90,-15],[-90,-12],[-90,-9],[-90,-6],[-90,-3],[-90,0],[-90,3],[-90,6],[-90,9],[-90,12],[-90,15],[-90,18],[-90,21],[-90,24],[-90,27],[-90,30],[-90,33],[-90,36],[-90,39],[-90,42],[-90,45]]},
{type: "LineString", coordinates: [[-45,-45],[-45,-42],[-45,-39],[-45,-36],[-45,-33],[-45,-30],[-45,-27],[-45,-24],[-45,-21],[-45,-18],[-45,-15],[-45,-12],[-45,-9],[-45,-6],[-45,-3],[-45,0],[-45,3],[-45,6],[-45,9],[-45,12],[-45,15],[-45,18],[-45,21],[-45,24],[-45,27],[-45,30],[-45,33],[-45,36],[-45,39],[-45,42],[-45,45]]},
{type: "LineString", coordinates: [[0,-45],[0,-42],[0,-39],[0,-36],[0,-33],[0,-30],[0,-27],[0,-24],[0,-21],[0,-18],[0,-15],[0,-12],[0,-9],[0,-6],[0,-3],[0,0],[0,3],[0,6],[0,9],[0,12],[0,15],[0,18],[0,21],[0,24],[0,27],[0,30],[0,33],[0,36],[0,39],[0,42],[0,45]]},
{type: "LineString", coordinates: [[45,-45],[45,-42],[45,-39],[45,-36],[45,-33],[45,-30],[45,-27],[45,-24],[45,-21],[45,-18],[45,-15],[45,-12],[45,-9],[45,-6],[45,-3],[45,0],[45,3],[45,6],[45,9],[45,12],[45,15],[45,18],[45,21],[45,24],[45,27],[45,30],[45,33],[45,36],[45,39],[45,42],[45,45]]},
{type: "LineString", coordinates: [[-90,-45],[-87,-45],[-84,-45],[-81,-45],[-78,-45],[-75,-45],[-72,-45],[-69,-45],[-66,-45],[-63,-45],[-60,-45],[-57,-45],[-54,-45],[-51,-45],[-48,-45],[-45,-45],[-42,-45],[-39,-45],[-36,-45],[-33,-45],[-30,-45],[-27,-45],[-24,-45],[-21,-45],[-18,-45],[-15,-45],[-12,-45],[-9,-45],[-6,-45],[-3,-45],[0,-45],[3,-45],[6,-45],[9,-45],[12,-45],[15,-45],[18,-45],[21,-45],[24,-45],[27,-45],[30,-45],[33,-45],[36,-45],[39,-45],[42,-45],[45,-45],[48,-45],[51,-45],[54,-45],[57,-45],[60,-45],[63,-45],[66,-45],[69,-45],[72,-45],[75,-45],[78,-45],[81,-45],[84,-45],[87,-45],[90,-45]]},
{type: "LineString", coordinates: [[-90,0],[-87,0],[-84,0],[-81,0],[-78,0],[-75,0],[-72,0],[-69,0],[-66,0],[-63,0],[-60,0],[-57,0],[-54,0],[-51,0],[-48,0],[-45,0],[-42,0],[-39,0],[-36,0],[-33,0],[-30,0],[-27,0],[-24,0],[-21,0],[-18,0],[-15,0],[-12,0],[-9,0],[-6,0],[-3,0],[0,0],[3,0],[6,0],[9,0],[12,0],[15,0],[18,0],[21,0],[24,0],[27,0],[30,0],[33,0],[36,0],[39,0],[42,0],[45,0],[48,0],[51,0],[54,0],[57,0],[60,0],[63,0],[66,0],[69,0],[72,0],[75,0],[78,0],[81,0],[84,0],[87,0],[90,0]]}
]);
}
}
}
});
suite.export(module);