Skip to content
This repository has been archived by the owner on Nov 7, 2018. It is now read-only.

Commit

Permalink
Add d3.geo.berghaus.invert.
Browse files Browse the repository at this point in the history
  • Loading branch information
jasondavies committed Feb 18, 2013
1 parent c50d85f commit ae27729
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
13 changes: 13 additions & 0 deletions geo/projection/berghaus.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,19 @@ function berghaus(n) {
return p;
}

forward.invert = function(x, y) {
var r = Math.sqrt(x * x + y * y);
if (r > π / 2) {
var θ = Math.atan2(y, x),
θ0 = k * Math.round((θ - π / 2) / k) + π / 2,
s = θ > θ0 ? -1 : 1,
cotα = 1 / Math.tan(s * Math.acos((r * Math.cos(θ0 - θ) - π) / Math.sqrt(π * π + r * r - 2 * π * r * Math.cos(θ0 - θ))));
θ = θ0 + 2 * Math.atan((cotα + s * Math.sqrt(cotα * cotα - 3)) / 3);
x = r * Math.cos(θ), y = r * Math.sin(θ);
}
return berghausAzimuthalEquidistant.invert(x, y);
};

return forward;
}

Expand Down
26 changes: 26 additions & 0 deletions geo/projection/test/berghaus-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
require("./env");

var vows = require("vows"),
assert = require("assert");

var suite = vows.describe("d3.geo.berghaus");

suite.addBatch({
"berghaus": {
topic: d3.geo.berghaus,
"projections and inverse projections": function(berghaus) {
assert.equalInverse(berghaus, [ 0, 0], [480, 250]);
assert.equalInverse(berghaus, [ 0, -45], [480, 367.809724]);
assert.equalInverse(berghaus, [ 0, 45], [480, 132.190275]);
assert.equalInverse(berghaus, [-90, 0], [244.380550, 250]);
assert.equalInverse(berghaus, [ 90, 0], [715.619449, 250]);
assert.equalInverse(berghaus, [-80, 15], [277.038148, 194.777583]);
assert.equalInverse(berghaus, [ 1, 1], [482.617728, 247.381873]);
assert.equalInverse(berghaus, [ 15, 45], [510.778518, 131.080938]);
assert.equalInverse(berghaus, [120, 30], [750.967904, 114.867516]);
assert.equalInverse(berghaus, [110, 10], [759.454234, 183.963114]);
}
}
});

suite.export(module);

0 comments on commit ae27729

Please sign in to comment.