UNPKG

1.77 kBJavaScriptView Raw
1/*
2 * Buckminster Fuller’s spherical triangle transformation procedure
3 *
4 * Based on Robert W. Gray’s formulae published in “Exact Transformation Equations
5 * For Fuller's World Map,” _Cartographica_, 32(3): 17-25 (1995).
6 *
7 * Implemented for D3.js by Philippe Rivière, 2018 (https://visionscarto.net/)
8 *
9 * To the extent possible under law, Philippe Rivière has waived all copyright
10 * and related or neighboring rights to this implementation. (Public Domain.)
11 */
12import { abs, atan2, cos, epsilon, sin, sqrt } from "./math.js";
13import { geoGnomonicRaw as gnomonicRaw } from "d3-geo";
14
15export default function GrayFullerRaw() {
16 var SQRT_3 = sqrt(3);
17
18 // Gray’s constants
19 var Z = sqrt(5 + 2 * sqrt(5)) / sqrt(15),
20 el = sqrt(8) / sqrt(5 + sqrt(5)),
21 dve = sqrt(3 + sqrt(5)) / sqrt(5 + sqrt(5));
22
23 var grayfuller = function(lambda, phi) {
24 var cosPhi = cos(phi),
25 s = Z / (cosPhi * cos(lambda)),
26 x = cosPhi * sin(lambda) * s,
27 y = sin(phi) * s,
28 a1p = atan2(2 * y / SQRT_3 + el / 3 - el / 2, dve),
29 a2p = atan2(x - y / SQRT_3 + el / 3 - el / 2, dve),
30 a3p = atan2(el / 3 - x - y / SQRT_3 - el / 2, dve);
31
32 return [SQRT_3 * (a2p - a3p), 2 * a1p - a2p - a3p];
33 };
34
35 // Inverse approximation
36 grayfuller.invert = function(x, y) {
37 // if the point is out of the triangle, return
38 // something meaningless (but far away enough)
39 if (x * x + y * y > 5) return [0, 3];
40
41 var R = 2.9309936378128416,
42 p = gnomonicRaw.invert(x / R, y / R);
43
44 var j = 0;
45 do {
46 var f = grayfuller(p[0], p[1]),
47 dx = x - f[0],
48 dy = y - f[1];
49 p[0] += 0.2 * dx;
50 p[1] += 0.2 * dy;
51 } while (j++ < 30 && abs(dx) + abs(dy) > epsilon);
52
53 return p;
54 };
55
56 return grayfuller;
57}
58
\No newline at end of file