@js-draw/math 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- package/README.md +3 -0
- package/build-config.json +4 -0
- package/dist/cjs/Color4.d.ts +83 -0
- package/dist/cjs/Color4.js +277 -0
- package/dist/cjs/Mat33.d.ts +131 -0
- package/dist/cjs/Mat33.js +345 -0
- package/dist/cjs/Vec2.d.ts +42 -0
- package/dist/cjs/Vec2.js +48 -0
- package/dist/cjs/Vec3.d.ts +126 -0
- package/dist/cjs/Vec3.js +203 -0
- package/dist/cjs/lib.d.ts +27 -0
- package/dist/cjs/lib.js +42 -0
- package/dist/cjs/polynomial/solveQuadratic.d.ts +9 -0
- package/dist/cjs/polynomial/solveQuadratic.js +39 -0
- package/dist/cjs/rounding.d.ts +15 -0
- package/dist/cjs/rounding.js +146 -0
- package/dist/cjs/shapes/Abstract2DShape.d.ts +49 -0
- package/dist/cjs/shapes/Abstract2DShape.js +38 -0
- package/dist/cjs/shapes/BezierJSWrapper.d.ts +36 -0
- package/dist/cjs/shapes/BezierJSWrapper.js +94 -0
- package/dist/cjs/shapes/CubicBezier.d.ts +17 -0
- package/dist/cjs/shapes/CubicBezier.js +35 -0
- package/dist/cjs/shapes/LineSegment2.d.ts +70 -0
- package/dist/cjs/shapes/LineSegment2.js +183 -0
- package/dist/cjs/shapes/Path.d.ts +96 -0
- package/dist/cjs/shapes/Path.js +766 -0
- package/dist/cjs/shapes/PointShape2D.d.ts +18 -0
- package/dist/cjs/shapes/PointShape2D.js +31 -0
- package/dist/cjs/shapes/QuadraticBezier.d.ts +35 -0
- package/dist/cjs/shapes/QuadraticBezier.js +120 -0
- package/dist/cjs/shapes/Rect2.d.ts +58 -0
- package/dist/cjs/shapes/Rect2.js +259 -0
- package/dist/cjs/shapes/Triangle.d.ts +46 -0
- package/dist/cjs/shapes/Triangle.js +126 -0
- package/dist/mjs/Color4.d.ts +83 -0
- package/dist/mjs/Color4.mjs +271 -0
- package/dist/mjs/Mat33.d.ts +131 -0
- package/dist/mjs/Mat33.mjs +338 -0
- package/dist/mjs/Vec2.d.ts +42 -0
- package/dist/mjs/Vec2.mjs +42 -0
- package/dist/mjs/Vec3.d.ts +126 -0
- package/dist/mjs/Vec3.mjs +199 -0
- package/dist/mjs/lib.d.ts +27 -0
- package/dist/mjs/lib.mjs +29 -0
- package/dist/mjs/polynomial/solveQuadratic.d.ts +9 -0
- package/dist/mjs/polynomial/solveQuadratic.mjs +37 -0
- package/dist/mjs/rounding.d.ts +15 -0
- package/dist/mjs/rounding.mjs +139 -0
- package/dist/mjs/shapes/Abstract2DShape.d.ts +49 -0
- package/dist/mjs/shapes/Abstract2DShape.mjs +36 -0
- package/dist/mjs/shapes/BezierJSWrapper.d.ts +36 -0
- package/dist/mjs/shapes/BezierJSWrapper.mjs +89 -0
- package/dist/mjs/shapes/CubicBezier.d.ts +17 -0
- package/dist/mjs/shapes/CubicBezier.mjs +30 -0
- package/dist/mjs/shapes/LineSegment2.d.ts +70 -0
- package/dist/mjs/shapes/LineSegment2.mjs +176 -0
- package/dist/mjs/shapes/Path.d.ts +96 -0
- package/dist/mjs/shapes/Path.mjs +759 -0
- package/dist/mjs/shapes/PointShape2D.d.ts +18 -0
- package/dist/mjs/shapes/PointShape2D.mjs +26 -0
- package/dist/mjs/shapes/QuadraticBezier.d.ts +35 -0
- package/dist/mjs/shapes/QuadraticBezier.mjs +113 -0
- package/dist/mjs/shapes/Rect2.d.ts +58 -0
- package/dist/mjs/shapes/Rect2.mjs +252 -0
- package/dist/mjs/shapes/Triangle.d.ts +46 -0
- package/dist/mjs/shapes/Triangle.mjs +121 -0
- package/package.json +48 -0
- package/tsconfig.json +7 -0
- package/typedoc.json +5 -0
@@ -0,0 +1,199 @@
|
|
1
|
+
/**
|
2
|
+
* A vector with three components, $\begin{pmatrix} x \\ y \\ z \end{pmatrix}$.
|
3
|
+
* Can also be used to represent a two-component vector.
|
4
|
+
*
|
5
|
+
* A `Vec3` is immutable.
|
6
|
+
*
|
7
|
+
* @example
|
8
|
+
*
|
9
|
+
* ```ts,runnable,console
|
10
|
+
* import { Vec3 } from '@js-draw/math';
|
11
|
+
*
|
12
|
+
* console.log('Vector addition:', Vec3.of(1, 2, 3).plus(Vec3.of(0, 1, 0)));
|
13
|
+
* console.log('Scalar multiplication:', Vec3.of(1, 2, 3).times(2));
|
14
|
+
* console.log('Cross products:', Vec3.unitX.cross(Vec3.unitY));
|
15
|
+
* console.log('Magnitude:', Vec3.of(1, 2, 3).length(), 'or', Vec3.of(1, 2, 3).magnitude());
|
16
|
+
* console.log('Square Magnitude:', Vec3.of(1, 2, 3).magnitudeSquared());
|
17
|
+
* console.log('As an array:', Vec3.unitZ.asArray());
|
18
|
+
* ```
|
19
|
+
*/
|
20
|
+
export class Vec3 {
|
21
|
+
constructor(x, y, z) {
|
22
|
+
this.x = x;
|
23
|
+
this.y = y;
|
24
|
+
this.z = z;
|
25
|
+
}
|
26
|
+
/** Returns the x, y components of this. */
|
27
|
+
get xy() {
|
28
|
+
// Useful for APIs that behave differently if .z is present.
|
29
|
+
return {
|
30
|
+
x: this.x,
|
31
|
+
y: this.y,
|
32
|
+
};
|
33
|
+
}
|
34
|
+
/** Construct a vector from three components. */
|
35
|
+
static of(x, y, z) {
|
36
|
+
return new Vec3(x, y, z);
|
37
|
+
}
|
38
|
+
/** Returns this' `idx`th component. For example, `Vec3.of(1, 2, 3).at(1) → 2`. */
|
39
|
+
at(idx) {
|
40
|
+
if (idx === 0)
|
41
|
+
return this.x;
|
42
|
+
if (idx === 1)
|
43
|
+
return this.y;
|
44
|
+
if (idx === 2)
|
45
|
+
return this.z;
|
46
|
+
throw new Error(`${idx} out of bounds!`);
|
47
|
+
}
|
48
|
+
/** Alias for this.magnitude. */
|
49
|
+
length() {
|
50
|
+
return this.magnitude();
|
51
|
+
}
|
52
|
+
magnitude() {
|
53
|
+
return Math.sqrt(this.dot(this));
|
54
|
+
}
|
55
|
+
magnitudeSquared() {
|
56
|
+
return this.dot(this);
|
57
|
+
}
|
58
|
+
/**
|
59
|
+
* Return this' angle in the XY plane (treats this as a Vec2).
|
60
|
+
*
|
61
|
+
* This is equivalent to `Math.atan2(vec.y, vec.x)`.
|
62
|
+
*/
|
63
|
+
angle() {
|
64
|
+
return Math.atan2(this.y, this.x);
|
65
|
+
}
|
66
|
+
/**
|
67
|
+
* Returns a unit vector in the same direction as this.
|
68
|
+
*
|
69
|
+
* If `this` has zero length, the resultant vector has `NaN` components.
|
70
|
+
*/
|
71
|
+
normalized() {
|
72
|
+
const norm = this.magnitude();
|
73
|
+
return Vec3.of(this.x / norm, this.y / norm, this.z / norm);
|
74
|
+
}
|
75
|
+
/**
|
76
|
+
* Like {@link normalized}, except returns zero if this has zero magnitude.
|
77
|
+
*/
|
78
|
+
normalizedOrZero() {
|
79
|
+
if (this.eq(Vec3.zero)) {
|
80
|
+
return Vec3.zero;
|
81
|
+
}
|
82
|
+
return this.normalized();
|
83
|
+
}
|
84
|
+
/** @returns A copy of `this` multiplied by a scalar. */
|
85
|
+
times(c) {
|
86
|
+
return Vec3.of(this.x * c, this.y * c, this.z * c);
|
87
|
+
}
|
88
|
+
plus(v) {
|
89
|
+
return Vec3.of(this.x + v.x, this.y + v.y, this.z + v.z);
|
90
|
+
}
|
91
|
+
minus(v) {
|
92
|
+
return Vec3.of(this.x - v.x, this.y - v.y, this.z - v.z);
|
93
|
+
}
|
94
|
+
dot(other) {
|
95
|
+
return this.x * other.x + this.y * other.y + this.z * other.z;
|
96
|
+
}
|
97
|
+
cross(other) {
|
98
|
+
// | i j k |
|
99
|
+
// | x1 y1 z1| = (i)(y1z2 - y2z1) - (j)(x1z2 - x2z1) + (k)(x1y2 - x2y1)
|
100
|
+
// | x2 y2 z2|
|
101
|
+
return Vec3.of(this.y * other.z - other.y * this.z, other.x * this.z - this.x * other.z, this.x * other.y - other.x * this.y);
|
102
|
+
}
|
103
|
+
/**
|
104
|
+
* If `other` is a `Vec3`, multiplies `this` component-wise by `other`. Otherwise,
|
105
|
+
* if `other is a `number`, returns the result of scalar multiplication.
|
106
|
+
*
|
107
|
+
* @example
|
108
|
+
* ```
|
109
|
+
* Vec3.of(1, 2, 3).scale(Vec3.of(2, 4, 6)); // → Vec3(2, 8, 18)
|
110
|
+
* ```
|
111
|
+
*/
|
112
|
+
scale(other) {
|
113
|
+
if (typeof other === 'number') {
|
114
|
+
return this.times(other);
|
115
|
+
}
|
116
|
+
return Vec3.of(this.x * other.x, this.y * other.y, this.z * other.z);
|
117
|
+
}
|
118
|
+
/**
|
119
|
+
* Returns a vector orthogonal to this. If this is a Vec2, returns `this` rotated
|
120
|
+
* 90 degrees counter-clockwise.
|
121
|
+
*/
|
122
|
+
orthog() {
|
123
|
+
// If parallel to the z-axis
|
124
|
+
if (this.dot(Vec3.unitX) === 0 && this.dot(Vec3.unitY) === 0) {
|
125
|
+
return this.dot(Vec3.unitX) === 0 ? Vec3.unitX : this.cross(Vec3.unitX).normalized();
|
126
|
+
}
|
127
|
+
return this.cross(Vec3.unitZ.times(-1)).normalized();
|
128
|
+
}
|
129
|
+
/** Returns this plus a vector of length `distance` in `direction`. */
|
130
|
+
extend(distance, direction) {
|
131
|
+
return this.plus(direction.normalized().times(distance));
|
132
|
+
}
|
133
|
+
/** Returns a vector `fractionTo` of the way to target from this. */
|
134
|
+
lerp(target, fractionTo) {
|
135
|
+
return this.times(1 - fractionTo).plus(target.times(fractionTo));
|
136
|
+
}
|
137
|
+
/**
|
138
|
+
* `zip` Maps a component of this and a corresponding component of
|
139
|
+
* `other` to a component of the output vector.
|
140
|
+
*
|
141
|
+
* @example
|
142
|
+
* ```
|
143
|
+
* const a = Vec3.of(1, 2, 3);
|
144
|
+
* const b = Vec3.of(0.5, 2.1, 2.9);
|
145
|
+
*
|
146
|
+
* const zipped = a.zip(b, (aComponent, bComponent) => {
|
147
|
+
* return Math.min(aComponent, bComponent);
|
148
|
+
* });
|
149
|
+
*
|
150
|
+
* console.log(zipped.toString()); // → Vec(0.5, 2, 2.9)
|
151
|
+
* ```
|
152
|
+
*/
|
153
|
+
zip(other, zip) {
|
154
|
+
return Vec3.of(zip(other.x, this.x), zip(other.y, this.y), zip(other.z, this.z));
|
155
|
+
}
|
156
|
+
/**
|
157
|
+
* Returns a vector with each component acted on by `fn`.
|
158
|
+
*
|
159
|
+
* @example
|
160
|
+
* ```
|
161
|
+
* console.log(Vec3.of(1, 2, 3).map(val => val + 1)); // → Vec(2, 3, 4)
|
162
|
+
* ```
|
163
|
+
*/
|
164
|
+
map(fn) {
|
165
|
+
return Vec3.of(fn(this.x, 0), fn(this.y, 1), fn(this.z, 2));
|
166
|
+
}
|
167
|
+
asArray() {
|
168
|
+
return [this.x, this.y, this.z];
|
169
|
+
}
|
170
|
+
/**
|
171
|
+
* [fuzz] The maximum difference between two components for this and [other]
|
172
|
+
* to be considered equal.
|
173
|
+
*
|
174
|
+
* @example
|
175
|
+
* ```
|
176
|
+
* Vec3.of(1, 2, 3).eq(Vec3.of(4, 5, 6), 100); // → true
|
177
|
+
* Vec3.of(1, 2, 3).eq(Vec3.of(4, 5, 6), 0.1); // → false
|
178
|
+
* Vec3.of(1, 2, 3).eq(Vec3.of(4, 5, 6), 3); // → true
|
179
|
+
* Vec3.of(1, 2, 3).eq(Vec3.of(4, 5, 6), 3.01); // → true
|
180
|
+
* Vec3.of(1, 2, 3).eq(Vec3.of(4, 5, 6), 2.99); // → false
|
181
|
+
* ```
|
182
|
+
*/
|
183
|
+
eq(other, fuzz = 1e-10) {
|
184
|
+
for (let i = 0; i < 3; i++) {
|
185
|
+
if (Math.abs(other.at(i) - this.at(i)) > fuzz) {
|
186
|
+
return false;
|
187
|
+
}
|
188
|
+
}
|
189
|
+
return true;
|
190
|
+
}
|
191
|
+
toString() {
|
192
|
+
return `Vec(${this.x}, ${this.y}, ${this.z})`;
|
193
|
+
}
|
194
|
+
}
|
195
|
+
Vec3.unitX = Vec3.of(1, 0, 0);
|
196
|
+
Vec3.unitY = Vec3.of(0, 1, 0);
|
197
|
+
Vec3.unitZ = Vec3.of(0, 0, 1);
|
198
|
+
Vec3.zero = Vec3.of(0, 0, 0);
|
199
|
+
export default Vec3;
|
@@ -0,0 +1,27 @@
|
|
1
|
+
/**
|
2
|
+
* ```ts,runnable,console
|
3
|
+
* import { Vec2, Mat33, Rect2 } from '@js-draw/math';
|
4
|
+
*
|
5
|
+
* // Example: Rotate a vector 90 degrees about the z-axis
|
6
|
+
* const rotate90Degrees = Mat33.zRotation(Math.PI/2); // π/2 radians = 90 deg
|
7
|
+
* const moveUp = Mat33.translation(Vec2.of(1, 0));
|
8
|
+
* const moveUpThenRotate = rotate90Degrees.rightMul(moveUp);
|
9
|
+
* console.log(moveUpThenRotate.transformVec2(Vec2.of(1, 2)));
|
10
|
+
*
|
11
|
+
* // Example: Bounding box of some points
|
12
|
+
* console.log(Rect2.bboxOf([
|
13
|
+
* Vec2.of(1, 2), Vec2.of(3, 4), Vec2.of(-100, 1000),
|
14
|
+
* ]));
|
15
|
+
* ```
|
16
|
+
*
|
17
|
+
* @packageDocumentation
|
18
|
+
*/
|
19
|
+
export { LineSegment2 } from './shapes/LineSegment2';
|
20
|
+
export { Path, PathCommandType, PathCommand, LinePathCommand, MoveToPathCommand, QuadraticBezierPathCommand, CubicBezierPathCommand, } from './shapes/Path';
|
21
|
+
export { Rect2 } from './shapes/Rect2';
|
22
|
+
export { QuadraticBezier } from './shapes/QuadraticBezier';
|
23
|
+
export { Mat33, Mat33Array } from './Mat33';
|
24
|
+
export { Point2, Vec2 } from './Vec2';
|
25
|
+
export { Vec3 } from './Vec3';
|
26
|
+
export { Color4 } from './Color4';
|
27
|
+
export { toRoundedString } from './rounding';
|
package/dist/mjs/lib.mjs
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
/**
|
2
|
+
* ```ts,runnable,console
|
3
|
+
* import { Vec2, Mat33, Rect2 } from '@js-draw/math';
|
4
|
+
*
|
5
|
+
* // Example: Rotate a vector 90 degrees about the z-axis
|
6
|
+
* const rotate90Degrees = Mat33.zRotation(Math.PI/2); // π/2 radians = 90 deg
|
7
|
+
* const moveUp = Mat33.translation(Vec2.of(1, 0));
|
8
|
+
* const moveUpThenRotate = rotate90Degrees.rightMul(moveUp);
|
9
|
+
* console.log(moveUpThenRotate.transformVec2(Vec2.of(1, 2)));
|
10
|
+
*
|
11
|
+
* // Example: Bounding box of some points
|
12
|
+
* console.log(Rect2.bboxOf([
|
13
|
+
* Vec2.of(1, 2), Vec2.of(3, 4), Vec2.of(-100, 1000),
|
14
|
+
* ]));
|
15
|
+
* ```
|
16
|
+
*
|
17
|
+
* @packageDocumentation
|
18
|
+
*/
|
19
|
+
export { LineSegment2 } from './shapes/LineSegment2.mjs';
|
20
|
+
export { Path, PathCommandType, } from './shapes/Path.mjs';
|
21
|
+
export { Rect2 } from './shapes/Rect2.mjs';
|
22
|
+
export { QuadraticBezier } from './shapes/QuadraticBezier.mjs';
|
23
|
+
export { Mat33 } from './Mat33.mjs';
|
24
|
+
export { Vec2 } from './Vec2.mjs';
|
25
|
+
export { Vec3 } from './Vec3.mjs';
|
26
|
+
export { Color4 } from './Color4.mjs';
|
27
|
+
export { toRoundedString } from './rounding.mjs';
|
28
|
+
// Note: All above exports cannot use `export { default as ... } from "..."` because this
|
29
|
+
// breaks TypeDoc -- TypeDoc otherwise labels any imports of these classes as `default`.
|
@@ -0,0 +1,9 @@
|
|
1
|
+
/**
|
2
|
+
* Solves an equation of the form ax² + bx + c = 0.
|
3
|
+
* The larger solution is returned first.
|
4
|
+
*
|
5
|
+
* If there are no solutions, returns `[NaN, NaN]`. If there is one solution,
|
6
|
+
* repeats the solution twice in the result.
|
7
|
+
*/
|
8
|
+
declare const solveQuadratic: (a: number, b: number, c: number) => [number, number];
|
9
|
+
export default solveQuadratic;
|
@@ -0,0 +1,37 @@
|
|
1
|
+
/**
|
2
|
+
* Solves an equation of the form ax² + bx + c = 0.
|
3
|
+
* The larger solution is returned first.
|
4
|
+
*
|
5
|
+
* If there are no solutions, returns `[NaN, NaN]`. If there is one solution,
|
6
|
+
* repeats the solution twice in the result.
|
7
|
+
*/
|
8
|
+
const solveQuadratic = (a, b, c) => {
|
9
|
+
// See also https://en.wikipedia.org/wiki/Quadratic_formula
|
10
|
+
if (a === 0) {
|
11
|
+
let solution;
|
12
|
+
if (b === 0) {
|
13
|
+
solution = c === 0 ? 0 : NaN;
|
14
|
+
}
|
15
|
+
else {
|
16
|
+
// Then we have bx + c = 0
|
17
|
+
// which implies bx = -c.
|
18
|
+
// Thus, x = -c/b
|
19
|
+
solution = -c / b;
|
20
|
+
}
|
21
|
+
return [solution, solution];
|
22
|
+
}
|
23
|
+
const discriminant = b * b - 4 * a * c;
|
24
|
+
if (discriminant < 0) {
|
25
|
+
return [NaN, NaN];
|
26
|
+
}
|
27
|
+
const rootDiscriminant = Math.sqrt(discriminant);
|
28
|
+
const solution1 = (-b + rootDiscriminant) / (2 * a);
|
29
|
+
const solution2 = (-b - rootDiscriminant) / (2 * a);
|
30
|
+
if (solution1 > solution2) {
|
31
|
+
return [solution1, solution2];
|
32
|
+
}
|
33
|
+
else {
|
34
|
+
return [solution2, solution1];
|
35
|
+
}
|
36
|
+
};
|
37
|
+
export default solveQuadratic;
|
@@ -0,0 +1,15 @@
|
|
1
|
+
export declare const cleanUpNumber: (text: string) => string;
|
2
|
+
/**
|
3
|
+
* Converts `num` to a string, removing trailing digits that were likely caused by
|
4
|
+
* precision errors.
|
5
|
+
*
|
6
|
+
* @example
|
7
|
+
* ```ts,runnable,console
|
8
|
+
* import { toRoundedString } from '@js-draw/math';
|
9
|
+
*
|
10
|
+
* console.log('Rounded: ', toRoundedString(1.000000011));
|
11
|
+
* ```
|
12
|
+
*/
|
13
|
+
export declare const toRoundedString: (num: number) => string;
|
14
|
+
export declare const getLenAfterDecimal: (numberAsString: string) => number;
|
15
|
+
export declare const toStringOfSamePrecision: (num: number, ...references: string[]) => string;
|
@@ -0,0 +1,139 @@
|
|
1
|
+
// @packageDocumentation @internal
|
2
|
+
// Clean up stringified numbers
|
3
|
+
export const cleanUpNumber = (text) => {
|
4
|
+
// Regular expression substitions can be somewhat expensive. Only do them
|
5
|
+
// if necessary.
|
6
|
+
if (text.indexOf('e') > 0) {
|
7
|
+
// Round to zero.
|
8
|
+
if (text.match(/[eE][-]\d{2,}$/)) {
|
9
|
+
return '0';
|
10
|
+
}
|
11
|
+
}
|
12
|
+
const lastChar = text.charAt(text.length - 1);
|
13
|
+
if (lastChar === '0' || lastChar === '.') {
|
14
|
+
// Remove trailing zeroes
|
15
|
+
text = text.replace(/([.]\d*[^0]+)0+$/, '$1');
|
16
|
+
text = text.replace(/[.]0+$/, '.');
|
17
|
+
// Remove trailing period
|
18
|
+
text = text.replace(/[.]$/, '');
|
19
|
+
}
|
20
|
+
const firstChar = text.charAt(0);
|
21
|
+
if (firstChar === '0' || firstChar === '-') {
|
22
|
+
// Remove unnecessary leading zeroes.
|
23
|
+
text = text.replace(/^(0+)[.]/, '.');
|
24
|
+
text = text.replace(/^-(0+)[.]/, '-.');
|
25
|
+
text = text.replace(/^(-?)0+$/, '$10');
|
26
|
+
}
|
27
|
+
if (text === '-0') {
|
28
|
+
return '0';
|
29
|
+
}
|
30
|
+
return text;
|
31
|
+
};
|
32
|
+
/**
|
33
|
+
* Converts `num` to a string, removing trailing digits that were likely caused by
|
34
|
+
* precision errors.
|
35
|
+
*
|
36
|
+
* @example
|
37
|
+
* ```ts,runnable,console
|
38
|
+
* import { toRoundedString } from '@js-draw/math';
|
39
|
+
*
|
40
|
+
* console.log('Rounded: ', toRoundedString(1.000000011));
|
41
|
+
* ```
|
42
|
+
*/
|
43
|
+
export const toRoundedString = (num) => {
|
44
|
+
// Try to remove rounding errors. If the number ends in at least three/four zeroes
|
45
|
+
// (or nines) just one or two digits, it's probably a rounding error.
|
46
|
+
const fixRoundingUpExp = /^([-]?\d*\.\d{3,})0{4,}\d{1,4}$/;
|
47
|
+
const hasRoundingDownExp = /^([-]?)(\d*)\.(\d{3,}9{4,})\d{1,4}$/;
|
48
|
+
let text = num.toString(10);
|
49
|
+
if (text.indexOf('.') === -1) {
|
50
|
+
return text;
|
51
|
+
}
|
52
|
+
const roundingDownMatch = hasRoundingDownExp.exec(text);
|
53
|
+
if (roundingDownMatch) {
|
54
|
+
const negativeSign = roundingDownMatch[1];
|
55
|
+
const postDecimalString = roundingDownMatch[3];
|
56
|
+
const lastDigit = parseInt(postDecimalString.charAt(postDecimalString.length - 1), 10);
|
57
|
+
const postDecimal = parseInt(postDecimalString, 10);
|
58
|
+
const preDecimal = parseInt(roundingDownMatch[2], 10);
|
59
|
+
const origPostDecimalString = roundingDownMatch[3];
|
60
|
+
let newPostDecimal = (postDecimal + 10 - lastDigit).toString();
|
61
|
+
let carry = 0;
|
62
|
+
if (newPostDecimal.length > postDecimal.toString().length) {
|
63
|
+
// Left-shift
|
64
|
+
newPostDecimal = newPostDecimal.substring(1);
|
65
|
+
carry = 1;
|
66
|
+
}
|
67
|
+
// parseInt(...).toString() removes leading zeroes. Add them back.
|
68
|
+
while (newPostDecimal.length < origPostDecimalString.length) {
|
69
|
+
newPostDecimal = carry.toString(10) + newPostDecimal;
|
70
|
+
carry = 0;
|
71
|
+
}
|
72
|
+
text = `${negativeSign + (preDecimal + carry).toString()}.${newPostDecimal}`;
|
73
|
+
}
|
74
|
+
text = text.replace(fixRoundingUpExp, '$1');
|
75
|
+
return cleanUpNumber(text);
|
76
|
+
};
|
77
|
+
const numberExp = /^([-]?)(\d*)[.](\d+)$/;
|
78
|
+
export const getLenAfterDecimal = (numberAsString) => {
|
79
|
+
const numberMatch = numberExp.exec(numberAsString);
|
80
|
+
if (!numberMatch) {
|
81
|
+
// If not a match, either the number is exponential notation (or is something
|
82
|
+
// like NaN or Infinity)
|
83
|
+
if (numberAsString.search(/[eE]/) !== -1 || /^[a-zA-Z]+$/.exec(numberAsString)) {
|
84
|
+
return -1;
|
85
|
+
// Or it has no decimal point
|
86
|
+
}
|
87
|
+
else {
|
88
|
+
return 0;
|
89
|
+
}
|
90
|
+
}
|
91
|
+
const afterDecimalLen = numberMatch[3].length;
|
92
|
+
return afterDecimalLen;
|
93
|
+
};
|
94
|
+
// [reference] should be a string representation of a base-10 number (no exponential (e.g. 10e10))
|
95
|
+
export const toStringOfSamePrecision = (num, ...references) => {
|
96
|
+
const text = num.toString(10);
|
97
|
+
const textMatch = numberExp.exec(text);
|
98
|
+
if (!textMatch) {
|
99
|
+
return text;
|
100
|
+
}
|
101
|
+
let decimalPlaces = -1;
|
102
|
+
for (const reference of references) {
|
103
|
+
decimalPlaces = Math.max(getLenAfterDecimal(reference), decimalPlaces);
|
104
|
+
}
|
105
|
+
if (decimalPlaces === -1) {
|
106
|
+
return toRoundedString(num);
|
107
|
+
}
|
108
|
+
// Make text's after decimal length match [afterDecimalLen].
|
109
|
+
let postDecimal = textMatch[3].substring(0, decimalPlaces);
|
110
|
+
let preDecimal = textMatch[2];
|
111
|
+
const nextDigit = textMatch[3].charAt(decimalPlaces);
|
112
|
+
if (nextDigit !== '') {
|
113
|
+
const asNumber = parseInt(nextDigit, 10);
|
114
|
+
if (asNumber >= 5) {
|
115
|
+
// Don't attempt to parseInt() an empty string.
|
116
|
+
if (postDecimal.length > 0) {
|
117
|
+
const leadingZeroMatch = /^(0+)(\d*)$/.exec(postDecimal);
|
118
|
+
let leadingZeroes = '';
|
119
|
+
let postLeading = postDecimal;
|
120
|
+
if (leadingZeroMatch) {
|
121
|
+
leadingZeroes = leadingZeroMatch[1];
|
122
|
+
postLeading = leadingZeroMatch[2];
|
123
|
+
}
|
124
|
+
postDecimal = (parseInt(postDecimal) + 1).toString();
|
125
|
+
// If postDecimal got longer, remove leading zeroes if possible
|
126
|
+
if (postDecimal.length > postLeading.length && leadingZeroes.length > 0) {
|
127
|
+
leadingZeroes = leadingZeroes.substring(1);
|
128
|
+
}
|
129
|
+
postDecimal = leadingZeroes + postDecimal;
|
130
|
+
}
|
131
|
+
if (postDecimal.length === 0 || postDecimal.length > decimalPlaces) {
|
132
|
+
preDecimal = (parseInt(preDecimal) + 1).toString();
|
133
|
+
postDecimal = postDecimal.substring(1);
|
134
|
+
}
|
135
|
+
}
|
136
|
+
}
|
137
|
+
const negativeSign = textMatch[1];
|
138
|
+
return cleanUpNumber(`${negativeSign}${preDecimal}.${postDecimal}`);
|
139
|
+
};
|
@@ -0,0 +1,49 @@
|
|
1
|
+
import LineSegment2 from './LineSegment2';
|
2
|
+
import { Point2 } from '../Vec2';
|
3
|
+
import Rect2 from './Rect2';
|
4
|
+
declare abstract class Abstract2DShape {
|
5
|
+
protected static readonly smallValue = 1e-12;
|
6
|
+
/**
|
7
|
+
* @returns the distance from `point` to this shape. If `point` is within this shape,
|
8
|
+
* this returns the distance from `point` to the edge of this shape.
|
9
|
+
*
|
10
|
+
* @see {@link signedDistance}
|
11
|
+
*/
|
12
|
+
distance(point: Point2): number;
|
13
|
+
/**
|
14
|
+
* Computes the [signed distance function](https://en.wikipedia.org/wiki/Signed_distance_function)
|
15
|
+
* for this shape.
|
16
|
+
*/
|
17
|
+
abstract signedDistance(point: Point2): number;
|
18
|
+
/**
|
19
|
+
* @returns points at which this shape intersects the given `lineSegment`.
|
20
|
+
*
|
21
|
+
* If this is a closed shape, returns points where the given `lineSegment` intersects
|
22
|
+
* the **boundary** of this.
|
23
|
+
*/
|
24
|
+
abstract intersectsLineSegment(lineSegment: LineSegment2): Point2[];
|
25
|
+
/**
|
26
|
+
* Returns `true` if and only if the given `point` is contained within this shape.
|
27
|
+
*
|
28
|
+
* `epsilon` is a small number used to counteract floating point error. Thus, if
|
29
|
+
* `point` is within `epsilon` of the inside of this shape, `containsPoint` may also
|
30
|
+
* return `true`.
|
31
|
+
*
|
32
|
+
* The default implementation relies on `signedDistance`.
|
33
|
+
* Subclasses may override this method to provide a more efficient implementation.
|
34
|
+
*/
|
35
|
+
containsPoint(point: Point2, epsilon?: number): boolean;
|
36
|
+
/**
|
37
|
+
* Returns a bounding box that precisely fits the content of this shape.
|
38
|
+
*/
|
39
|
+
abstract getTightBoundingBox(): Rect2;
|
40
|
+
/**
|
41
|
+
* Returns a bounding box that **loosely** fits the content of this shape.
|
42
|
+
*
|
43
|
+
* The result of this call can be larger than the result of {@link getTightBoundingBox},
|
44
|
+
* **but should not be smaller**. Thus, a call to `getLooseBoundingBox` can be significantly
|
45
|
+
* faster than a call to {@link getTightBoundingBox} for some shapes.
|
46
|
+
*/
|
47
|
+
getLooseBoundingBox(): Rect2;
|
48
|
+
}
|
49
|
+
export default Abstract2DShape;
|
@@ -0,0 +1,36 @@
|
|
1
|
+
class Abstract2DShape {
|
2
|
+
/**
|
3
|
+
* @returns the distance from `point` to this shape. If `point` is within this shape,
|
4
|
+
* this returns the distance from `point` to the edge of this shape.
|
5
|
+
*
|
6
|
+
* @see {@link signedDistance}
|
7
|
+
*/
|
8
|
+
distance(point) {
|
9
|
+
return Math.abs(this.signedDistance(point));
|
10
|
+
}
|
11
|
+
/**
|
12
|
+
* Returns `true` if and only if the given `point` is contained within this shape.
|
13
|
+
*
|
14
|
+
* `epsilon` is a small number used to counteract floating point error. Thus, if
|
15
|
+
* `point` is within `epsilon` of the inside of this shape, `containsPoint` may also
|
16
|
+
* return `true`.
|
17
|
+
*
|
18
|
+
* The default implementation relies on `signedDistance`.
|
19
|
+
* Subclasses may override this method to provide a more efficient implementation.
|
20
|
+
*/
|
21
|
+
containsPoint(point, epsilon = Abstract2DShape.smallValue) {
|
22
|
+
return this.signedDistance(point) < epsilon;
|
23
|
+
}
|
24
|
+
/**
|
25
|
+
* Returns a bounding box that **loosely** fits the content of this shape.
|
26
|
+
*
|
27
|
+
* The result of this call can be larger than the result of {@link getTightBoundingBox},
|
28
|
+
* **but should not be smaller**. Thus, a call to `getLooseBoundingBox` can be significantly
|
29
|
+
* faster than a call to {@link getTightBoundingBox} for some shapes.
|
30
|
+
*/
|
31
|
+
getLooseBoundingBox() {
|
32
|
+
return this.getTightBoundingBox();
|
33
|
+
}
|
34
|
+
}
|
35
|
+
Abstract2DShape.smallValue = 1e-12;
|
36
|
+
export default Abstract2DShape;
|
@@ -0,0 +1,36 @@
|
|
1
|
+
import { Bezier } from 'bezier-js';
|
2
|
+
import { Point2, Vec2 } from '../Vec2';
|
3
|
+
import Abstract2DShape from './Abstract2DShape';
|
4
|
+
import LineSegment2 from './LineSegment2';
|
5
|
+
import Rect2 from './Rect2';
|
6
|
+
/**
|
7
|
+
* A lazy-initializing wrapper around Bezier-js.
|
8
|
+
*
|
9
|
+
* Subclasses may override `at`, `derivativeAt`, and `normal` with functions
|
10
|
+
* that do not initialize a `bezier-js` `Bezier`.
|
11
|
+
*
|
12
|
+
* Do not use this class directly. It may be removed/replaced in a future release.
|
13
|
+
* @internal
|
14
|
+
*/
|
15
|
+
declare abstract class BezierJSWrapper extends Abstract2DShape {
|
16
|
+
#private;
|
17
|
+
/** Returns the start, control points, and end point of this Bézier. */
|
18
|
+
abstract getPoints(): Point2[];
|
19
|
+
protected getBezier(): Bezier;
|
20
|
+
signedDistance(point: Point2): number;
|
21
|
+
/**
|
22
|
+
* @returns the (more) exact distance from `point` to this.
|
23
|
+
*
|
24
|
+
* @see {@link approximateDistance}
|
25
|
+
*/
|
26
|
+
distance(point: Point2): number;
|
27
|
+
/**
|
28
|
+
* @returns the curve evaluated at `t`.
|
29
|
+
*/
|
30
|
+
at(t: number): Point2;
|
31
|
+
derivativeAt(t: number): Point2;
|
32
|
+
normal(t: number): Vec2;
|
33
|
+
getTightBoundingBox(): Rect2;
|
34
|
+
intersectsLineSegment(line: LineSegment2): Point2[];
|
35
|
+
}
|
36
|
+
export default BezierJSWrapper;
|
@@ -0,0 +1,89 @@
|
|
1
|
+
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
|
2
|
+
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
|
3
|
+
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
|
4
|
+
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
5
|
+
};
|
6
|
+
var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
|
7
|
+
if (kind === "m") throw new TypeError("Private method is not writable");
|
8
|
+
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
|
9
|
+
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
|
10
|
+
return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
|
11
|
+
};
|
12
|
+
var _BezierJSWrapper_bezierJs;
|
13
|
+
import { Bezier } from 'bezier-js';
|
14
|
+
import { Vec2 } from '../Vec2.mjs';
|
15
|
+
import Abstract2DShape from './Abstract2DShape.mjs';
|
16
|
+
import Rect2 from './Rect2.mjs';
|
17
|
+
/**
|
18
|
+
* A lazy-initializing wrapper around Bezier-js.
|
19
|
+
*
|
20
|
+
* Subclasses may override `at`, `derivativeAt`, and `normal` with functions
|
21
|
+
* that do not initialize a `bezier-js` `Bezier`.
|
22
|
+
*
|
23
|
+
* Do not use this class directly. It may be removed/replaced in a future release.
|
24
|
+
* @internal
|
25
|
+
*/
|
26
|
+
class BezierJSWrapper extends Abstract2DShape {
|
27
|
+
constructor() {
|
28
|
+
super(...arguments);
|
29
|
+
_BezierJSWrapper_bezierJs.set(this, null);
|
30
|
+
}
|
31
|
+
getBezier() {
|
32
|
+
if (!__classPrivateFieldGet(this, _BezierJSWrapper_bezierJs, "f")) {
|
33
|
+
__classPrivateFieldSet(this, _BezierJSWrapper_bezierJs, new Bezier(this.getPoints().map(p => p.xy)), "f");
|
34
|
+
}
|
35
|
+
return __classPrivateFieldGet(this, _BezierJSWrapper_bezierJs, "f");
|
36
|
+
}
|
37
|
+
signedDistance(point) {
|
38
|
+
// .d: Distance
|
39
|
+
return this.getBezier().project(point.xy).d;
|
40
|
+
}
|
41
|
+
/**
|
42
|
+
* @returns the (more) exact distance from `point` to this.
|
43
|
+
*
|
44
|
+
* @see {@link approximateDistance}
|
45
|
+
*/
|
46
|
+
distance(point) {
|
47
|
+
// A Bézier curve has no interior, thus, signed distance is the same as distance.
|
48
|
+
return this.signedDistance(point);
|
49
|
+
}
|
50
|
+
/**
|
51
|
+
* @returns the curve evaluated at `t`.
|
52
|
+
*/
|
53
|
+
at(t) {
|
54
|
+
return Vec2.ofXY(this.getBezier().get(t));
|
55
|
+
}
|
56
|
+
derivativeAt(t) {
|
57
|
+
return Vec2.ofXY(this.getBezier().derivative(t));
|
58
|
+
}
|
59
|
+
normal(t) {
|
60
|
+
return Vec2.ofXY(this.getBezier().normal(t));
|
61
|
+
}
|
62
|
+
getTightBoundingBox() {
|
63
|
+
const bbox = this.getBezier().bbox();
|
64
|
+
const width = bbox.x.max - bbox.x.min;
|
65
|
+
const height = bbox.y.max - bbox.y.min;
|
66
|
+
return new Rect2(bbox.x.min, bbox.y.min, width, height);
|
67
|
+
}
|
68
|
+
intersectsLineSegment(line) {
|
69
|
+
const bezier = this.getBezier();
|
70
|
+
const intersectionPoints = bezier.intersects(line).map(t => {
|
71
|
+
// We're using the .intersects(line) function, which is documented
|
72
|
+
// to always return numbers. However, to satisfy the type checker (and
|
73
|
+
// possibly improperly-defined types),
|
74
|
+
if (typeof t === 'string') {
|
75
|
+
t = parseFloat(t);
|
76
|
+
}
|
77
|
+
const point = Vec2.ofXY(bezier.get(t));
|
78
|
+
// Ensure that the intersection is on the line segment
|
79
|
+
if (point.minus(line.p1).magnitude() > line.length
|
80
|
+
|| point.minus(line.p2).magnitude() > line.length) {
|
81
|
+
return null;
|
82
|
+
}
|
83
|
+
return point;
|
84
|
+
}).filter(entry => entry !== null);
|
85
|
+
return intersectionPoints;
|
86
|
+
}
|
87
|
+
}
|
88
|
+
_BezierJSWrapper_bezierJs = new WeakMap();
|
89
|
+
export default BezierJSWrapper;
|