@js-draw/math 1.16.0 → 1.18.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.
- package/dist/cjs/Mat33.js +6 -1
- package/dist/cjs/Vec3.d.ts +23 -1
- package/dist/cjs/Vec3.js +33 -7
- package/dist/cjs/lib.d.ts +2 -1
- package/dist/cjs/lib.js +5 -1
- package/dist/cjs/shapes/Abstract2DShape.d.ts +3 -0
- package/dist/cjs/shapes/BezierJSWrapper.d.ts +19 -5
- package/dist/cjs/shapes/BezierJSWrapper.js +170 -18
- package/dist/cjs/shapes/LineSegment2.d.ts +45 -5
- package/dist/cjs/shapes/LineSegment2.js +89 -11
- package/dist/cjs/shapes/Parameterized2DShape.d.ts +36 -0
- package/dist/cjs/shapes/Parameterized2DShape.js +20 -0
- package/dist/cjs/shapes/Path.d.ts +131 -13
- package/dist/cjs/shapes/Path.js +507 -26
- package/dist/cjs/shapes/PointShape2D.d.ts +14 -3
- package/dist/cjs/shapes/PointShape2D.js +28 -5
- package/dist/cjs/shapes/QuadraticBezier.d.ts +6 -3
- package/dist/cjs/shapes/QuadraticBezier.js +21 -7
- package/dist/cjs/shapes/Rect2.d.ts +9 -1
- package/dist/cjs/shapes/Rect2.js +9 -2
- package/dist/cjs/utils/convexHull2Of.d.ts +9 -0
- package/dist/cjs/utils/convexHull2Of.js +61 -0
- package/dist/cjs/utils/convexHull2Of.test.d.ts +1 -0
- package/dist/mjs/Mat33.mjs +6 -1
- package/dist/mjs/Vec3.d.ts +23 -1
- package/dist/mjs/Vec3.mjs +33 -7
- package/dist/mjs/lib.d.ts +2 -1
- package/dist/mjs/lib.mjs +2 -1
- package/dist/mjs/shapes/Abstract2DShape.d.ts +3 -0
- package/dist/mjs/shapes/BezierJSWrapper.d.ts +19 -5
- package/dist/mjs/shapes/BezierJSWrapper.mjs +168 -18
- package/dist/mjs/shapes/LineSegment2.d.ts +45 -5
- package/dist/mjs/shapes/LineSegment2.mjs +89 -11
- package/dist/mjs/shapes/Parameterized2DShape.d.ts +36 -0
- package/dist/mjs/shapes/Parameterized2DShape.mjs +13 -0
- package/dist/mjs/shapes/Path.d.ts +131 -13
- package/dist/mjs/shapes/Path.mjs +504 -25
- package/dist/mjs/shapes/PointShape2D.d.ts +14 -3
- package/dist/mjs/shapes/PointShape2D.mjs +28 -5
- package/dist/mjs/shapes/QuadraticBezier.d.ts +6 -3
- package/dist/mjs/shapes/QuadraticBezier.mjs +21 -7
- package/dist/mjs/shapes/Rect2.d.ts +9 -1
- package/dist/mjs/shapes/Rect2.mjs +9 -2
- package/dist/mjs/utils/convexHull2Of.d.ts +9 -0
- package/dist/mjs/utils/convexHull2Of.mjs +59 -0
- package/dist/mjs/utils/convexHull2Of.test.d.ts +1 -0
- package/package.json +5 -5
- package/src/Mat33.ts +8 -2
- package/src/Vec3.test.ts +42 -7
- package/src/Vec3.ts +37 -8
- package/src/lib.ts +5 -0
- package/src/shapes/Abstract2DShape.ts +3 -0
- package/src/shapes/BezierJSWrapper.ts +195 -14
- package/src/shapes/LineSegment2.test.ts +61 -1
- package/src/shapes/LineSegment2.ts +110 -12
- package/src/shapes/Parameterized2DShape.ts +44 -0
- package/src/shapes/Path.test.ts +233 -5
- package/src/shapes/Path.ts +593 -37
- package/src/shapes/PointShape2D.ts +33 -6
- package/src/shapes/QuadraticBezier.test.ts +69 -12
- package/src/shapes/QuadraticBezier.ts +25 -8
- package/src/shapes/Rect2.ts +10 -3
- package/src/utils/convexHull2Of.test.ts +43 -0
- package/src/utils/convexHull2Of.ts +71 -0
@@ -2,7 +2,7 @@ import LineSegment2 from './LineSegment2';
|
|
2
2
|
import Mat33 from '../Mat33';
|
3
3
|
import Rect2 from './Rect2';
|
4
4
|
import { Point2 } from '../Vec2';
|
5
|
-
import
|
5
|
+
import Parameterized2DShape from './Parameterized2DShape';
|
6
6
|
export declare enum PathCommandType {
|
7
7
|
LineTo = 0,
|
8
8
|
MoveTo = 1,
|
@@ -29,14 +29,68 @@ export interface MoveToPathCommand {
|
|
29
29
|
point: Point2;
|
30
30
|
}
|
31
31
|
export type PathCommand = CubicBezierPathCommand | QuadraticBezierPathCommand | MoveToPathCommand | LinePathCommand;
|
32
|
-
interface IntersectionResult {
|
33
|
-
curve:
|
34
|
-
|
35
|
-
|
32
|
+
export interface IntersectionResult {
|
33
|
+
curve: Parameterized2DShape;
|
34
|
+
curveIndex: number;
|
35
|
+
/** Parameter value for the closest point **on** the path to the intersection. @internal */
|
36
|
+
parameterValue: number;
|
37
|
+
/** Point at which the intersection occured. */
|
36
38
|
point: Point2;
|
37
39
|
}
|
40
|
+
/** Options for {@link Path.splitNear} and {@link Path.splitAt} */
|
41
|
+
export interface PathSplitOptions {
|
42
|
+
/**
|
43
|
+
* Allows mapping points on newly added segments. This is useful, for example,
|
44
|
+
* to round points to prevent long decimals when later saving.
|
45
|
+
*/
|
46
|
+
mapNewPoint?: (point: Point2) => Point2;
|
47
|
+
}
|
48
|
+
/**
|
49
|
+
* Allows indexing a particular part of a path.
|
50
|
+
*
|
51
|
+
* @see {@link Path.at} {@link Path.tangentAt}
|
52
|
+
*/
|
53
|
+
export interface CurveIndexRecord {
|
54
|
+
curveIndex: number;
|
55
|
+
parameterValue: number;
|
56
|
+
}
|
57
|
+
/** Returns a positive number if `a` comes after `b`, 0 if equal, and negative otherwise. */
|
58
|
+
export declare const compareCurveIndices: (a: CurveIndexRecord, b: CurveIndexRecord) => number;
|
59
|
+
/**
|
60
|
+
* Returns a version of `index` with its parameter value incremented by `stepBy`
|
61
|
+
* (which can be either positive or negative).
|
62
|
+
*/
|
63
|
+
export declare const stepCurveIndexBy: (index: CurveIndexRecord, stepBy: number) => CurveIndexRecord;
|
38
64
|
/**
|
39
65
|
* Represents a union of lines and curves.
|
66
|
+
*
|
67
|
+
* To create a path from a string, see {@link fromString}.
|
68
|
+
*
|
69
|
+
* @example
|
70
|
+
* ```ts,runnable,console
|
71
|
+
* import {Path, Mat33, Vec2, LineSegment2} from '@js-draw/math';
|
72
|
+
*
|
73
|
+
* // Creates a path from an SVG path string.
|
74
|
+
* // In this case,
|
75
|
+
* // 1. Move to (0,0)
|
76
|
+
* // 2. Line to (100,0)
|
77
|
+
* const path = Path.fromString('M0,0 L100,0');
|
78
|
+
*
|
79
|
+
* // Logs the distance from (10,0) to the curve 1 unit
|
80
|
+
* // away from path. This curve forms a stroke with the path at
|
81
|
+
* // its center.
|
82
|
+
* const strokeRadius = 1;
|
83
|
+
* console.log(path.signedDistance(Vec2.of(10,0), strokeRadius));
|
84
|
+
*
|
85
|
+
* // Log a version of the path that's scaled by a factor of 4.
|
86
|
+
* console.log(path.transformedBy(Mat33.scaling2D(4)).toString());
|
87
|
+
*
|
88
|
+
* // Log all intersections of a stroked version of the path with
|
89
|
+
* // a vertical line segment.
|
90
|
+
* // (Try removing the `strokeRadius` parameter).
|
91
|
+
* const segment = new LineSegment2(Vec2.of(5, -100), Vec2.of(5, 100));
|
92
|
+
* console.log(path.intersection(segment, strokeRadius).map(i => i.point));
|
93
|
+
* ```
|
40
94
|
*/
|
41
95
|
export declare class Path {
|
42
96
|
readonly startPoint: Point2;
|
@@ -55,9 +109,15 @@ export declare class Path {
|
|
55
109
|
* See also {@link fromString}
|
56
110
|
*/
|
57
111
|
constructor(startPoint: Point2, parts: Readonly<PathCommand>[]);
|
112
|
+
/**
|
113
|
+
* Computes and returns the full bounding box for this path.
|
114
|
+
*
|
115
|
+
* If a slight over-estimate of a path's bounding box is sufficient, use
|
116
|
+
* {@link bbox} instead.
|
117
|
+
*/
|
58
118
|
getExactBBox(): Rect2;
|
59
119
|
private cachedGeometry;
|
60
|
-
get geometry():
|
120
|
+
get geometry(): Parameterized2DShape[];
|
61
121
|
/**
|
62
122
|
* Iterates through the start/end points of each component in this path.
|
63
123
|
*
|
@@ -68,7 +128,20 @@ export declare class Path {
|
|
68
128
|
private cachedPolylineApproximation;
|
69
129
|
polylineApproximation(): LineSegment2[];
|
70
130
|
static computeBBoxForSegment(startPoint: Point2, part: PathCommand): Rect2;
|
71
|
-
/**
|
131
|
+
/**
|
132
|
+
* Returns the signed distance between `point` and a curve `strokeRadius` units
|
133
|
+
* away from this path.
|
134
|
+
*
|
135
|
+
* This returns the **signed distance**, which means that points inside this shape
|
136
|
+
* have their distance negated. For example,
|
137
|
+
* ```ts,runnable,console
|
138
|
+
* import {Path, Vec2} from '@js-draw/math';
|
139
|
+
* console.log(Path.fromString('m0,0 L100,0').signedDistance(Vec2.zero, 1));
|
140
|
+
* ```
|
141
|
+
* would print `-1` because (0,0) is on `m0,0 L100,0` and thus one unit away from its boundary.
|
142
|
+
*
|
143
|
+
* **Note**: `strokeRadius = strokeWidth / 2`
|
144
|
+
*/
|
72
145
|
signedDistance(point: Point2, strokeRadius: number): number;
|
73
146
|
/**
|
74
147
|
* Let `S` be a closed path a distance `strokeRadius` from this path.
|
@@ -86,11 +159,49 @@ export declare class Path {
|
|
86
159
|
* **Note**: `strokeRadius` is half of a stroke's width.
|
87
160
|
*/
|
88
161
|
intersection(line: LineSegment2, strokeRadius?: number): IntersectionResult[];
|
162
|
+
/**
|
163
|
+
* @returns the nearest point on this path to the given `point`.
|
164
|
+
*/
|
165
|
+
nearestPointTo(point: Point2): IntersectionResult;
|
166
|
+
at(index: CurveIndexRecord): import("../Vec3").Vec3;
|
167
|
+
tangentAt(index: CurveIndexRecord): import("../Vec3").Vec3;
|
168
|
+
/** Splits this path in two near the given `point`. */
|
169
|
+
splitNear(point: Point2, options?: PathSplitOptions): [Path] | [Path, Path];
|
170
|
+
/**
|
171
|
+
* Returns a copy of this path with `deleteFrom` until `deleteUntil` replaced with `insert`.
|
172
|
+
*
|
173
|
+
* This method is analogous to {@link Array.toSpliced}.
|
174
|
+
*/
|
175
|
+
spliced(deleteFrom: CurveIndexRecord, deleteTo: CurveIndexRecord, insert: Path | undefined, options?: PathSplitOptions): Path;
|
176
|
+
splitAt(at: CurveIndexRecord, options?: PathSplitOptions): [Path] | [Path, Path];
|
177
|
+
splitAt(at: CurveIndexRecord[], options?: PathSplitOptions): Path[];
|
178
|
+
/**
|
179
|
+
* Replaces all `MoveTo` commands with `LineTo` commands and connects the end point of this
|
180
|
+
* path to the start point.
|
181
|
+
*/
|
182
|
+
asClosed(): Path;
|
89
183
|
private static mapPathCommand;
|
90
184
|
mapPoints(mapping: (point: Point2) => Point2): Path;
|
91
185
|
transformedBy(affineTransfm: Mat33): Path;
|
92
|
-
|
93
|
-
|
186
|
+
/**
|
187
|
+
* @internal
|
188
|
+
*/
|
189
|
+
closedContainsPoint(point: Point2): boolean;
|
190
|
+
union(other: Path | PathCommand[] | null, options?: {
|
191
|
+
allowReverse?: boolean;
|
192
|
+
}): Path;
|
193
|
+
/**
|
194
|
+
* @returns a version of this path with the direction reversed.
|
195
|
+
*
|
196
|
+
* Example:
|
197
|
+
* ```ts,runnable,console
|
198
|
+
* import {Path} from '@js-draw/math';
|
199
|
+
* console.log(Path.fromString('m0,0l1,1').reversed()); // -> M1,1 L0,0
|
200
|
+
* ```
|
201
|
+
*/
|
202
|
+
reversed(): Path;
|
203
|
+
/** Computes and returns the end point of this path */
|
204
|
+
getEndPoint(): import("../Vec3").Vec3;
|
94
205
|
/**
|
95
206
|
* Like {@link closedRoughlyIntersects} except takes stroke width into account.
|
96
207
|
*
|
@@ -102,7 +213,15 @@ export declare class Path {
|
|
102
213
|
* `strokeRadius` is half of `strokeWidth`.
|
103
214
|
*/
|
104
215
|
roughlyIntersects(rect: Rect2, strokeWidth?: number): boolean;
|
216
|
+
/**
|
217
|
+
* Treats this as a closed path and returns true if part of `rect` is *roughly* within
|
218
|
+
* this path's interior.
|
219
|
+
*
|
220
|
+
* **Note**: Assumes that this is a closed, non-self-intersecting path.
|
221
|
+
*/
|
105
222
|
closedRoughlyIntersects(rect: Rect2): boolean;
|
223
|
+
/** @returns true if all points on this are equivalent to the points on `other` */
|
224
|
+
eq(other: Path, tolerance?: number): boolean;
|
106
225
|
/**
|
107
226
|
* Returns a path that outlines `rect`.
|
108
227
|
*
|
@@ -126,10 +245,8 @@ export declare class Path {
|
|
126
245
|
/**
|
127
246
|
* Create a `Path` from a subset of the SVG path specification.
|
128
247
|
*
|
129
|
-
*
|
130
|
-
*
|
131
|
-
* - Elliptical arcs are currently unsupported.
|
132
|
-
* - TODO: Support `s`,`t` commands shorthands.
|
248
|
+
* Currently, this does not support elliptical arcs or `s` and `t` command
|
249
|
+
* shorthands. See https://github.com/personalizedrefrigerator/js-draw/pull/19.
|
133
250
|
*
|
134
251
|
* @example
|
135
252
|
* ```ts,runnable,console
|
@@ -140,6 +257,7 @@ export declare class Path {
|
|
140
257
|
* ```
|
141
258
|
*/
|
142
259
|
static fromString(pathString: string): Path;
|
260
|
+
static fromConvexHullOf(points: Point2[]): Path;
|
143
261
|
static empty: Path;
|
144
262
|
}
|
145
263
|
export default Path;
|