source: node_modules/d3-geo/src/path/centroid.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: 2.0 KB
RevLine 
[e4c61dd]1import {sqrt} from "../math.js";
2
3// TODO Enforce positive area for exterior, negative area for interior?
4
5var X0 = 0,
6 Y0 = 0,
7 Z0 = 0,
8 X1 = 0,
9 Y1 = 0,
10 Z1 = 0,
11 X2 = 0,
12 Y2 = 0,
13 Z2 = 0,
14 x00,
15 y00,
16 x0,
17 y0;
18
19var centroidStream = {
20 point: centroidPoint,
21 lineStart: centroidLineStart,
22 lineEnd: centroidLineEnd,
23 polygonStart: function() {
24 centroidStream.lineStart = centroidRingStart;
25 centroidStream.lineEnd = centroidRingEnd;
26 },
27 polygonEnd: function() {
28 centroidStream.point = centroidPoint;
29 centroidStream.lineStart = centroidLineStart;
30 centroidStream.lineEnd = centroidLineEnd;
31 },
32 result: function() {
33 var centroid = Z2 ? [X2 / Z2, Y2 / Z2]
34 : Z1 ? [X1 / Z1, Y1 / Z1]
35 : Z0 ? [X0 / Z0, Y0 / Z0]
36 : [NaN, NaN];
37 X0 = Y0 = Z0 =
38 X1 = Y1 = Z1 =
39 X2 = Y2 = Z2 = 0;
40 return centroid;
41 }
42};
43
44function centroidPoint(x, y) {
45 X0 += x;
46 Y0 += y;
47 ++Z0;
48}
49
50function centroidLineStart() {
51 centroidStream.point = centroidPointFirstLine;
52}
53
54function centroidPointFirstLine(x, y) {
55 centroidStream.point = centroidPointLine;
56 centroidPoint(x0 = x, y0 = y);
57}
58
59function centroidPointLine(x, y) {
60 var dx = x - x0, dy = y - y0, z = sqrt(dx * dx + dy * dy);
61 X1 += z * (x0 + x) / 2;
62 Y1 += z * (y0 + y) / 2;
63 Z1 += z;
64 centroidPoint(x0 = x, y0 = y);
65}
66
67function centroidLineEnd() {
68 centroidStream.point = centroidPoint;
69}
70
71function centroidRingStart() {
72 centroidStream.point = centroidPointFirstRing;
73}
74
75function centroidRingEnd() {
76 centroidPointRing(x00, y00);
77}
78
79function centroidPointFirstRing(x, y) {
80 centroidStream.point = centroidPointRing;
81 centroidPoint(x00 = x0 = x, y00 = y0 = y);
82}
83
84function centroidPointRing(x, y) {
85 var dx = x - x0,
86 dy = y - y0,
87 z = sqrt(dx * dx + dy * dy);
88
89 X1 += z * (x0 + x) / 2;
90 Y1 += z * (y0 + y) / 2;
91 Z1 += z;
92
93 z = y0 * x - x0 * y;
94 X2 += z * (x0 + x);
95 Y2 += z * (y0 + y);
96 Z2 += z * 3;
97 centroidPoint(x0 = x, y0 = y);
98}
99
100export default centroidStream;
Note: See TracBrowser for help on using the repository browser.