@js-draw/math 1.18.0 → 1.19.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -18,7 +18,39 @@ declare class PointShape2D extends Parameterized2DShape {
18
18
  /**
19
19
  * Returns an arbitrary unit-length vector.
20
20
  */
21
- normalAt(_t: number): Vec3;
21
+ normalAt(_t: number): {
22
+ readonly x: number;
23
+ readonly y: number;
24
+ readonly z: number;
25
+ readonly xy: {
26
+ x: number;
27
+ y: number;
28
+ };
29
+ at(idx: number): number;
30
+ length(): number;
31
+ magnitude(): number;
32
+ magnitudeSquared(): number;
33
+ squareDistanceTo(p: Vec3): number;
34
+ distanceTo(p: Vec3): number;
35
+ maximumEntryMagnitude(): number;
36
+ angle(): number;
37
+ normalized(): Vec3;
38
+ normalizedOrZero(): Vec3;
39
+ times(c: number): Vec3;
40
+ plus(v: Vec3): Vec3;
41
+ minus(v: Vec3): Vec3;
42
+ dot(other: Vec3): number;
43
+ cross(other: Vec3): Vec3;
44
+ scale(other: number | Vec3): Vec3;
45
+ orthog(): Vec3;
46
+ extend(distance: number, direction: Vec3): Vec3;
47
+ lerp(target: Vec3, fractionTo: number): Vec3;
48
+ zip(other: Vec3, zip: (componentInThis: number, componentInOther: number) => number): Vec3;
49
+ map(fn: (component: number, index: number) => number): Vec3;
50
+ asArray(): [number, number, number];
51
+ eq(other: Vec3, fuzz?: number): boolean;
52
+ toString(): string;
53
+ };
22
54
  tangentAt(_t: number): Vec3;
23
55
  splitAt(_t: number): [PointShape2D];
24
56
  nearestPointTo(_point: Point2): {
@@ -57,14 +57,46 @@ export declare class Rect2 extends Abstract2DShape {
57
57
  get bottomLeft(): Vec3;
58
58
  get width(): number;
59
59
  get height(): number;
60
- get center(): Vec3;
60
+ get center(): {
61
+ readonly x: number;
62
+ readonly y: number;
63
+ readonly z: number;
64
+ readonly xy: {
65
+ x: number;
66
+ y: number;
67
+ };
68
+ at(idx: number): number;
69
+ length(): number;
70
+ magnitude(): number;
71
+ magnitudeSquared(): number;
72
+ squareDistanceTo(p: Vec3): number;
73
+ distanceTo(p: Vec3): number;
74
+ maximumEntryMagnitude(): number;
75
+ angle(): number;
76
+ normalized(): Vec3;
77
+ normalizedOrZero(): Vec3;
78
+ times(c: number): Vec3;
79
+ plus(v: Vec3): Vec3;
80
+ minus(v: Vec3): Vec3;
81
+ dot(other: Vec3): number;
82
+ cross(other: Vec3): Vec3;
83
+ scale(other: number | Vec3): Vec3;
84
+ orthog(): Vec3;
85
+ extend(distance: number, direction: Vec3): Vec3;
86
+ lerp(target: Vec3, fractionTo: number): Vec3;
87
+ zip(other: Vec3, zip: (componentInThis: number, componentInOther: number) => number): Vec3;
88
+ map(fn: (component: number, index: number) => number): Vec3;
89
+ asArray(): [number, number, number];
90
+ eq(other: Vec3, fuzz?: number): boolean;
91
+ toString(): string;
92
+ };
61
93
  getEdges(): LineSegment2[];
62
94
  intersectsLineSegment(lineSegment: LineSegment2): Point2[];
63
95
  signedDistance(point: Vec3): number;
64
96
  getTightBoundingBox(): Rect2;
65
97
  transformedBoundingBox(affineTransform: Mat33): Rect2;
66
- /** @return true iff this is equal to [other] ± fuzz */
67
- eq(other: Rect2, fuzz?: number): boolean;
98
+ /** @return true iff this is equal to `other ± tolerance` */
99
+ eq(other: Rect2, tolerance?: number): boolean;
68
100
  toString(): string;
69
101
  static fromCorners(corner1: Point2, corner2: Point2): Rect2;
70
102
  static bboxOf(points: Point2[], margin?: number): Rect2;
@@ -227,9 +227,9 @@ class Rect2 extends Abstract2DShape_1.default {
227
227
  transformedBoundingBox(affineTransform) {
228
228
  return Rect2.bboxOf(this.corners.map(corner => affineTransform.transformVec2(corner)));
229
229
  }
230
- /** @return true iff this is equal to [other] ± fuzz */
231
- eq(other, fuzz = 0) {
232
- return this.topLeft.eq(other.topLeft, fuzz) && this.size.eq(other.size, fuzz);
230
+ /** @return true iff this is equal to `other ± tolerance` */
231
+ eq(other, tolerance = 0) {
232
+ return this.topLeft.eq(other.topLeft, tolerance) && this.size.eq(other.size, tolerance);
233
233
  }
234
234
  toString() {
235
235
  return `Rect(point(${this.x}, ${this.y}), size(${this.w}, ${this.h}))`;
@@ -1,42 +1,5 @@
1
- import Vec3 from './Vec3';
2
- /**
3
- * Utility functions that facilitate treating `Vec3`s as 2D vectors.
4
- *
5
- * @example
6
- * ```ts,runnable,console
7
- * import { Vec2 } from '@js-draw/math';
8
- * console.log(Vec2.of(1, 2));
9
- * ```
10
- */
11
- export declare namespace Vec2 {
12
- /**
13
- * Creates a `Vec2` from an x and y coordinate.
14
- *
15
- * For example,
16
- * ```ts
17
- * const v = Vec2.of(3, 4); // x=3, y=4.
18
- * ```
19
- */
20
- const of: (x: number, y: number) => Vec2;
21
- /**
22
- * Creates a `Vec2` from an object containing x and y coordinates.
23
- *
24
- * For example,
25
- * ```ts
26
- * const v1 = Vec2.ofXY({ x: 3, y: 4.5 });
27
- * const v2 = Vec2.ofXY({ x: -123.4, y: 1 });
28
- * ```
29
- */
30
- const ofXY: ({ x, y }: {
31
- x: number;
32
- y: number;
33
- }) => Vec2;
34
- /** A vector of length 1 in the X direction (→). */
35
- const unitX: Vec3;
36
- /** A vector of length 1 in the Y direction (↑). */
37
- const unitY: Vec3;
38
- /** The zero vector: A vector with x=0, y=0. */
39
- const zero: Vec3;
40
- }
1
+ import { Vec3, Vec2 } from './Vec3';
41
2
  export type Point2 = Vec3;
42
3
  export type Vec2 = Vec3;
4
+ export { Vec3, Vec2 };
5
+ export default Vec2;
package/dist/mjs/Vec2.mjs CHANGED
@@ -1,42 +1,6 @@
1
- import Vec3 from './Vec3.mjs';
2
- /**
3
- * Utility functions that facilitate treating `Vec3`s as 2D vectors.
4
- *
5
- * @example
6
- * ```ts,runnable,console
7
- * import { Vec2 } from '@js-draw/math';
8
- * console.log(Vec2.of(1, 2));
9
- * ```
10
- */
11
- export var Vec2;
12
- (function (Vec2) {
13
- /**
14
- * Creates a `Vec2` from an x and y coordinate.
15
- *
16
- * For example,
17
- * ```ts
18
- * const v = Vec2.of(3, 4); // x=3, y=4.
19
- * ```
20
- */
21
- Vec2.of = (x, y) => {
22
- return Vec3.of(x, y, 0);
23
- };
24
- /**
25
- * Creates a `Vec2` from an object containing x and y coordinates.
26
- *
27
- * For example,
28
- * ```ts
29
- * const v1 = Vec2.ofXY({ x: 3, y: 4.5 });
30
- * const v2 = Vec2.ofXY({ x: -123.4, y: 1 });
31
- * ```
32
- */
33
- Vec2.ofXY = ({ x, y }) => {
34
- return Vec3.of(x, y, 0);
35
- };
36
- /** A vector of length 1 in the X direction (→). */
37
- Vec2.unitX = Vec2.of(1, 0);
38
- /** A vector of length 1 in the Y direction (↑). */
39
- Vec2.unitY = Vec2.of(0, 1);
40
- /** The zero vector: A vector with x=0, y=0. */
41
- Vec2.zero = Vec2.of(0, 0);
42
- })(Vec2 || (Vec2 = {}));
1
+ // Internally, we define Vec2 as a namespace within Vec3 --
2
+ // this allows referencing Vec2s from Vec3 constructors without
3
+ // cyclic references.
4
+ import { Vec3, Vec2 } from './Vec3.mjs';
5
+ export { Vec3, Vec2 };
6
+ export default Vec2;
@@ -17,22 +17,23 @@
17
17
  * console.log('As an array:', Vec3.unitZ.asArray());
18
18
  * ```
19
19
  */
20
- export declare class Vec3 {
20
+ export interface Vec3 {
21
21
  readonly x: number;
22
22
  readonly y: number;
23
23
  readonly z: number;
24
- private constructor();
25
- /** Returns the x, y components of this. */
26
- get xy(): {
24
+ /**
25
+ * Returns the x, y components of this.
26
+ * May be implemented as a getter method.
27
+ */
28
+ readonly xy: {
27
29
  x: number;
28
30
  y: number;
29
31
  };
30
- /** Construct a vector from three components. */
31
- static of(x: number, y: number, z: number): Vec3;
32
- /** Returns this' `idx`th component. For example, `Vec3.of(1, 2, 3).at(1) → 2`. */
33
- at(idx: number): number;
34
- /** Alias for this.magnitude. */
32
+ /** Returns the vector's `idx`th component. For example, `Vec3.of(1, 2, 3).at(1) → 2`. */
33
+ at(i: number): number;
34
+ /** Alias for `.magnitude`. */
35
35
  length(): number;
36
+ /** Returns the length of this vector in ℝ^3. */
36
37
  magnitude(): number;
37
38
  magnitudeSquared(): number;
38
39
  /**
@@ -41,7 +42,7 @@ export declare class Vec3 {
41
42
  *
42
43
  * Equivalent to `.minus(p).magnitudeSquared()`.
43
44
  */
44
- squareDistanceTo(p: Vec3): number;
45
+ squareDistanceTo(other: Vec3): number;
45
46
  /**
46
47
  * Interpreting this vector as a point in ℝ³, returns the distance to the point
47
48
  * `p`.
@@ -90,10 +91,17 @@ export declare class Vec3 {
90
91
  normalizedOrZero(): Vec3;
91
92
  /** @returns A copy of `this` multiplied by a scalar. */
92
93
  times(c: number): Vec3;
94
+ /** Performs vector addition. */
93
95
  plus(v: Vec3): Vec3;
94
96
  minus(v: Vec3): Vec3;
95
- dot(other: Vec3): number;
96
- cross(other: Vec3): Vec3;
97
+ /**
98
+ * Computes the scalar product between this and `v`.
99
+ *
100
+ * In particular, `a.dot(b)` is equivalent to `a.x * b.x + a.y * b.y + a.z * b.z`.
101
+ */
102
+ dot(v: Vec3): number;
103
+ /** Computes the cross product between this and `v` */
104
+ cross(v: Vec3): Vec3;
97
105
  /**
98
106
  * If `other` is a `Vec3`, multiplies `this` component-wise by `other`. Otherwise,
99
107
  * if `other is a `number`, returns the result of scalar multiplication.
@@ -154,11 +162,99 @@ export declare class Vec3 {
154
162
  * Vec3.of(1, 2, 3).eq(Vec3.of(4, 5, 6), 2.99); // → false
155
163
  * ```
156
164
  */
165
+ eq(other: Vec3, tolerance?: number): boolean;
166
+ toString(): string;
167
+ }
168
+ declare class Vec2Impl implements Vec3 {
169
+ readonly x: number;
170
+ readonly y: number;
171
+ constructor(x: number, y: number);
172
+ get z(): number;
173
+ get xy(): {
174
+ x: number;
175
+ y: number;
176
+ };
177
+ at(idx: number): number;
178
+ length(): number;
179
+ magnitude(): number;
180
+ magnitudeSquared(): number;
181
+ squareDistanceTo(p: Vec3): number;
182
+ distanceTo(p: Vec3): number;
183
+ maximumEntryMagnitude(): number;
184
+ angle(): number;
185
+ normalized(): Vec3;
186
+ normalizedOrZero(): Vec3;
187
+ times(c: number): Vec3;
188
+ plus(v: Vec3): Vec3;
189
+ minus(v: Vec3): Vec3;
190
+ dot(other: Vec3): number;
191
+ cross(other: Vec3): Vec3;
192
+ scale(other: Vec3 | number): Vec3;
193
+ orthog(): Vec3;
194
+ extend(distance: number, direction: Vec3): Vec3;
195
+ lerp(target: Vec3, fractionTo: number): Vec3;
196
+ zip(other: Vec3, zip: (componentInThis: number, componentInOther: number) => number): Vec3;
197
+ map(fn: (component: number, index: number) => number): Vec3;
198
+ asArray(): [number, number, number];
157
199
  eq(other: Vec3, fuzz?: number): boolean;
158
200
  toString(): string;
159
- static unitX: Vec3;
160
- static unitY: Vec3;
161
- static unitZ: Vec3;
162
- static zero: Vec3;
201
+ }
202
+ /**
203
+ * A `Vec2` is a `Vec3` optimized for working in a plane. As such, they have an
204
+ * always-zero `z` component.
205
+ *
206
+ * ```ts,runnable,console
207
+ * import { Vec2 } from '@js-draw/math';
208
+ * console.log(Vec2.of(1, 2));
209
+ * ```
210
+ */
211
+ export declare namespace Vec2 {
212
+ /**
213
+ * Creates a `Vec2` from an x and y coordinate.
214
+ *
215
+ * @example
216
+ * ```ts,runnable,console
217
+ * import { Vec2 } from '@js-draw/math';
218
+ * const v = Vec2.of(3, 4); // x=3, y=4.
219
+ * ```
220
+ */
221
+ const of: (x: number, y: number) => Vec2Impl;
222
+ /**
223
+ * Creates a `Vec2` from an object containing `x` and `y` coordinates.
224
+ *
225
+ * @example
226
+ * ```ts,runnable,console
227
+ * import { Vec2 } from '@js-draw/math';
228
+ * const v1 = Vec2.ofXY({ x: 3, y: 4.5 });
229
+ * const v2 = Vec2.ofXY({ x: -123.4, y: 1 });
230
+ * ```
231
+ */
232
+ const ofXY: ({ x, y }: {
233
+ x: number;
234
+ y: number;
235
+ }) => Vec2Impl;
236
+ /** A vector of length 1 in the X direction (→). */
237
+ const unitX: Vec2Impl;
238
+ /** A vector of length 1 in the Y direction (↑). */
239
+ const unitY: Vec2Impl;
240
+ /** The zero vector: A vector with x=0, y=0. */
241
+ const zero: Vec2Impl;
242
+ }
243
+ export declare namespace Vec3 {
244
+ /**
245
+ * Construct a vector from three components.
246
+ *
247
+ * @example
248
+ * ```ts,runnable,console
249
+ * import { Vec3 } from '@js-draw/math';
250
+ * const v1 = Vec3.of(1, 2, 3);
251
+ * ```
252
+ */
253
+ const of: (x: number, y: number, z: number) => Vec3;
254
+ const unitX: Vec2Impl;
255
+ const unitY: Vec2Impl;
256
+ const zero: Vec2Impl;
257
+ /** A vector of length 1 in the z direction. */
258
+ const unitZ: Vec3;
163
259
  }
164
260
  export default Vec3;