@expofp/geometry 0.0.0-experimental.2632d13 → 0.0.0-experimental.3b0db16
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/README.md +3 -1
- package/dist/lib/geo.d.ts +0 -14
- package/dist/lib/geo.js +0 -45
- package/dist/lib/intersections.d.ts +16 -4
- package/dist/lib/intersections.js +19 -5
- package/dist/lib/polygon.d.ts +6 -15
- package/dist/lib/polygon.js +17 -72
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -32,7 +32,9 @@ areas, and `containsPoint` ignore `z`/`elevation`. **Intersection** tests (`line
|
|
|
32
32
|
`intersectLineRect`) compare `elevation` only when **both** operands define it (an `undefined` elevation —
|
|
33
33
|
a `Box`, or a bare `LineLike` — means "any height" and always intersects); when both are defined they
|
|
34
34
|
intersect only within an `elevationTolerance` (default `1e-3`), and the result point's `z` is the shared
|
|
35
|
-
elevation.
|
|
35
|
+
elevation. Two planar helpers skip elevation entirely: `ringContainsPoint` (even-odd test against a
|
|
36
|
+
simple ring, concave allowed) and `segmentsCross` (strict interior crossing — endpoint touches and
|
|
37
|
+
collinear overlaps do not count).
|
|
36
38
|
|
|
37
39
|
## Design decisions
|
|
38
40
|
|
package/dist/lib/geo.d.ts
CHANGED
|
@@ -63,20 +63,6 @@ export declare function bearing(from: LatLng, to: LatLng): number;
|
|
|
63
63
|
* @returns the destination {@link LatLng}
|
|
64
64
|
*/
|
|
65
65
|
export declare function destinationPoint(from: LatLng, distanceM: number, bearingDeg: number): LatLng;
|
|
66
|
-
/**
|
|
67
|
-
* Samples the great circle between two points as a polyline.
|
|
68
|
-
*
|
|
69
|
-
* The shortest path over the sphere is not a straight line in any flat
|
|
70
|
-
* projection, so a renderer that joins two far-apart points with one segment
|
|
71
|
-
* draws the wrong curve. Sampling the arc — spherical interpolation, equal
|
|
72
|
-
* angular steps — lets an ordinary polyline stand in for it; `segments` trades
|
|
73
|
-
* fidelity for vertex count, and the endpoints are always returned unchanged.
|
|
74
|
-
* @param from - start point (degrees)
|
|
75
|
-
* @param to - end point (degrees)
|
|
76
|
-
* @param segments - how many equal arcs to split the path into (minimum 1)
|
|
77
|
-
* @returns `segments + 1` points from `from` to `to` inclusive
|
|
78
|
-
*/
|
|
79
|
-
export declare function greatCirclePath(from: LatLng, to: LatLng, segments: number): LatLng[];
|
|
80
66
|
/**
|
|
81
67
|
* Projects a local 2D point to geographic coordinates using two anchor
|
|
82
68
|
* correspondences. The projection uses a polar-coordinate affine transform:
|
package/dist/lib/geo.js
CHANGED
|
@@ -78,51 +78,6 @@ export function destinationPoint(from, distanceM, bearingDeg) {
|
|
|
78
78
|
// radians, so `normalizeAngle` is exactly the longitude wrap.
|
|
79
79
|
return { lat: radToDeg(phi2), lng: radToDeg(normalizeAngle(lambda2)) };
|
|
80
80
|
}
|
|
81
|
-
/**
|
|
82
|
-
* Samples the great circle between two points as a polyline.
|
|
83
|
-
*
|
|
84
|
-
* The shortest path over the sphere is not a straight line in any flat
|
|
85
|
-
* projection, so a renderer that joins two far-apart points with one segment
|
|
86
|
-
* draws the wrong curve. Sampling the arc — spherical interpolation, equal
|
|
87
|
-
* angular steps — lets an ordinary polyline stand in for it; `segments` trades
|
|
88
|
-
* fidelity for vertex count, and the endpoints are always returned unchanged.
|
|
89
|
-
* @param from - start point (degrees)
|
|
90
|
-
* @param to - end point (degrees)
|
|
91
|
-
* @param segments - how many equal arcs to split the path into (minimum 1)
|
|
92
|
-
* @returns `segments + 1` points from `from` to `to` inclusive
|
|
93
|
-
*/
|
|
94
|
-
export function greatCirclePath(from, to, segments) {
|
|
95
|
-
const steps = Math.max(1, Math.floor(segments));
|
|
96
|
-
const phi1 = degToRad(from.lat);
|
|
97
|
-
const lambda1 = degToRad(from.lng);
|
|
98
|
-
const phi2 = degToRad(to.lat);
|
|
99
|
-
const lambda2 = degToRad(to.lng);
|
|
100
|
-
// Angular separation of the two points, from the same haversine that measures
|
|
101
|
-
// the distance — so the sampling and the reported length agree.
|
|
102
|
-
const delta = haversineDistance(from, to) / EARTH_RADIUS_METERS;
|
|
103
|
-
const sinDelta = Math.sin(delta);
|
|
104
|
-
const path = [from];
|
|
105
|
-
for (let step = 1; step < steps; step++) {
|
|
106
|
-
const fraction = step / steps;
|
|
107
|
-
// Coincident (or antipodal) points leave no arc to interpolate along; hold
|
|
108
|
-
// the start rather than dividing by a vanishing sine.
|
|
109
|
-
if (sinDelta === 0) {
|
|
110
|
-
path.push(from);
|
|
111
|
-
continue;
|
|
112
|
-
}
|
|
113
|
-
const a = Math.sin((1 - fraction) * delta) / sinDelta;
|
|
114
|
-
const b = Math.sin(fraction * delta) / sinDelta;
|
|
115
|
-
const x = a * Math.cos(phi1) * Math.cos(lambda1) + b * Math.cos(phi2) * Math.cos(lambda2);
|
|
116
|
-
const y = a * Math.cos(phi1) * Math.sin(lambda1) + b * Math.cos(phi2) * Math.sin(lambda2);
|
|
117
|
-
const z = a * Math.sin(phi1) + b * Math.sin(phi2);
|
|
118
|
-
path.push({
|
|
119
|
-
lat: radToDeg(Math.atan2(z, Math.sqrt(x * x + y * y))),
|
|
120
|
-
lng: radToDeg(Math.atan2(y, x)),
|
|
121
|
-
});
|
|
122
|
-
}
|
|
123
|
-
path.push(to);
|
|
124
|
-
return path;
|
|
125
|
-
}
|
|
126
81
|
// ---------------------------------------------------------------------------
|
|
127
82
|
// Planar ↔ geographic projection (two-anchor affine bridge)
|
|
128
83
|
// ---------------------------------------------------------------------------
|
|
@@ -3,19 +3,31 @@ import { type Point } from './point.js';
|
|
|
3
3
|
import { type RectLike } from './rect.js';
|
|
4
4
|
/**
|
|
5
5
|
* Computes the crossing of lines `a` and `b` using the parametric form. Returns the crossing
|
|
6
|
-
* point
|
|
7
|
-
*
|
|
8
|
-
*
|
|
6
|
+
* point, its parameters `t` along `a` and `u` along `b` (`0` = `p0`, `1` = `p1`), plus flags
|
|
7
|
+
* indicating whether the crossing lies within each segment's `[0, 1]` parameter range. Returns
|
|
8
|
+
* `null` only when the lines are parallel (denominator is zero) or when both lines have a defined
|
|
9
|
+
* elevation and they differ by more than `elevationTolerance`.
|
|
9
10
|
* @param a - first line segment, defined by `p0` and `p1`
|
|
10
11
|
* @param b - second line segment, defined by `p0` and `p1`
|
|
11
12
|
* @param elevationTolerance - maximum elevation difference for a crossing to be reported; defaults to {@link ELEVATION_TOLERANCE}
|
|
12
|
-
* @returns `{ point, onLine1, onLine2 }`, or `null` when the lines are parallel or elevation-incompatible
|
|
13
|
+
* @returns `{ point, t, u, onLine1, onLine2 }`, or `null` when the lines are parallel or elevation-incompatible
|
|
13
14
|
*/
|
|
14
15
|
export declare function lineIntersection(a: LineLike, b: LineLike, elevationTolerance?: number): {
|
|
15
16
|
point: Point;
|
|
17
|
+
t: number;
|
|
18
|
+
u: number;
|
|
16
19
|
onLine1: boolean;
|
|
17
20
|
onLine2: boolean;
|
|
18
21
|
} | null;
|
|
22
|
+
/**
|
|
23
|
+
* Whether `other` crosses the interior of `segment`, in plan: touching at an end of `segment`,
|
|
24
|
+
* running parallel or overlapping do not count.
|
|
25
|
+
* @param segment - the segment whose interior is tested
|
|
26
|
+
* @param other - the segment it may cross, endpoints included
|
|
27
|
+
* @param eps - endpoint tolerance along `segment`, as a fraction of its length (default `1e-6`)
|
|
28
|
+
* @returns true when the interior of `segment` crosses `other`
|
|
29
|
+
*/
|
|
30
|
+
export declare function segmentsCross(segment: LineLike, other: LineLike, eps?: number): boolean;
|
|
19
31
|
/**
|
|
20
32
|
* Returns the points where a line segment crosses a rect's edges. Works for any `RectLike`,
|
|
21
33
|
* including a rotated rect or an unrotated `Box` (a `Box` satisfies `RectLike` and is treated
|
|
@@ -2,13 +2,14 @@ import { ELEVATION_TOLERANCE, pointLerp } from './point.js';
|
|
|
2
2
|
import { rectCorners } from './rect.js';
|
|
3
3
|
/**
|
|
4
4
|
* Computes the crossing of lines `a` and `b` using the parametric form. Returns the crossing
|
|
5
|
-
* point
|
|
6
|
-
*
|
|
7
|
-
*
|
|
5
|
+
* point, its parameters `t` along `a` and `u` along `b` (`0` = `p0`, `1` = `p1`), plus flags
|
|
6
|
+
* indicating whether the crossing lies within each segment's `[0, 1]` parameter range. Returns
|
|
7
|
+
* `null` only when the lines are parallel (denominator is zero) or when both lines have a defined
|
|
8
|
+
* elevation and they differ by more than `elevationTolerance`.
|
|
8
9
|
* @param a - first line segment, defined by `p0` and `p1`
|
|
9
10
|
* @param b - second line segment, defined by `p0` and `p1`
|
|
10
11
|
* @param elevationTolerance - maximum elevation difference for a crossing to be reported; defaults to {@link ELEVATION_TOLERANCE}
|
|
11
|
-
* @returns `{ point, onLine1, onLine2 }`, or `null` when the lines are parallel or elevation-incompatible
|
|
12
|
+
* @returns `{ point, t, u, onLine1, onLine2 }`, or `null` when the lines are parallel or elevation-incompatible
|
|
12
13
|
*/
|
|
13
14
|
export function lineIntersection(a, b, elevationTolerance = ELEVATION_TOLERANCE) {
|
|
14
15
|
if (a.elevation != null &&
|
|
@@ -25,7 +26,20 @@ export function lineIntersection(a, b, elevationTolerance = ELEVATION_TOLERANCE)
|
|
|
25
26
|
const u = ((x1 - x3) * (y1 - y2) - (y1 - y3) * (x1 - x2)) / denom;
|
|
26
27
|
const point = pointLerp(a.p0, a.p1, t);
|
|
27
28
|
point.z = a.elevation ?? b.elevation ?? 0;
|
|
28
|
-
return { point, onLine1: t >= 0 && t <= 1, onLine2: u >= 0 && u <= 1 };
|
|
29
|
+
return { point, t, u, onLine1: t >= 0 && t <= 1, onLine2: u >= 0 && u <= 1 };
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* Whether `other` crosses the interior of `segment`, in plan: touching at an end of `segment`,
|
|
33
|
+
* running parallel or overlapping do not count.
|
|
34
|
+
* @param segment - the segment whose interior is tested
|
|
35
|
+
* @param other - the segment it may cross, endpoints included
|
|
36
|
+
* @param eps - endpoint tolerance along `segment`, as a fraction of its length (default `1e-6`)
|
|
37
|
+
* @returns true when the interior of `segment` crosses `other`
|
|
38
|
+
*/
|
|
39
|
+
export function segmentsCross(segment, other, eps = 1e-6) {
|
|
40
|
+
// An infinite tolerance ignores elevation without copying the segments.
|
|
41
|
+
const hit = lineIntersection(segment, other, Infinity);
|
|
42
|
+
return !!hit && hit.onLine2 && hit.t > eps && hit.t < 1 - eps;
|
|
29
43
|
}
|
|
30
44
|
/**
|
|
31
45
|
* Returns the intersection point of two line segments `a` and `b`, or `null` when the segments
|
package/dist/lib/polygon.d.ts
CHANGED
|
@@ -167,20 +167,11 @@ export declare function polygonContainsPoint(p: PolygonLike, point: Point2Like,
|
|
|
167
167
|
areaTolerance?: number;
|
|
168
168
|
}): boolean;
|
|
169
169
|
/**
|
|
170
|
-
*
|
|
171
|
-
*
|
|
172
|
-
* @param
|
|
173
|
-
* @
|
|
174
|
-
*
|
|
175
|
-
* (collinear inputs reduce to their 2 extreme points).
|
|
176
|
-
*/
|
|
177
|
-
export declare function convexHull(points: readonly Point2Like[]): Point2Like[];
|
|
178
|
-
/**
|
|
179
|
-
* Area centroid of a simple polygon ring (shoelace formula). Vertices may be closed or open
|
|
180
|
-
* (a repeated last vertex is handled by the wrap-around).
|
|
181
|
-
* @param vertices - polygon ring vertices in order (either winding)
|
|
182
|
-
* @returns the area centroid; degenerate rings (fewer than 3 vertices or zero area) fall back
|
|
183
|
-
* to the vertex average, and an empty input returns `{ x: 0, y: 0 }`.
|
|
170
|
+
* Even-odd test: whether `point` is inside `ring`, closed implicitly. A point on the boundary may go
|
|
171
|
+
* either way.
|
|
172
|
+
* @param ring - ordered ring vertices (CW or CCW), closed implicitly
|
|
173
|
+
* @param point - the point to test
|
|
174
|
+
* @returns true when `point` is inside the ring
|
|
184
175
|
*/
|
|
185
|
-
export declare function
|
|
176
|
+
export declare function ringContainsPoint(ring: readonly Point2Like[], point: Point2Like): boolean;
|
|
186
177
|
//# sourceMappingURL=polygon.d.ts.map
|
package/dist/lib/polygon.js
CHANGED
|
@@ -219,78 +219,23 @@ export function polygonContainsPoint(p, point, opts) {
|
|
|
219
219
|
return false;
|
|
220
220
|
}
|
|
221
221
|
/**
|
|
222
|
-
*
|
|
223
|
-
*
|
|
224
|
-
* @param
|
|
225
|
-
* @
|
|
226
|
-
*
|
|
227
|
-
* (collinear inputs reduce to their 2 extreme points).
|
|
228
|
-
*/
|
|
229
|
-
export function convexHull(points) {
|
|
230
|
-
const seen = new Set();
|
|
231
|
-
const pts = [];
|
|
232
|
-
for (const p of points) {
|
|
233
|
-
const key = `${p.x},${p.y}`;
|
|
234
|
-
if (!seen.has(key)) {
|
|
235
|
-
seen.add(key);
|
|
236
|
-
pts.push({ x: p.x, y: p.y });
|
|
237
|
-
}
|
|
238
|
-
}
|
|
239
|
-
if (pts.length <= 2)
|
|
240
|
-
return pts;
|
|
241
|
-
pts.sort((a, b) => a.x - b.x || a.y - b.y);
|
|
242
|
-
const cross = (o, a, b) => (a.x - o.x) * (b.y - o.y) - (a.y - o.y) * (b.x - o.x);
|
|
243
|
-
const lower = [];
|
|
244
|
-
for (const p of pts) {
|
|
245
|
-
while (lower.length >= 2 && cross(lower[lower.length - 2], lower[lower.length - 1], p) <= 0) {
|
|
246
|
-
lower.pop();
|
|
247
|
-
}
|
|
248
|
-
lower.push(p);
|
|
249
|
-
}
|
|
250
|
-
const upper = [];
|
|
251
|
-
for (let i = pts.length - 1; i >= 0; i--) {
|
|
252
|
-
const p = pts[i];
|
|
253
|
-
while (upper.length >= 2 && cross(upper[upper.length - 2], upper[upper.length - 1], p) <= 0) {
|
|
254
|
-
upper.pop();
|
|
255
|
-
}
|
|
256
|
-
upper.push(p);
|
|
257
|
-
}
|
|
258
|
-
lower.pop();
|
|
259
|
-
upper.pop();
|
|
260
|
-
const hull = lower.concat(upper);
|
|
261
|
-
// Fully collinear input: both chains reduce to the same 2 extremes.
|
|
262
|
-
return hull.length >= 3 ? hull : [pts[0], pts[pts.length - 1]];
|
|
263
|
-
}
|
|
264
|
-
/**
|
|
265
|
-
* Area centroid of a simple polygon ring (shoelace formula). Vertices may be closed or open
|
|
266
|
-
* (a repeated last vertex is handled by the wrap-around).
|
|
267
|
-
* @param vertices - polygon ring vertices in order (either winding)
|
|
268
|
-
* @returns the area centroid; degenerate rings (fewer than 3 vertices or zero area) fall back
|
|
269
|
-
* to the vertex average, and an empty input returns `{ x: 0, y: 0 }`.
|
|
222
|
+
* Even-odd test: whether `point` is inside `ring`, closed implicitly. A point on the boundary may go
|
|
223
|
+
* either way.
|
|
224
|
+
* @param ring - ordered ring vertices (CW or CCW), closed implicitly
|
|
225
|
+
* @param point - the point to test
|
|
226
|
+
* @returns true when `point` is inside the ring
|
|
270
227
|
*/
|
|
271
|
-
export function
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
const
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
cx += (a.x + b.x) * crossTerm;
|
|
283
|
-
cy += (a.y + b.y) * crossTerm;
|
|
284
|
-
}
|
|
285
|
-
signedArea /= 2;
|
|
286
|
-
if (signedArea === 0) {
|
|
287
|
-
let sx = 0;
|
|
288
|
-
let sy = 0;
|
|
289
|
-
for (const v of vertices) {
|
|
290
|
-
sx += v.x;
|
|
291
|
-
sy += v.y;
|
|
292
|
-
}
|
|
293
|
-
return { x: sx / vertices.length, y: sy / vertices.length };
|
|
228
|
+
export function ringContainsPoint(ring, point) {
|
|
229
|
+
let inside = false;
|
|
230
|
+
for (let i = 0, j = ring.length - 1; i < ring.length; j = i++) {
|
|
231
|
+
const current = ring[i];
|
|
232
|
+
const previous = ring[j];
|
|
233
|
+
const straddles = current.y > point.y !== previous.y > point.y;
|
|
234
|
+
if (!straddles)
|
|
235
|
+
continue;
|
|
236
|
+
const crossingX = ((previous.x - current.x) * (point.y - current.y)) / (previous.y - current.y) + current.x;
|
|
237
|
+
if (point.x < crossingX)
|
|
238
|
+
inside = !inside;
|
|
294
239
|
}
|
|
295
|
-
return
|
|
240
|
+
return inside;
|
|
296
241
|
}
|