source: node_modules/d3-geo/src/area.js

Last change on this file was e4c61dd, checked in by istevanoska <ilinastevanoska@…>, 6 months ago

Prototype 1.1

  • Property mode set to 100644
File size: 2.0 KB
RevLine 
[e4c61dd]1import {Adder} from "d3-array";
2import {atan2, cos, quarterPi, radians, sin, tau} from "./math.js";
3import noop from "./noop.js";
4import stream from "./stream.js";
5
6export var areaRingSum = new Adder();
7
8// hello?
9
10var areaSum = new Adder(),
11 lambda00,
12 phi00,
13 lambda0,
14 cosPhi0,
15 sinPhi0;
16
17export var areaStream = {
18 point: noop,
19 lineStart: noop,
20 lineEnd: noop,
21 polygonStart: function() {
22 areaRingSum = new Adder();
23 areaStream.lineStart = areaRingStart;
24 areaStream.lineEnd = areaRingEnd;
25 },
26 polygonEnd: function() {
27 var areaRing = +areaRingSum;
28 areaSum.add(areaRing < 0 ? tau + areaRing : areaRing);
29 this.lineStart = this.lineEnd = this.point = noop;
30 },
31 sphere: function() {
32 areaSum.add(tau);
33 }
34};
35
36function areaRingStart() {
37 areaStream.point = areaPointFirst;
38}
39
40function areaRingEnd() {
41 areaPoint(lambda00, phi00);
42}
43
44function areaPointFirst(lambda, phi) {
45 areaStream.point = areaPoint;
46 lambda00 = lambda, phi00 = phi;
47 lambda *= radians, phi *= radians;
48 lambda0 = lambda, cosPhi0 = cos(phi = phi / 2 + quarterPi), sinPhi0 = sin(phi);
49}
50
51function areaPoint(lambda, phi) {
52 lambda *= radians, phi *= radians;
53 phi = phi / 2 + quarterPi; // half the angular distance from south pole
54
55 // Spherical excess E for a spherical triangle with vertices: south pole,
56 // previous point, current point. Uses a formula derived from Cagnoli’s
57 // theorem. See Todhunter, Spherical Trig. (1871), Sec. 103, Eq. (2).
58 var dLambda = lambda - lambda0,
59 sdLambda = dLambda >= 0 ? 1 : -1,
60 adLambda = sdLambda * dLambda,
61 cosPhi = cos(phi),
62 sinPhi = sin(phi),
63 k = sinPhi0 * sinPhi,
64 u = cosPhi0 * cosPhi + k * cos(adLambda),
65 v = k * sdLambda * sin(adLambda);
66 areaRingSum.add(atan2(v, u));
67
68 // Advance the previous points.
69 lambda0 = lambda, cosPhi0 = cosPhi, sinPhi0 = sinPhi;
70}
71
72export default function(object) {
73 areaSum = new Adder();
74 stream(object, areaStream);
75 return areaSum * 2;
76}
Note: See TracBrowser for help on using the repository browser.