@fimbul-works/vec 1.0.1 → 1.0.3
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/VEC2.md +147 -0
- package/VEC3.md +159 -0
- package/VEC4.md +146 -0
- package/package.json +3 -3
package/VEC2.md
ADDED
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
# Vec2
|
|
2
|
+
|
|
3
|
+
A 2D vector implementation with comprehensive mathematical operations and utility methods.
|
|
4
|
+
|
|
5
|
+
## Constructor
|
|
6
|
+
|
|
7
|
+
### Vec2
|
|
8
|
+
|
|
9
|
+
- `constructor(x: number = 0, y: number = x)`: Creates a new vector with the given x and y coordinates
|
|
10
|
+
|
|
11
|
+
## Static Methods
|
|
12
|
+
|
|
13
|
+
### Creation and Conversion
|
|
14
|
+
|
|
15
|
+
- `fromArray(arr: [number, number] | number[])`: Creates a Vec2 from an array
|
|
16
|
+
- `fromObject(obj: { x: number; y: number })`: Creates a Vec2 from an object with x and y properties
|
|
17
|
+
- `fromJSON(json: { x: number; y: number })`: Creates a Vec2 from a JSON object
|
|
18
|
+
- `fromPolarCoords(r: number, theta: number)`: Creates a Vec2 from polar coordinates
|
|
19
|
+
- `immutable(x: number = 0, y: number = 0)`: Creates an immutable Vec2-like object
|
|
20
|
+
- `one()`: Creates a vector with all components set to 1.0
|
|
21
|
+
- `random(random: () => number = Math.random)`: Creates a random unit vector
|
|
22
|
+
- `zero()`: Creates a zero vector
|
|
23
|
+
|
|
24
|
+
### Vector Operations
|
|
25
|
+
|
|
26
|
+
- `add(v: Vec2, w: Vec2)`: Adds two vectors
|
|
27
|
+
- `subtract(v: Vec2, w: Vec2)`: Subtracts one vector from another
|
|
28
|
+
- `multiply(v: Vec2, w: Vec2)`: Multiplies two vectors component-wise
|
|
29
|
+
- `divide(v: Vec2, w: Vec2)`: Divides two vectors component-wise
|
|
30
|
+
- `dot(v: Vec2, w: Vec2)`: Calculates the dot product
|
|
31
|
+
- `cross(v: Vec2, w: Vec2)`: Calculates the cross product
|
|
32
|
+
- `scale(v: Vec2, c: number)`: Scales a vector by a scalar value
|
|
33
|
+
- `negate(v: Vec2)`: Negates a vector
|
|
34
|
+
- `normalize(v: Vec2)`: Normalizes a vector
|
|
35
|
+
- `project(v: Vec2, w: Vec2)`: Projects one vector onto another
|
|
36
|
+
- `reflect(v: Vec2, normal: Vec2)`: Reflects a vector across a normal vector
|
|
37
|
+
- `lerp(v: Vec2, w: Vec2, t: number)`: Performs linear interpolation between vectors
|
|
38
|
+
|
|
39
|
+
### Distance Calculations
|
|
40
|
+
|
|
41
|
+
- `distance(v: Vec2, w: Vec2)`: Calculates Euclidean distance between vectors
|
|
42
|
+
- `distanceSq(v: Vec2, w: Vec2)`: Calculates squared Euclidean distance
|
|
43
|
+
- `distanceChebyshev(v: Vec2, w: Vec2)`: Calculates Chebyshev distance
|
|
44
|
+
- `distanceManhattan(v: Vec2, w: Vec2)`: Calculates Manhattan distance
|
|
45
|
+
- `distanceMinkowski(v: Vec2, w: Vec2, p: number)`: Calculates Minkowski distance
|
|
46
|
+
|
|
47
|
+
### Comparison and State
|
|
48
|
+
|
|
49
|
+
- `angleBetween(v: Vec2, w: Vec2)`: Calculates angle between vectors
|
|
50
|
+
- `equals(v: Vec2, w: Vec2, epsilon?: number)`: Compares vectors with optional epsilon
|
|
51
|
+
- `isInfinite(v: Vec2)`: Checks if vector has infinite components
|
|
52
|
+
- `isNaN(v: Vec2)`: Checks if vector has NaN components
|
|
53
|
+
- `isZero(v: Vec2)`: Checks if vector is zero
|
|
54
|
+
- `satisfyEquality(v: Vec2, w: Vec2)`: Checks if vectors are equal
|
|
55
|
+
- `satisfyOpposition(v: Vec2, w: Vec2)`: Checks if vectors are opposite
|
|
56
|
+
|
|
57
|
+
## Instance Properties
|
|
58
|
+
|
|
59
|
+
### Getters and Setters
|
|
60
|
+
|
|
61
|
+
- `x`: Gets or sets the x-component
|
|
62
|
+
- `y`: Gets or sets the y-component
|
|
63
|
+
- `xy`: Gets or sets both components as an array
|
|
64
|
+
- `magnitude`: Gets or sets the vector's magnitude
|
|
65
|
+
- `magnitudeSq`: Gets the squared magnitude (read-only)
|
|
66
|
+
- `angleX`: Gets or sets the angle with positive x-axis
|
|
67
|
+
- `angleY`: Gets or sets the angle with positive y-axis
|
|
68
|
+
|
|
69
|
+
## Instance Methods
|
|
70
|
+
|
|
71
|
+
### Vector Operations
|
|
72
|
+
|
|
73
|
+
- `add(v: Vec2)`: Adds another vector
|
|
74
|
+
- `subtract(v: Vec2)`: Subtracts another vector
|
|
75
|
+
- `multiply(v: Vec2)`: Multiplies with another vector
|
|
76
|
+
- `divide(v: Vec2)`: Divides by another vector
|
|
77
|
+
- `scale(c: number)`: Scales by a scalar value
|
|
78
|
+
- `normalize()`: Normalizes the vector
|
|
79
|
+
- `negate()`: Negates the vector
|
|
80
|
+
- `project(v: Vec2)`: Projects onto another vector
|
|
81
|
+
- `reflect(normal: Vec2)`: Reflects across a normal vector
|
|
82
|
+
- `rotateZ(phi: number)`: Rotates around Z-axis
|
|
83
|
+
- `turnLeft()`: Rotates 90 degrees left
|
|
84
|
+
- `turnRight()`: Rotates 90 degrees right
|
|
85
|
+
- `zero()`: Sets to zero vector
|
|
86
|
+
- `random(random?: () => number)`: Sets to random direction
|
|
87
|
+
|
|
88
|
+
### Distance Calculations
|
|
89
|
+
|
|
90
|
+
- `distance(v: Vec2)`: Calculates Euclidean distance to another vector
|
|
91
|
+
- `distanceSq(v: Vec2)`: Calculates squared Euclidean distance
|
|
92
|
+
- `distanceChebyshev(v: Vec2)`: Calculates Chebyshev distance
|
|
93
|
+
- `distanceManhattan(v: Vec2)`: Calculates Manhattan distance
|
|
94
|
+
- `distanceMinkowski(v: Vec2, p: number)`: Calculates Minkowski distance
|
|
95
|
+
|
|
96
|
+
### Comparison and State
|
|
97
|
+
|
|
98
|
+
- `angleBetween(v: Vec2)`: Calculates angle to another vector
|
|
99
|
+
- `equals(v: Vec2, epsilon?: number)`: Compares with another vector
|
|
100
|
+
- `isInfinite()`: Checks for infinite components
|
|
101
|
+
- `isNaN()`: Checks for NaN components
|
|
102
|
+
- `isZero()`: Checks if zero vector
|
|
103
|
+
- `satisfyEquality(v: Vec2)`: Checks equality with another vector
|
|
104
|
+
- `satisfyOpposition(v: Vec2)`: Checks opposition with another vector
|
|
105
|
+
|
|
106
|
+
### Magnitude Operations
|
|
107
|
+
|
|
108
|
+
- `clamp(min: number, max: number)`: Clamps magnitude between values
|
|
109
|
+
- `limitMax(max: number)`: Limits maximum magnitude
|
|
110
|
+
- `limitMin(min: number)`: Limits minimum magnitude
|
|
111
|
+
|
|
112
|
+
### Utility Methods
|
|
113
|
+
|
|
114
|
+
- `clone()`: Creates a copy of the vector
|
|
115
|
+
- `copy(v: Vec2)`: Copies components from another vector
|
|
116
|
+
- `lookAt(v: Vec2)`: Points towards another vector
|
|
117
|
+
- `toString()`: Returns string representation
|
|
118
|
+
- `toObject()`: Converts to plain object
|
|
119
|
+
- `toJSON()`: Converts to JSON format
|
|
120
|
+
|
|
121
|
+
## Iteration
|
|
122
|
+
|
|
123
|
+
Vec2 implements the iterator protocol, allowing it to be used with for...of loops and spread operators. The iterator yields the x and y components in order.
|
|
124
|
+
|
|
125
|
+
## Example Usage
|
|
126
|
+
|
|
127
|
+
```typescript
|
|
128
|
+
// Create vectors
|
|
129
|
+
const v1 = new Vec2(3, 4);
|
|
130
|
+
const v2 = new Vec2(1, 2);
|
|
131
|
+
|
|
132
|
+
// Basic operations
|
|
133
|
+
const sum = Vec2.add(v1, v2);
|
|
134
|
+
const dot = Vec2.dot(v1, v2);
|
|
135
|
+
|
|
136
|
+
// Method chaining
|
|
137
|
+
v1.normalize()
|
|
138
|
+
.scale(2)
|
|
139
|
+
.rotate(Math.PI / 4);
|
|
140
|
+
|
|
141
|
+
// Get properties
|
|
142
|
+
console.log(v1.magnitude);
|
|
143
|
+
console.log(v1.angleX);
|
|
144
|
+
|
|
145
|
+
// Convert to array
|
|
146
|
+
const [x, y] = v1;
|
|
147
|
+
```
|
package/VEC3.md
ADDED
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
# Vec3
|
|
2
|
+
|
|
3
|
+
A 3D vector implementation with comprehensive mathematical operations and utility methods.
|
|
4
|
+
|
|
5
|
+
## Constructor
|
|
6
|
+
|
|
7
|
+
### Vec3
|
|
8
|
+
|
|
9
|
+
- `constructor(x: number = 0, y: number = 0, z: number = 0)`: Creates a new vector with the given x, y and z coordinates
|
|
10
|
+
|
|
11
|
+
## Static Methods
|
|
12
|
+
|
|
13
|
+
### Creation and Conversion
|
|
14
|
+
|
|
15
|
+
- `fromArray(arr: [number, number, number] | number[])`: Creates a Vec3 from an array
|
|
16
|
+
- `fromObject(obj: { x: number; y: number; z: number })`: Creates a Vec3 from an object with x, y and z properties
|
|
17
|
+
- `fromJSON(json: { x: number; y: number; z: number })`: Creates a Vec3 from a JSON object
|
|
18
|
+
- `fromCylindricalCoords(r: number, phi: number, z: number)`: Creates a Vec3 from cylindrical coordinates
|
|
19
|
+
- `fromSphericalCoords(r: number, theta: number, phi: number)`: Creates a Vec3 from spherical coordinates
|
|
20
|
+
- `immutable(x: number = 0, y: number = 0, z: number = 0)`: Creates an immutable Vec3-like object
|
|
21
|
+
- `one()`: Creates a vector with all components set to 1.0
|
|
22
|
+
- `random(random: () => number = Math.random)`: Creates a random unit vector
|
|
23
|
+
- `zero()`: Creates a zero vector
|
|
24
|
+
|
|
25
|
+
### Vector Operations
|
|
26
|
+
|
|
27
|
+
- `add(v: Vec3, w: Vec3)`: Adds two vectors
|
|
28
|
+
- `subtract(v: Vec3, w: Vec3)`: Subtracts one vector from another
|
|
29
|
+
- `multiply(v: Vec3, w: Vec3)`: Multiplies two vectors component-wise
|
|
30
|
+
- `divide(v: Vec3, w: Vec3)`: Divides two vectors component-wise
|
|
31
|
+
- `dot(v: Vec3, w: Vec3)`: Calculates the dot product
|
|
32
|
+
- `cross(v: Vec3, w: Vec3)`: Calculates the cross product
|
|
33
|
+
- `scale(v: Vec3, c: number)`: Scales a vector by a scalar value
|
|
34
|
+
- `negate(v: Vec3)`: Negates a vector
|
|
35
|
+
- `normalize(v: Vec3)`: Normalizes a vector
|
|
36
|
+
- `project(v: Vec3, w: Vec3)`: Projects one vector onto another
|
|
37
|
+
- `reflect(v: Vec3, normal: Vec3)`: Reflects a vector across a normal vector
|
|
38
|
+
- `lerp(v: Vec3, w: Vec3, t: number)`: Performs linear interpolation between vectors
|
|
39
|
+
|
|
40
|
+
### Distance Calculations
|
|
41
|
+
|
|
42
|
+
- `distance(v: Vec3, w: Vec3)`: Calculates Euclidean distance between vectors
|
|
43
|
+
- `distanceSq(v: Vec3, w: Vec3)`: Calculates squared Euclidean distance
|
|
44
|
+
- `distanceChebyshev(v: Vec3, w: Vec3)`: Calculates Chebyshev distance
|
|
45
|
+
- `distanceManhattan(v: Vec3, w: Vec3)`: Calculates Manhattan distance
|
|
46
|
+
- `distanceMinkowski(v: Vec3, w: Vec3, p: number)`: Calculates Minkowski distance
|
|
47
|
+
|
|
48
|
+
### Comparison and State
|
|
49
|
+
|
|
50
|
+
- `angleBetween(v: Vec3, w: Vec3)`: Calculates angle between vectors
|
|
51
|
+
- `equals(v: Vec3, w: Vec3, epsilon?: number)`: Compares vectors with optional epsilon
|
|
52
|
+
- `isInfinite(v: Vec3)`: Checks if vector has infinite components
|
|
53
|
+
- `isNaN(v: Vec3)`: Checks if vector has NaN components
|
|
54
|
+
- `isZero(v: Vec3)`: Checks if vector is zero
|
|
55
|
+
- `satisfyEquality(v: Vec3, w: Vec3)`: Checks if vectors are equal
|
|
56
|
+
- `satisfyOpposition(v: Vec3, w: Vec3)`: Checks if vectors are opposite
|
|
57
|
+
|
|
58
|
+
## Instance Properties
|
|
59
|
+
|
|
60
|
+
### Getters and Setters
|
|
61
|
+
|
|
62
|
+
- `x`: Gets or sets the x-component
|
|
63
|
+
- `y`: Gets or sets the y-component
|
|
64
|
+
- `z`: Gets or sets the z-component
|
|
65
|
+
- `xyz`: Gets or sets all components as an array
|
|
66
|
+
- `magnitude`: Gets or sets the vector's magnitude
|
|
67
|
+
- `magnitudeSq`: Gets the squared magnitude (read-only)
|
|
68
|
+
- `angleX`: Gets the angle with positive x-axis
|
|
69
|
+
- `angleY`: Gets the angle with positive y-axis
|
|
70
|
+
- `angleZ`: Gets the angle with positive z-axis
|
|
71
|
+
|
|
72
|
+
## Instance Methods
|
|
73
|
+
|
|
74
|
+
### Vector Operations
|
|
75
|
+
|
|
76
|
+
- `add(v: Vec3)`: Adds another vector
|
|
77
|
+
- `subtract(v: Vec3)`: Subtracts another vector
|
|
78
|
+
- `multiply(v: Vec3)`: Multiplies with another vector
|
|
79
|
+
- `divide(v: Vec3)`: Divides by another vector
|
|
80
|
+
- `scale(c: number)`: Scales by a scalar value
|
|
81
|
+
- `normalize()`: Normalizes the vector
|
|
82
|
+
- `negate()`: Negates the vector
|
|
83
|
+
- `project(v: Vec3)`: Projects onto another vector
|
|
84
|
+
- `reflect(normal: Vec3)`: Reflects across a normal vector
|
|
85
|
+
- `rotateX(phi: number)`: Rotates around X-axis
|
|
86
|
+
- `rotateY(phi: number)`: Rotates around Y-axis
|
|
87
|
+
- `rotateZ(phi: number)`: Rotates around Z-axis
|
|
88
|
+
- `zero()`: Sets to zero vector
|
|
89
|
+
- `random(random?: () => number)`: Sets to random direction
|
|
90
|
+
|
|
91
|
+
### Distance Calculations
|
|
92
|
+
|
|
93
|
+
- `distance(v: Vec3)`: Calculates Euclidean distance to another vector
|
|
94
|
+
- `distanceSq(v: Vec3)`: Calculates squared Euclidean distance
|
|
95
|
+
- `distanceChebyshev(v: Vec3)`: Calculates Chebyshev distance
|
|
96
|
+
- `distanceManhattan(v: Vec3)`: Calculates Manhattan distance
|
|
97
|
+
- `distanceMinkowski(v: Vec3, p: number)`: Calculates Minkowski distance
|
|
98
|
+
|
|
99
|
+
### Cross Product
|
|
100
|
+
|
|
101
|
+
- `cross(v: Vec3)`: Calculates the cross product with another vector
|
|
102
|
+
|
|
103
|
+
### Comparison and State
|
|
104
|
+
|
|
105
|
+
- `angleBetween(v: Vec3)`: Calculates angle to another vector
|
|
106
|
+
- `equals(v: Vec3, epsilon?: number)`: Compares with another vector
|
|
107
|
+
- `isInfinite()`: Checks for infinite components
|
|
108
|
+
- `isNaN()`: Checks for NaN components
|
|
109
|
+
- `isZero()`: Checks if zero vector
|
|
110
|
+
- `satisfyEquality(v: Vec3)`: Checks equality with another vector
|
|
111
|
+
- `satisfyOpposition(v: Vec3)`: Checks opposition with another vector
|
|
112
|
+
|
|
113
|
+
### Magnitude Operations
|
|
114
|
+
|
|
115
|
+
- `clamp(min: number, max: number)`: Clamps magnitude between values
|
|
116
|
+
- `limitMax(max: number)`: Limits maximum magnitude
|
|
117
|
+
- `limitMin(min: number)`: Limits minimum magnitude
|
|
118
|
+
|
|
119
|
+
### Utility Methods
|
|
120
|
+
|
|
121
|
+
- `clone()`: Creates a copy of the vector
|
|
122
|
+
- `copy(v: Vec3)`: Copies components from another vector
|
|
123
|
+
- `lookAt(v: Vec3)`: Points towards another vector
|
|
124
|
+
- `toString()`: Returns string representation
|
|
125
|
+
- `toObject()`: Converts to plain object
|
|
126
|
+
- `toJSON()`: Converts to JSON format
|
|
127
|
+
|
|
128
|
+
## Iteration
|
|
129
|
+
|
|
130
|
+
Vec3 implements the iterator protocol, allowing it to be used with for...of loops and spread operators. The iterator yields the x, y, and z components in order.
|
|
131
|
+
|
|
132
|
+
## Example Usage
|
|
133
|
+
|
|
134
|
+
```typescript
|
|
135
|
+
// Create vectors
|
|
136
|
+
const v1 = new Vec3(3, 4, 5);
|
|
137
|
+
const v2 = new Vec3(1, 2, 3);
|
|
138
|
+
|
|
139
|
+
// Basic operations
|
|
140
|
+
const sum = Vec3.add(v1, v2);
|
|
141
|
+
const crossProduct = Vec3.cross(v1, v2);
|
|
142
|
+
const dot = Vec3.dot(v1, v2);
|
|
143
|
+
|
|
144
|
+
// Method chaining
|
|
145
|
+
v1.normalize()
|
|
146
|
+
.scale(2)
|
|
147
|
+
.rotateX(Math.PI / 4);
|
|
148
|
+
|
|
149
|
+
// Get properties
|
|
150
|
+
console.log(v1.magnitude);
|
|
151
|
+
console.log(v1.angleX);
|
|
152
|
+
|
|
153
|
+
// Convert to array
|
|
154
|
+
const [x, y, z] = v1;
|
|
155
|
+
|
|
156
|
+
// Create from special coordinates
|
|
157
|
+
const cylindrical = Vec3.fromCylindricalCoords(2, Math.PI/4, 5);
|
|
158
|
+
const spherical = Vec3.fromSphericalCoords(2, Math.PI/3, Math.PI/4);
|
|
159
|
+
```
|
package/VEC4.md
ADDED
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
# Vec4
|
|
2
|
+
|
|
3
|
+
A 4D vector implementation with comprehensive mathematical operations and utility methods.
|
|
4
|
+
|
|
5
|
+
## Constructor
|
|
6
|
+
|
|
7
|
+
### Vec4
|
|
8
|
+
|
|
9
|
+
- `constructor(x: number = 0, y: number = 0, z: number = 0, w: number = 0)`: Creates a new vector with the given x, y, z, and w coordinates
|
|
10
|
+
|
|
11
|
+
## Static Methods
|
|
12
|
+
|
|
13
|
+
### Creation and Conversion
|
|
14
|
+
|
|
15
|
+
- `fromArray(arr: [number, number, number, number] | number[])`: Creates a Vec4 from an array
|
|
16
|
+
- `fromObject(obj: { x: number; y: number; z: number; w: number })`: Creates a Vec4 from an object with x, y, z, and w properties
|
|
17
|
+
- `fromJSON(json: { x: number; y: number; z: number; w: number })`: Creates a Vec4 from a JSON object
|
|
18
|
+
- `immutable(x: number = 0, y: number = 0, z: number = 0, w: number = 0)`: Creates an immutable Vec4-like object
|
|
19
|
+
- `one()`: Creates a vector with all components set to 1.0
|
|
20
|
+
- `random(random: () => number = Math.random)`: Creates a random unit vector
|
|
21
|
+
- `zero()`: Creates a zero vector
|
|
22
|
+
|
|
23
|
+
### Vector Operations
|
|
24
|
+
|
|
25
|
+
- `add(v: Vec4, w: Vec4)`: Adds two vectors
|
|
26
|
+
- `subtract(v: Vec4, w: Vec4)`: Subtracts one vector from another
|
|
27
|
+
- `multiply(v: Vec4, w: Vec4)`: Multiplies two vectors component-wise
|
|
28
|
+
- `divide(v: Vec4, w: Vec4)`: Divides two vectors component-wise
|
|
29
|
+
- `dot(v: Vec4, w: Vec4)`: Calculates the dot product
|
|
30
|
+
- `scale(v: Vec4, c: number)`: Scales a vector by a scalar value
|
|
31
|
+
- `negate(v: Vec4)`: Negates a vector
|
|
32
|
+
- `normalize(v: Vec4)`: Normalizes a vector
|
|
33
|
+
- `project(v: Vec4, w: Vec4)`: Projects one vector onto another
|
|
34
|
+
- `reflect(v: Vec4, normal: Vec4)`: Reflects a vector across a normal vector
|
|
35
|
+
- `lerp(v: Vec4, w: Vec4, t: number)`: Performs linear interpolation between vectors
|
|
36
|
+
|
|
37
|
+
### Distance Calculations
|
|
38
|
+
|
|
39
|
+
- `distance(v: Vec4, w: Vec4)`: Calculates Euclidean distance between vectors
|
|
40
|
+
- `distanceSq(v: Vec4, w: Vec4)`: Calculates squared Euclidean distance
|
|
41
|
+
- `distanceChebyshev(v: Vec4, w: Vec4)`: Calculates Chebyshev distance
|
|
42
|
+
- `distanceManhattan(v: Vec4, w: Vec4)`: Calculates Manhattan distance
|
|
43
|
+
- `distanceMinkowski(v: Vec4, w: Vec4, p: number)`: Calculates Minkowski distance
|
|
44
|
+
|
|
45
|
+
### Comparison and State
|
|
46
|
+
|
|
47
|
+
- `angleBetween(v: Vec4, w: Vec4)`: Calculates angle between vectors
|
|
48
|
+
- `equals(v: Vec4, w: Vec4, epsilon?: number)`: Compares vectors with optional epsilon
|
|
49
|
+
- `isInfinite(v: Vec4)`: Checks if vector has infinite components
|
|
50
|
+
- `isNaN(v: Vec4)`: Checks if vector has NaN components
|
|
51
|
+
- `isZero(v: Vec4)`: Checks if vector is zero
|
|
52
|
+
- `satisfyEquality(v: Vec4, w: Vec4)`: Checks if vectors are equal
|
|
53
|
+
- `satisfyOpposition(v: Vec4, w: Vec4)`: Checks if vectors are opposite
|
|
54
|
+
|
|
55
|
+
## Instance Properties
|
|
56
|
+
|
|
57
|
+
### Getters and Setters
|
|
58
|
+
|
|
59
|
+
- `x`: Gets or sets the x-component
|
|
60
|
+
- `y`: Gets or sets the y-component
|
|
61
|
+
- `z`: Gets or sets the z-component
|
|
62
|
+
- `w`: Gets or sets the w-component
|
|
63
|
+
- `xyzw`: Gets or sets all components as an array
|
|
64
|
+
- `magnitude`: Gets or sets the vector's magnitude
|
|
65
|
+
- `magnitudeSq`: Gets the squared magnitude (read-only)
|
|
66
|
+
- `angleX`: Gets the angle with positive x-axis
|
|
67
|
+
- `angleY`: Gets the angle with positive y-axis
|
|
68
|
+
- `angleZ`: Gets the angle with positive z-axis
|
|
69
|
+
- `angleW`: Gets the angle with positive w-axis
|
|
70
|
+
|
|
71
|
+
## Instance Methods
|
|
72
|
+
|
|
73
|
+
### Vector Operations
|
|
74
|
+
|
|
75
|
+
- `add(v: Vec4)`: Adds another vector
|
|
76
|
+
- `subtract(v: Vec4)`: Subtracts another vector
|
|
77
|
+
- `multiply(v: Vec4)`: Multiplies with another vector
|
|
78
|
+
- `divide(v: Vec4)`: Divides by another vector
|
|
79
|
+
- `scale(c: number)`: Scales by a scalar value
|
|
80
|
+
- `normalize()`: Normalizes the vector
|
|
81
|
+
- `negate()`: Negates the vector
|
|
82
|
+
- `project(v: Vec4)`: Projects onto another vector
|
|
83
|
+
- `reflect(normal: Vec4)`: Reflects across a normal vector
|
|
84
|
+
- `zero()`: Sets to zero vector
|
|
85
|
+
- `random(random?: () => number)`: Sets to random direction
|
|
86
|
+
|
|
87
|
+
### Distance Calculations
|
|
88
|
+
|
|
89
|
+
- `distance(v: Vec4)`: Calculates Euclidean distance to another vector
|
|
90
|
+
- `distanceSq(v: Vec4)`: Calculates squared Euclidean distance
|
|
91
|
+
- `distanceChebyshev(v: Vec4)`: Calculates Chebyshev distance
|
|
92
|
+
- `distanceManhattan(v: Vec4)`: Calculates Manhattan distance
|
|
93
|
+
- `distanceMinkowski(v: Vec4, p: number)`: Calculates Minkowski distance
|
|
94
|
+
|
|
95
|
+
### Comparison and State
|
|
96
|
+
|
|
97
|
+
- `angleBetween(v: Vec4)`: Calculates angle to another vector
|
|
98
|
+
- `equals(v: Vec4, epsilon?: number)`: Compares with another vector
|
|
99
|
+
- `isInfinite()`: Checks for infinite components
|
|
100
|
+
- `isNaN()`: Checks for NaN components
|
|
101
|
+
- `isZero()`: Checks if zero vector
|
|
102
|
+
- `satisfyEquality(v: Vec4)`: Checks equality with another vector
|
|
103
|
+
- `satisfyOpposition(v: Vec4)`: Checks opposition with another vector
|
|
104
|
+
|
|
105
|
+
### Magnitude Operations
|
|
106
|
+
|
|
107
|
+
- `clamp(min: number, max: number)`: Clamps magnitude between values
|
|
108
|
+
- `limitMax(max: number)`: Limits maximum magnitude
|
|
109
|
+
- `limitMin(min: number)`: Limits minimum magnitude
|
|
110
|
+
|
|
111
|
+
### Utility Methods
|
|
112
|
+
|
|
113
|
+
- `clone()`: Creates a copy of the vector
|
|
114
|
+
- `copy(v: Vec4)`: Copies components from another vector
|
|
115
|
+
- `lookAt(v: Vec4)`: Points towards another vector
|
|
116
|
+
- `toString()`: Returns string representation
|
|
117
|
+
- `toObject()`: Converts to plain object
|
|
118
|
+
- `toJSON()`: Converts to JSON format
|
|
119
|
+
|
|
120
|
+
## Iteration
|
|
121
|
+
|
|
122
|
+
Vec4 implements the iterator protocol, allowing it to be used with for...of loops and spread operators. The iterator yields the x, y, z, and w components in order.
|
|
123
|
+
|
|
124
|
+
## Example Usage
|
|
125
|
+
|
|
126
|
+
```typescript
|
|
127
|
+
// Create vectors
|
|
128
|
+
const v1 = new Vec4(1, 2, 3, 1); // Homogeneous coordinates
|
|
129
|
+
const v2 = new Vec4(0, 1, 0, 0); // Direction vector
|
|
130
|
+
|
|
131
|
+
// Basic operations
|
|
132
|
+
const sum = Vec4.add(v1, v2);
|
|
133
|
+
const dot = Vec4.dot(v1, v2);
|
|
134
|
+
|
|
135
|
+
// Method chaining
|
|
136
|
+
v1.normalize()
|
|
137
|
+
.scale(2)
|
|
138
|
+
.limitMax(5);
|
|
139
|
+
|
|
140
|
+
// Get properties
|
|
141
|
+
console.log(v1.magnitude);
|
|
142
|
+
console.log(v1.angleX);
|
|
143
|
+
|
|
144
|
+
// Convert to array
|
|
145
|
+
const [x, y, z, w] = v1;
|
|
146
|
+
```
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@fimbul-works/vec",
|
|
3
|
-
"version": "1.0.
|
|
4
|
-
"description": "A
|
|
3
|
+
"version": "1.0.3",
|
|
4
|
+
"description": "A comprehensive TypeScript vector math library providing 2D, 3D, and 4D vector operations with a focus on performance and type safety.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"private": false,
|
|
7
7
|
"license": "MIT",
|
|
@@ -31,7 +31,7 @@
|
|
|
31
31
|
"main": "./dist/index.js",
|
|
32
32
|
"module": "./dist/index.js",
|
|
33
33
|
"types": "./dist/index.d.ts",
|
|
34
|
-
"files": ["dist", "README.md", "LICENSE"],
|
|
34
|
+
"files": ["dist", "README.md", "VEC2.md", "VEC3.md", "VEC4.md", "LICENSE"],
|
|
35
35
|
"sideEffects": false,
|
|
36
36
|
"scripts": {
|
|
37
37
|
"dev": "tsc --watch",
|