@gitborlando/geo 4.1.0 → 5.1.0

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.
@@ -1,156 +0,0 @@
1
- export type Point = {
2
- x: number
3
- y: number
4
- }
5
-
6
- // distance between 2 points
7
- function distance(p1: Point, p2: Point): number {
8
- return Math.sqrt(distanceSq(p1, p2))
9
- }
10
-
11
- // distance between 2 points squared
12
- function distanceSq(p1: Point, p2: Point): number {
13
- return Math.pow(p1.x - p2.x, 2) + Math.pow(p1.y - p2.y, 2)
14
- }
15
-
16
- // Sistance squared from a point p to the line segment vw
17
- function distanceToSegmentSq(p: Point, v: Point, w: Point): number {
18
- const l2 = distanceSq(v, w)
19
- if (l2 === 0) {
20
- return distanceSq(p, v)
21
- }
22
- let t = ((p.x - v.x) * (w.x - v.x) + (p.y - v.y) * (w.y - v.y)) / l2
23
- t = Math.max(0, Math.min(1, t))
24
- return distanceSq(p, lerp(v, w, t))
25
- }
26
-
27
- function lerp(a: Point, b: Point, t: number): Point {
28
- return { x: a.x + (b.x - a.x) * t, y: a.y + (b.y - a.y) * t }
29
- }
30
-
31
- // Adapted from https://seant23.wordpress.com/2010/11/12/offset-bezier-curves/
32
- function flatness(points: readonly Point[], offset: number): number {
33
- const p1 = points[offset + 0]
34
- const p2 = points[offset + 1]
35
- const p3 = points[offset + 2]
36
- const p4 = points[offset + 3]
37
-
38
- let ux = 3 * p2.x - 2 * p1.x - p4.x
39
- ux *= ux
40
- let uy = 3 * p2.y - 2 * p1.y - p4.y
41
- uy *= uy
42
- let vx = 3 * p3.x - 2 * p4.x - p1.x
43
- vx *= vx
44
- let vy = 3 * p3.y - 2 * p4.y - p1.y
45
- vy *= vy
46
-
47
- if (ux < vx) {
48
- ux = vx
49
- }
50
-
51
- if (uy < vy) {
52
- uy = vy
53
- }
54
-
55
- return ux + uy
56
- }
57
-
58
- function getPointsOnBezierCurveWithSplitting(
59
- points: readonly Point[],
60
- offset: number,
61
- tolerance: number,
62
- newPoints?: Point[],
63
- ): Point[] {
64
- const outPoints = newPoints || []
65
- if (flatness(points, offset) < tolerance) {
66
- const p0 = points[offset + 0]
67
- if (outPoints.length) {
68
- const d = distance(outPoints[outPoints.length - 1], p0)
69
- if (d > 1) {
70
- outPoints.push(p0)
71
- }
72
- } else {
73
- outPoints.push(p0)
74
- }
75
- outPoints.push(points[offset + 3])
76
- } else {
77
- // subdivide
78
- const t = 0.5
79
- const p1 = points[offset + 0]
80
- const p2 = points[offset + 1]
81
- const p3 = points[offset + 2]
82
- const p4 = points[offset + 3]
83
-
84
- const q1 = lerp(p1, p2, t)
85
- const q2 = lerp(p2, p3, t)
86
- const q3 = lerp(p3, p4, t)
87
-
88
- const r1 = lerp(q1, q2, t)
89
- const r2 = lerp(q2, q3, t)
90
-
91
- const red = lerp(r1, r2, t)
92
-
93
- getPointsOnBezierCurveWithSplitting([p1, q1, r1, red], 0, tolerance, outPoints)
94
- getPointsOnBezierCurveWithSplitting([red, r2, q3, p4], 0, tolerance, outPoints)
95
- }
96
- return outPoints
97
- }
98
-
99
- export function simplify(points: readonly Point[], distance: number): Point[] {
100
- return simplifyPoints(points, 0, points.length, distance)
101
- }
102
-
103
- // Ramer–Douglas–Peucker algorithm
104
- // https://en.wikipedia.org/wiki/Ramer%E2%80%93Douglas%E2%80%93Peucker_algorithm
105
- export function simplifyPoints(
106
- points: readonly Point[],
107
- start: number,
108
- end: number,
109
- epsilon: number,
110
- newPoints?: Point[],
111
- ): Point[] {
112
- const outPoints = newPoints || []
113
-
114
- // find the most distance point from the endpoints
115
- const s = points[start]
116
- const e = points[end - 1]
117
- let maxDistSq = 0
118
- let maxNdx = 1
119
- for (let i = start + 1; i < end - 1; ++i) {
120
- const distSq = distanceToSegmentSq(points[i], s, e)
121
- if (distSq > maxDistSq) {
122
- maxDistSq = distSq
123
- maxNdx = i
124
- }
125
- }
126
-
127
- // if that point is too far, split
128
- if (Math.sqrt(maxDistSq) > epsilon) {
129
- simplifyPoints(points, start, maxNdx + 1, epsilon, outPoints)
130
- simplifyPoints(points, maxNdx, end, epsilon, outPoints)
131
- } else {
132
- if (!outPoints.length) {
133
- outPoints.push(s)
134
- }
135
- outPoints.push(e)
136
- }
137
-
138
- return outPoints
139
- }
140
-
141
- export function pointsOnBezierCurves(
142
- points: readonly Point[],
143
- tolerance: number = 0.15,
144
- distance?: number,
145
- ): Point[] {
146
- const newPoints: Point[] = []
147
- const numSegments = (points.length - 1) / 3
148
- for (let i = 0; i < numSegments; i++) {
149
- const offset = i * 3
150
- getPointsOnBezierCurveWithSplitting(points, offset, tolerance, newPoints)
151
- }
152
- if (distance && distance > 0) {
153
- return simplifyPoints(newPoints, 0, newPoints.length, distance)
154
- }
155
- return newPoints
156
- }
package/tsconfig.json DELETED
@@ -1,31 +0,0 @@
1
- {
2
- "compilerOptions": {
3
- "baseUrl": "./",
4
- "target": "ESNext",
5
- "useDefineForClassFields": true,
6
- "lib": ["ESNext", "DOM", "DOM.Iterable"],
7
- "module": "ESNext",
8
- "skipLibCheck": true,
9
- /* Bundler mode */
10
- "moduleResolution": "bundler",
11
- "allowImportingTsExtensions": true,
12
- "experimentalDecorators": true,
13
- "emitDecoratorMetadata": true,
14
- "isolatedModules": true,
15
- "moduleDetection": "force",
16
- "noEmit": true,
17
- "jsx": "react-jsx",
18
- /* Linting */
19
- "strict": true,
20
- "noUnusedLocals": false,
21
- "noUnusedParameters": false,
22
- "noFallthroughCasesInSwitch": false
23
- },
24
- "include": [
25
- "packages",
26
- "src",
27
- "tsup.config.ts",
28
- "vitest.config.ts",
29
- "src/__test__"
30
- ]
31
- }
package/tsup.config.ts DELETED
@@ -1,10 +0,0 @@
1
- import { defineConfig } from 'tsup'
2
-
3
- export default defineConfig({
4
- entry: ['src/index.ts'],
5
- outDir: 'dist',
6
- format: ['esm'],
7
- dts: true,
8
- splitting: true,
9
- clean: true,
10
- })
package/vitest.config.ts DELETED
@@ -1,8 +0,0 @@
1
- import { defineConfig } from 'vitest/config'
2
-
3
- export default defineConfig({
4
- test: {
5
- environment: 'node',
6
- globals: true,
7
- },
8
- })