source: node_modules/d3-geo/src/length.js@ e4c61dd

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

Prototype 1.1

  • Property mode set to 100644
File size: 1.3 KB
Line 
1import {Adder} from "d3-array";
2import {abs, atan2, cos, radians, sin, sqrt} from "./math.js";
3import noop from "./noop.js";
4import stream from "./stream.js";
5
6var lengthSum,
7 lambda0,
8 sinPhi0,
9 cosPhi0;
10
11var lengthStream = {
12 sphere: noop,
13 point: noop,
14 lineStart: lengthLineStart,
15 lineEnd: noop,
16 polygonStart: noop,
17 polygonEnd: noop
18};
19
20function lengthLineStart() {
21 lengthStream.point = lengthPointFirst;
22 lengthStream.lineEnd = lengthLineEnd;
23}
24
25function lengthLineEnd() {
26 lengthStream.point = lengthStream.lineEnd = noop;
27}
28
29function lengthPointFirst(lambda, phi) {
30 lambda *= radians, phi *= radians;
31 lambda0 = lambda, sinPhi0 = sin(phi), cosPhi0 = cos(phi);
32 lengthStream.point = lengthPoint;
33}
34
35function lengthPoint(lambda, phi) {
36 lambda *= radians, phi *= radians;
37 var sinPhi = sin(phi),
38 cosPhi = cos(phi),
39 delta = abs(lambda - lambda0),
40 cosDelta = cos(delta),
41 sinDelta = sin(delta),
42 x = cosPhi * sinDelta,
43 y = cosPhi0 * sinPhi - sinPhi0 * cosPhi * cosDelta,
44 z = sinPhi0 * sinPhi + cosPhi0 * cosPhi * cosDelta;
45 lengthSum.add(atan2(sqrt(x * x + y * y), z));
46 lambda0 = lambda, sinPhi0 = sinPhi, cosPhi0 = cosPhi;
47}
48
49export default function(object) {
50 lengthSum = new Adder();
51 stream(object, lengthStream);
52 return +lengthSum;
53}
Note: See TracBrowser for help on using the repository browser.