source: node_modules/d3-polygon/dist/d3-polygon.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: 4.0 KB
Line 
1// https://d3js.org/d3-polygon/ v3.0.1 Copyright 2010-2021 Mike Bostock
2(function (global, factory) {
3typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
4typeof define === 'function' && define.amd ? define(['exports'], factory) :
5(global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.d3 = global.d3 || {}));
6}(this, (function (exports) { 'use strict';
7
8function area(polygon) {
9 var i = -1,
10 n = polygon.length,
11 a,
12 b = polygon[n - 1],
13 area = 0;
14
15 while (++i < n) {
16 a = b;
17 b = polygon[i];
18 area += a[1] * b[0] - a[0] * b[1];
19 }
20
21 return area / 2;
22}
23
24function centroid(polygon) {
25 var i = -1,
26 n = polygon.length,
27 x = 0,
28 y = 0,
29 a,
30 b = polygon[n - 1],
31 c,
32 k = 0;
33
34 while (++i < n) {
35 a = b;
36 b = polygon[i];
37 k += c = a[0] * b[1] - b[0] * a[1];
38 x += (a[0] + b[0]) * c;
39 y += (a[1] + b[1]) * c;
40 }
41
42 return k *= 3, [x / k, y / k];
43}
44
45// Returns the 2D cross product of AB and AC vectors, i.e., the z-component of
46// the 3D cross product in a quadrant I Cartesian coordinate system (+x is
47// right, +y is up). Returns a positive value if ABC is counter-clockwise,
48// negative if clockwise, and zero if the points are collinear.
49function cross(a, b, c) {
50 return (b[0] - a[0]) * (c[1] - a[1]) - (b[1] - a[1]) * (c[0] - a[0]);
51}
52
53function lexicographicOrder(a, b) {
54 return a[0] - b[0] || a[1] - b[1];
55}
56
57// Computes the upper convex hull per the monotone chain algorithm.
58// Assumes points.length >= 3, is sorted by x, unique in y.
59// Returns an array of indices into points in left-to-right order.
60function computeUpperHullIndexes(points) {
61 const n = points.length,
62 indexes = [0, 1];
63 let size = 2, i;
64
65 for (i = 2; i < n; ++i) {
66 while (size > 1 && cross(points[indexes[size - 2]], points[indexes[size - 1]], points[i]) <= 0) --size;
67 indexes[size++] = i;
68 }
69
70 return indexes.slice(0, size); // remove popped points
71}
72
73function hull(points) {
74 if ((n = points.length) < 3) return null;
75
76 var i,
77 n,
78 sortedPoints = new Array(n),
79 flippedPoints = new Array(n);
80
81 for (i = 0; i < n; ++i) sortedPoints[i] = [+points[i][0], +points[i][1], i];
82 sortedPoints.sort(lexicographicOrder);
83 for (i = 0; i < n; ++i) flippedPoints[i] = [sortedPoints[i][0], -sortedPoints[i][1]];
84
85 var upperIndexes = computeUpperHullIndexes(sortedPoints),
86 lowerIndexes = computeUpperHullIndexes(flippedPoints);
87
88 // Construct the hull polygon, removing possible duplicate endpoints.
89 var skipLeft = lowerIndexes[0] === upperIndexes[0],
90 skipRight = lowerIndexes[lowerIndexes.length - 1] === upperIndexes[upperIndexes.length - 1],
91 hull = [];
92
93 // Add upper hull in right-to-l order.
94 // Then add lower hull in left-to-right order.
95 for (i = upperIndexes.length - 1; i >= 0; --i) hull.push(points[sortedPoints[upperIndexes[i]][2]]);
96 for (i = +skipLeft; i < lowerIndexes.length - skipRight; ++i) hull.push(points[sortedPoints[lowerIndexes[i]][2]]);
97
98 return hull;
99}
100
101function contains(polygon, point) {
102 var n = polygon.length,
103 p = polygon[n - 1],
104 x = point[0], y = point[1],
105 x0 = p[0], y0 = p[1],
106 x1, y1,
107 inside = false;
108
109 for (var i = 0; i < n; ++i) {
110 p = polygon[i], x1 = p[0], y1 = p[1];
111 if (((y1 > y) !== (y0 > y)) && (x < (x0 - x1) * (y - y1) / (y0 - y1) + x1)) inside = !inside;
112 x0 = x1, y0 = y1;
113 }
114
115 return inside;
116}
117
118function length(polygon) {
119 var i = -1,
120 n = polygon.length,
121 b = polygon[n - 1],
122 xa,
123 ya,
124 xb = b[0],
125 yb = b[1],
126 perimeter = 0;
127
128 while (++i < n) {
129 xa = xb;
130 ya = yb;
131 b = polygon[i];
132 xb = b[0];
133 yb = b[1];
134 xa -= xb;
135 ya -= yb;
136 perimeter += Math.hypot(xa, ya);
137 }
138
139 return perimeter;
140}
141
142exports.polygonArea = area;
143exports.polygonCentroid = centroid;
144exports.polygonContains = contains;
145exports.polygonHull = hull;
146exports.polygonLength = length;
147
148Object.defineProperty(exports, '__esModule', { value: true });
149
150})));
Note: See TracBrowser for help on using the repository browser.