@wemap/maths 2.7.7 → 3.1.11

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/index.js CHANGED
@@ -1,6 +1,8 @@
1
1
  import Matrix from './src/Matrix';
2
2
  import Matrix3 from './src/Matrix3';
3
3
  import Matrix4 from './src/Matrix4';
4
+ import Point2 from './src/Point2';
5
+ import Polygon from './src/Polygon';
4
6
  import Quaternion from './src/Quaternion';
5
7
  import Rotations from './src/Rotations';
6
8
  import Utils from './src/Utils';
@@ -19,6 +21,8 @@ export {
19
21
  Matrix,
20
22
  Matrix3,
21
23
  Matrix4,
24
+ Point2,
25
+ Polygon,
22
26
  Quaternion,
23
27
  Rotations,
24
28
  Utils,
package/package.json CHANGED
@@ -12,7 +12,7 @@
12
12
  "directory": "packages/maths"
13
13
  },
14
14
  "name": "@wemap/maths",
15
- "version": "2.7.7",
15
+ "version": "3.1.11",
16
16
  "bugs": {
17
17
  "url": "https://github.com/wemap/wemap-modules-js/issues"
18
18
  },
@@ -26,5 +26,5 @@
26
26
  "wemap"
27
27
  ],
28
28
  "license": "ISC",
29
- "gitHead": "988e0675759932cfc6b64df7768c6e6e573df1d9"
29
+ "gitHead": "1e72b637bcc6e4d378bdb7c93870228636bdcb35"
30
30
  }
package/src/Point2.js ADDED
@@ -0,0 +1,52 @@
1
+ class Point2 {
2
+
3
+ /**
4
+ * Distance from a point to a segment
5
+ * @param {Number[2]} p point
6
+ * @param {Number[2]} v segment point 1
7
+ * @param {Number[2]} w segment point 2
8
+ */
9
+ static distanceToSegment(p, v, w) {
10
+ const A = p[0] - v[0];
11
+ const B = p[1] - v[1];
12
+ const C = w[0] - v[0];
13
+ const D = w[1] - v[1];
14
+
15
+ const dot = A * C + B * D;
16
+ const lenSq = C * C + D * D;
17
+ let param = -1;
18
+ if (lenSq !== 0) {
19
+ param = dot / lenSq;
20
+ }
21
+
22
+ let xx;
23
+ let yy;
24
+
25
+ if (param < 0) {
26
+ xx = v[0];
27
+ yy = v[1];
28
+ } else if (param > 1) {
29
+ xx = w[0];
30
+ yy = w[1];
31
+ } else {
32
+ xx = v[0] + param * C;
33
+ yy = v[1] + param * D;
34
+ }
35
+
36
+ const dx = p[0] - xx;
37
+ const dy = p[1] - yy;
38
+ return Math.sqrt(dx * dx + dy * dy);
39
+ }
40
+
41
+ /**
42
+ * Distance between two points
43
+ * @param {Number[2]} p1
44
+ * @param {Number[2]} p2
45
+ */
46
+ static distanceToPoint(p1, p2) {
47
+ return Math.sqrt(((p1[0] - p2[0]) ** 2) + ((p1[1] - p2[1]) ** 2));
48
+ }
49
+
50
+ }
51
+
52
+ export default Point2;
package/src/Polygon.js ADDED
@@ -0,0 +1,62 @@
1
+ import Point2 from './Point2';
2
+
3
+ class Polygon {
4
+
5
+ // https://github.com/substack/point-in-polygon/blob/master/index.js
6
+ static isPointInside(polygon, point) {
7
+
8
+ const x = point[0], y = point[1];
9
+
10
+ let inside = false;
11
+ for (let i = 0, j = polygon.length - 1; i < polygon.length; j = i++) {
12
+ const xi = polygon[i][0], yi = polygon[i][1];
13
+ const xj = polygon[j][0], yj = polygon[j][1];
14
+
15
+ const intersect = ((yi > y) !== (yj > y))
16
+ && (x < (xj - xi) * (y - yi) / (yj - yi) + xi);
17
+ if (intersect) {
18
+ inside = !inside;
19
+ }
20
+ }
21
+
22
+ return inside;
23
+ }
24
+
25
+ static distanceToOtherRectangle(poly1, poly2) {
26
+
27
+ let minDist = Number.MAX_VALUE;
28
+
29
+ for (const point of poly1) {
30
+ if (Polygon.isPointInside(poly2, point)) {
31
+ return 0;
32
+ }
33
+ minDist = Math.min(minDist, Polygon.distanceToPoint(poly2, point));
34
+ }
35
+
36
+ for (const point of poly2) {
37
+ if (Polygon.isPointInside(poly1, point)) {
38
+ return 0;
39
+ }
40
+ minDist = Math.min(minDist, Polygon.distanceToPoint(poly1, point));
41
+ }
42
+
43
+ return minDist;
44
+ }
45
+
46
+ static distanceToPoint(polygon, point) {
47
+
48
+ let minDist = Number.MAX_VALUE;
49
+
50
+ polygon.forEach((segPoint1, idx) => {
51
+ const segPoint2 = polygon[(idx + 1) % polygon.length];
52
+ minDist = Math.min(
53
+ minDist,
54
+ Point2.distanceToSegment(point, segPoint1, segPoint2)
55
+ );
56
+ });
57
+
58
+ return minDist;
59
+ }
60
+ }
61
+
62
+ export default Polygon;
package/src/Rotations.js CHANGED
@@ -88,7 +88,7 @@ class Rotations {
88
88
 
89
89
 
90
90
  static getHeadingFromQuaternion(quaternion) {
91
- // Minus before "yaw" angle is here because ENU attitude is counter-clockwise whereas WGS84 is clockwise.
91
+ // Minus before "yaw" angle is here because ENU attitude is counter-clockwise whereas Coordinates is clockwise.
92
92
 
93
93
  const [qw, qx, qy, qz] = quaternion;
94
94
  if (Math.asin(2 * qy * qz + 2 * qx * qw) < Math.PI / 4) {