@benev/math 0.2.0-4 → 0.3.0-2
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/LICENSE +1 -1
- package/README.md +3 -3
- package/package.json +5 -5
- package/s/{tools → core}/circular.ts +1 -1
- package/s/core/quat.ts +12 -8
- package/s/{tools → core}/scalar.ts +2 -2
- package/s/core/vec2.ts +24 -20
- package/s/core/vec3.ts +24 -20
- package/s/core/vec4.ts +6 -2
- package/s/index.ts +5 -10
- package/s/optimizers/hash-map.ts +2 -2
- package/s/optimizers/zen.ts +4 -4
- package/s/physics/2d/collide2d.barrel.ts +3 -0
- package/s/physics/2d/collide2d.ts +2 -2
- package/s/physics/2d/intersect2d.barrel.ts +3 -0
- package/s/physics/2d/intersect2d.ts +11 -11
- package/s/physics/index.ts +4 -0
- package/s/shapes/2d/circle.ts +3 -3
- package/s/shapes/2d/rect.ts +7 -7
- package/s/shapes/3d/box.ts +10 -10
- package/s/shapes/3d/segment.ts +8 -8
- package/s/shapes/index.ts +7 -0
- package/s/tools/angles.ts +26 -26
- package/s/tools/make-noise.ts +13 -0
- package/s/tools/spline.ts +30 -36
- package/x/concepts/angles.d.ts +29 -0
- package/x/concepts/angles.js +62 -0
- package/x/concepts/angles.js.map +1 -0
- package/x/concepts/circular.d.ts +17 -0
- package/x/concepts/circular.js +70 -0
- package/x/concepts/circular.js.map +1 -0
- package/x/concepts/noise.d.ts +9 -0
- package/x/concepts/noise.js +20 -0
- package/x/concepts/noise.js.map +1 -0
- package/x/concepts/quat.d.ts +35 -0
- package/x/concepts/quat.js +110 -0
- package/x/concepts/quat.js.map +1 -0
- package/x/concepts/randy.d.ts +39 -0
- package/x/concepts/randy.js +95 -0
- package/x/concepts/randy.js.map +1 -0
- package/x/concepts/scalar.d.ts +51 -0
- package/x/concepts/scalar.js +219 -0
- package/x/concepts/scalar.js.map +1 -0
- package/x/concepts/spline.d.ts +9 -0
- package/x/concepts/spline.js +59 -0
- package/x/concepts/spline.js.map +1 -0
- package/x/concepts/vec2.d.ts +114 -0
- package/x/concepts/vec2.js +314 -0
- package/x/concepts/vec2.js.map +1 -0
- package/x/concepts/vec3.d.ts +117 -0
- package/x/concepts/vec3.js +357 -0
- package/x/concepts/vec3.js.map +1 -0
- package/x/concepts/vec4.d.ts +21 -0
- package/x/concepts/vec4.js +62 -0
- package/x/concepts/vec4.js.map +1 -0
- package/x/core/circular.d.ts +17 -0
- package/x/core/circular.js +71 -0
- package/x/core/circular.js.map +1 -0
- package/x/core/quat.d.ts +4 -3
- package/x/core/quat.js +11 -8
- package/x/core/quat.js.map +1 -1
- package/x/core/scalar.d.ts +51 -0
- package/x/core/scalar.js +219 -0
- package/x/core/scalar.js.map +1 -0
- package/x/core/vec2.d.ts +11 -10
- package/x/core/vec2.js +23 -20
- package/x/core/vec2.js.map +1 -1
- package/x/core/vec3.d.ts +11 -10
- package/x/core/vec3.js +23 -20
- package/x/core/vec3.js.map +1 -1
- package/x/core/vec4.d.ts +2 -1
- package/x/core/vec4.js +5 -2
- package/x/core/vec4.js.map +1 -1
- package/x/helpers/angles.d.ts +19 -0
- package/x/helpers/angles.js +41 -0
- package/x/helpers/angles.js.map +1 -0
- package/x/helpers/circular.d.ts +17 -0
- package/x/helpers/circular.js +71 -0
- package/x/helpers/circular.js.map +1 -0
- package/x/helpers/noise.d.ts +9 -0
- package/x/helpers/noise.js +20 -0
- package/x/helpers/noise.js.map +1 -0
- package/x/helpers/randy.d.ts +31 -0
- package/x/helpers/randy.js +85 -0
- package/x/helpers/randy.js.map +1 -0
- package/x/helpers/scalar.d.ts +51 -0
- package/x/helpers/scalar.js +219 -0
- package/x/helpers/scalar.js.map +1 -0
- package/x/helpers/spline.d.ts +9 -0
- package/x/helpers/spline.js +61 -0
- package/x/helpers/spline.js.map +1 -0
- package/x/importmap.json +7 -0
- package/x/index.d.ts +4 -10
- package/x/index.js +4 -10
- package/x/index.js.map +1 -1
- package/x/optimizers/hash-map.js +2 -2
- package/x/optimizers/zen.js +4 -4
- package/x/optimizers/zen.js.map +1 -1
- package/x/physics/2d/collide2d.barrel.d.ts +1 -0
- package/x/physics/2d/collide2d.barrel.js +2 -0
- package/x/physics/2d/collide2d.barrel.js.map +1 -0
- package/x/physics/2d/collide2d.js +2 -2
- package/x/physics/2d/collide2d.js.map +1 -1
- package/x/physics/2d/intersect2d.barrel.d.ts +1 -0
- package/x/physics/2d/intersect2d.barrel.js +2 -0
- package/x/physics/2d/intersect2d.barrel.js.map +1 -0
- package/x/physics/2d/intersect2d.d.ts +3 -3
- package/x/physics/2d/intersect2d.js +11 -11
- package/x/physics/2d/intersect2d.js.map +1 -1
- package/x/physics/index.d.ts +2 -0
- package/x/physics/index.js +3 -0
- package/x/physics/index.js.map +1 -0
- package/x/primitives/circular.d.ts +17 -0
- package/x/primitives/circular.js +70 -0
- package/x/primitives/circular.js.map +1 -0
- package/x/primitives/quat.d.ts +35 -0
- package/x/primitives/quat.js +110 -0
- package/x/primitives/quat.js.map +1 -0
- package/x/primitives/scalar.d.ts +51 -0
- package/x/primitives/scalar.js +219 -0
- package/x/primitives/scalar.js.map +1 -0
- package/x/primitives/vec2.d.ts +114 -0
- package/x/primitives/vec2.js +319 -0
- package/x/primitives/vec2.js.map +1 -0
- package/x/primitives/vec3.d.ts +117 -0
- package/x/primitives/vec3.js +363 -0
- package/x/primitives/vec3.js.map +1 -0
- package/x/primitives/vec4.d.ts +21 -0
- package/x/primitives/vec4.js +62 -0
- package/x/primitives/vec4.js.map +1 -0
- package/x/shapes/2d/circle.js +3 -3
- package/x/shapes/2d/circle.js.map +1 -1
- package/x/shapes/2d/rect.js +7 -7
- package/x/shapes/2d/rect.js.map +1 -1
- package/x/shapes/3d/box.js +10 -10
- package/x/shapes/3d/box.js.map +1 -1
- package/x/shapes/3d/segment.js +7 -7
- package/x/shapes/3d/segment.js.map +1 -1
- package/x/shapes/index.d.ts +4 -0
- package/x/shapes/index.js +5 -0
- package/x/shapes/index.js.map +1 -0
- package/x/tools/angles.d.ts +22 -13
- package/x/tools/angles.js +27 -29
- package/x/tools/angles.js.map +1 -1
- package/x/tools/spline.d.ts +2 -4
- package/x/tools/spline.js +20 -26
- package/x/tools/spline.js.map +1 -1
- package/x/utils/angles.d.ts +19 -0
- package/x/utils/angles.js +41 -0
- package/x/utils/angles.js.map +1 -0
- package/x/utils/circular.d.ts +17 -0
- package/x/utils/circular.js +70 -0
- package/x/utils/circular.js.map +1 -0
- package/x/utils/noise.d.ts +9 -0
- package/x/utils/noise.js +20 -0
- package/x/utils/noise.js.map +1 -0
- package/x/utils/randy.d.ts +31 -0
- package/x/utils/randy.js +85 -0
- package/x/utils/randy.js.map +1 -0
- package/x/utils/scalar.d.ts +51 -0
- package/x/utils/scalar.js +219 -0
- package/x/utils/scalar.js.map +1 -0
- package/x/utils/spline.d.ts +9 -0
- package/x/utils/spline.js +61 -0
- package/x/utils/spline.js.map +1 -0
package/s/shapes/2d/rect.ts
CHANGED
|
@@ -18,19 +18,19 @@ export class Rect {
|
|
|
18
18
|
}
|
|
19
19
|
|
|
20
20
|
static fromCorner(min: Vec2, size: Vec2) {
|
|
21
|
-
const max = min.
|
|
21
|
+
const max = min.dup().add(size)
|
|
22
22
|
return new this(min, max)
|
|
23
23
|
}
|
|
24
24
|
|
|
25
25
|
static fromCenter(center: Vec2, size: Vec2) {
|
|
26
|
-
const halfSize = size.
|
|
27
|
-
const min = center.
|
|
28
|
-
const max = center.
|
|
26
|
+
const halfSize = size.dup().half()
|
|
27
|
+
const min = center.dup().sub(halfSize)
|
|
28
|
+
const max = center.dup().add(halfSize)
|
|
29
29
|
return new this(min, max)
|
|
30
30
|
}
|
|
31
31
|
|
|
32
32
|
clone() {
|
|
33
|
-
return new Rect(this.min.
|
|
33
|
+
return new Rect(this.min.dup(), this.max.dup())
|
|
34
34
|
}
|
|
35
35
|
|
|
36
36
|
toJSON(): RectJson {
|
|
@@ -50,11 +50,11 @@ export class Rect {
|
|
|
50
50
|
}
|
|
51
51
|
|
|
52
52
|
size() {
|
|
53
|
-
return this.max.
|
|
53
|
+
return this.max.dup().sub(this.min)
|
|
54
54
|
}
|
|
55
55
|
|
|
56
56
|
center() {
|
|
57
|
-
return this.min.
|
|
57
|
+
return this.min.dup().add(this.size().half())
|
|
58
58
|
}
|
|
59
59
|
|
|
60
60
|
translate(delta: Vec2) {
|
package/s/shapes/3d/box.ts
CHANGED
|
@@ -17,13 +17,13 @@ export class Box {
|
|
|
17
17
|
}
|
|
18
18
|
|
|
19
19
|
static fromCorner(min: Vec3, size: Vec3) {
|
|
20
|
-
return new this(min, min.
|
|
20
|
+
return new this(min, min.dup().add(size))
|
|
21
21
|
}
|
|
22
22
|
|
|
23
23
|
static fromCenter(center: Vec3, size: Vec3) {
|
|
24
|
-
const halfSize = size.
|
|
25
|
-
const min = center.
|
|
26
|
-
const max = center.
|
|
24
|
+
const halfSize = size.dup().half()
|
|
25
|
+
const min = center.dup().sub(halfSize)
|
|
26
|
+
const max = center.dup().add(halfSize)
|
|
27
27
|
return new this(min, max)
|
|
28
28
|
}
|
|
29
29
|
|
|
@@ -32,7 +32,7 @@ export class Box {
|
|
|
32
32
|
}
|
|
33
33
|
|
|
34
34
|
clone() {
|
|
35
|
-
return new Box(this.min.
|
|
35
|
+
return new Box(this.min.dup(), this.max.dup())
|
|
36
36
|
}
|
|
37
37
|
|
|
38
38
|
set(box: BoxLike) {
|
|
@@ -41,11 +41,11 @@ export class Box {
|
|
|
41
41
|
}
|
|
42
42
|
|
|
43
43
|
size() {
|
|
44
|
-
return this.max.
|
|
44
|
+
return this.max.dup().sub(this.min)
|
|
45
45
|
}
|
|
46
46
|
|
|
47
47
|
center() {
|
|
48
|
-
return this.min.
|
|
48
|
+
return this.min.dup().add(this.size().half())
|
|
49
49
|
}
|
|
50
50
|
|
|
51
51
|
normalize() {
|
|
@@ -68,15 +68,15 @@ export class Box {
|
|
|
68
68
|
}
|
|
69
69
|
|
|
70
70
|
grow(increase: Vec3) {
|
|
71
|
-
const halfIncrease = increase.
|
|
72
|
-
this.min.
|
|
71
|
+
const halfIncrease = increase.dup().half()
|
|
72
|
+
this.min.sub(halfIncrease)
|
|
73
73
|
this.max.add(halfIncrease)
|
|
74
74
|
return this
|
|
75
75
|
}
|
|
76
76
|
|
|
77
77
|
growBy(increase: number) {
|
|
78
78
|
const halfIncrease = increase / 2
|
|
79
|
-
this.min.
|
|
79
|
+
this.min.subBy(halfIncrease)
|
|
80
80
|
this.max.addBy(halfIncrease)
|
|
81
81
|
return this
|
|
82
82
|
}
|
package/s/shapes/3d/segment.ts
CHANGED
|
@@ -8,7 +8,7 @@ export class Segment {
|
|
|
8
8
|
) {}
|
|
9
9
|
|
|
10
10
|
vector() {
|
|
11
|
-
return this.end.
|
|
11
|
+
return this.end.dup().sub(this.start)
|
|
12
12
|
}
|
|
13
13
|
|
|
14
14
|
length() {
|
|
@@ -16,31 +16,31 @@ export class Segment {
|
|
|
16
16
|
}
|
|
17
17
|
|
|
18
18
|
center() {
|
|
19
|
-
return this.start.
|
|
19
|
+
return this.start.dup()
|
|
20
20
|
.add(this.end)
|
|
21
21
|
.half()
|
|
22
22
|
}
|
|
23
23
|
|
|
24
24
|
clone() {
|
|
25
25
|
return new Segment(
|
|
26
|
-
this.start.
|
|
27
|
-
this.end.
|
|
26
|
+
this.start.dup(),
|
|
27
|
+
this.end.dup(),
|
|
28
28
|
)
|
|
29
29
|
}
|
|
30
30
|
|
|
31
31
|
fromStart(length: number) {
|
|
32
32
|
const direction = this.vector().normalize()
|
|
33
|
-
return this.start.
|
|
33
|
+
return this.start.dup().add(direction.mulBy(length))
|
|
34
34
|
}
|
|
35
35
|
|
|
36
36
|
point(fraction: number) {
|
|
37
|
-
return this.start.
|
|
37
|
+
return this.start.dup().add(this.vector().mulBy(fraction))
|
|
38
38
|
}
|
|
39
39
|
|
|
40
40
|
scale(fraction: number) {
|
|
41
41
|
const {center} = this
|
|
42
|
-
const newHalfVector = this.vector().
|
|
43
|
-
this.start.set(center().
|
|
42
|
+
const newHalfVector = this.vector().mulBy(fraction / 2)
|
|
43
|
+
this.start.set(center().sub(newHalfVector))
|
|
44
44
|
this.end.set(center().add(newHalfVector))
|
|
45
45
|
return this
|
|
46
46
|
}
|
package/s/tools/angles.ts
CHANGED
|
@@ -1,49 +1,49 @@
|
|
|
1
1
|
|
|
2
|
-
import {Scalar} from "
|
|
2
|
+
import {Scalar} from "../core/scalar.js"
|
|
3
3
|
|
|
4
|
-
const pi = Math.PI
|
|
4
|
+
export const pi = Math.PI
|
|
5
5
|
|
|
6
|
-
|
|
6
|
+
/** basic unit for measuring rotations */
|
|
7
|
+
export const radians = {
|
|
7
8
|
circle: 2 * pi,
|
|
8
|
-
|
|
9
|
+
halfCircle: pi,
|
|
9
10
|
|
|
10
11
|
toDegrees(r: number) {
|
|
11
12
|
return r * (180 / pi)
|
|
12
13
|
},
|
|
14
|
+
|
|
13
15
|
toArcseconds(r: number) {
|
|
14
|
-
return
|
|
16
|
+
return radians.toDegrees(r) * 3600
|
|
15
17
|
},
|
|
18
|
+
|
|
16
19
|
toTurns(r: number) {
|
|
17
|
-
return r /
|
|
20
|
+
return r / radians.circle
|
|
18
21
|
},
|
|
19
22
|
|
|
20
23
|
circleDistance(radiansA: number, radiansB: number): number {
|
|
21
|
-
const diff = Math.abs(Scalar.wrap(radiansA - radiansB, 0,
|
|
22
|
-
return Math.min(diff,
|
|
24
|
+
const diff = Math.abs(Scalar.wrap(radiansA - radiansB, 0, radians.circle))
|
|
25
|
+
return Math.min(diff, radians.circle - diff)
|
|
23
26
|
},
|
|
24
27
|
}
|
|
25
28
|
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
},
|
|
30
|
-
toDegrees(t: number) {
|
|
31
|
-
return Radians.toDegrees(Turns.toRadians(t))
|
|
32
|
-
},
|
|
29
|
+
/** convert turns to radians */
|
|
30
|
+
export function turns(t: number) {
|
|
31
|
+
return t * radians.circle
|
|
33
32
|
}
|
|
33
|
+
turns.toRadians = turns
|
|
34
|
+
turns.toDegrees = (t: number) => radians.toDegrees(turns(t))
|
|
34
35
|
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
},
|
|
36
|
+
/** convert arcseconds to radians */
|
|
37
|
+
export function arcseconds(a: number) {
|
|
38
|
+
return degrees.toRadians(a / 3600)
|
|
39
39
|
}
|
|
40
|
+
arcseconds.toRadians = arcseconds
|
|
41
|
+
arcseconds.toDegrees = (a: number) => radians.toDegrees(arcseconds(a))
|
|
40
42
|
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
},
|
|
45
|
-
toTurns(d: number) {
|
|
46
|
-
return Radians.toTurns(Degrees.toRadians(d))
|
|
47
|
-
},
|
|
43
|
+
/** convert degrees to radians */
|
|
44
|
+
export function degrees(d: number) {
|
|
45
|
+
return d * (pi / 180)
|
|
48
46
|
}
|
|
47
|
+
degrees.toRadians = degrees
|
|
48
|
+
degrees.toTurns = (d: number) => radians.toTurns(degrees(d))
|
|
49
49
|
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
|
|
2
|
+
import {createNoise2D} from "simplex-noise"
|
|
3
|
+
import {Random} from "./randy.js"
|
|
4
|
+
|
|
5
|
+
export function makeNoise(random: Random) {
|
|
6
|
+
const noise2d = createNoise2D(random)
|
|
7
|
+
|
|
8
|
+
return (x: number, y = 0, scale = 1) => {
|
|
9
|
+
const s = noise2d(x * scale, y * scale)
|
|
10
|
+
return (s + 1) / 2
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
|
package/s/tools/spline.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
|
|
2
|
-
import {Scalar} from "
|
|
2
|
+
import {Scalar} from "../core/scalar.js"
|
|
3
3
|
import {XyArray} from "../core/vec2.js"
|
|
4
4
|
|
|
5
5
|
/** resolve a number within a linear spline. */
|
|
@@ -40,50 +40,44 @@ export function catmullRom(x: number, points: XyArray[]) {
|
|
|
40
40
|
|
|
41
41
|
if (x >= x1 && x <= x2) {
|
|
42
42
|
const t = (x - x1) / (x2 - x1)
|
|
43
|
-
return
|
|
43
|
+
return catmullRomMechanics(t, points[i - 1], points[i], points[i + 1], points[i + 2])
|
|
44
44
|
}
|
|
45
45
|
}
|
|
46
46
|
|
|
47
47
|
throw new Error("x is out of bounds, try again")
|
|
48
48
|
}
|
|
49
49
|
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
if (points.length < 2)
|
|
55
|
-
throw new Error("need at least two points, come on")
|
|
50
|
+
/** simple linear spline where the control points are equally-spaced based on their array indices (x is expected to be between 0 and 1). */
|
|
51
|
+
export function ezLinear(x: number, points: number[]) {
|
|
52
|
+
if (points.length < 2)
|
|
53
|
+
throw new Error("need at least two points, come on")
|
|
56
54
|
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
55
|
+
const points2 = points.map(
|
|
56
|
+
(p, index): XyArray =>
|
|
57
|
+
[Scalar.clamp(index / (points.length - 1)), p]
|
|
58
|
+
)
|
|
61
59
|
|
|
62
|
-
|
|
63
|
-
}
|
|
60
|
+
return linear(Scalar.clamp(x), points2)
|
|
64
61
|
}
|
|
65
62
|
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
return a * t3 + b * t2 + c * t + d
|
|
87
|
-
}
|
|
63
|
+
/** internal big-brain maths for the catmull-rom implementation */
|
|
64
|
+
function catmullRomMechanics(
|
|
65
|
+
t: number,
|
|
66
|
+
[,p0]: XyArray,
|
|
67
|
+
[,p1]: XyArray,
|
|
68
|
+
[,p2]: XyArray,
|
|
69
|
+
[,p3]: XyArray,
|
|
70
|
+
) {
|
|
71
|
+
|
|
72
|
+
const t2 = t * t
|
|
73
|
+
const t3 = t2 * t
|
|
74
|
+
|
|
75
|
+
// coefficients for the cubic polynomial (Catmull-Rom)
|
|
76
|
+
const a = -0.5 * p0 + 1.5 * p1 - 1.5 * p2 + 0.5 * p3
|
|
77
|
+
const b = p0 - 2.5 * p1 + 2 * p2 - 0.5 * p3
|
|
78
|
+
const c = -0.5 * p0 + 0.5 * p2
|
|
79
|
+
const d = p1
|
|
80
|
+
|
|
81
|
+
return a * t3 + b * t2 + c * t + d
|
|
88
82
|
}
|
|
89
83
|
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
export declare const Radians: {
|
|
2
|
+
pi: number;
|
|
3
|
+
circle: number;
|
|
4
|
+
/** @deprecated use Radians.toDegrees instead */
|
|
5
|
+
to: {
|
|
6
|
+
degrees(r: number): number;
|
|
7
|
+
arcseconds(r: number): number;
|
|
8
|
+
turns(r: number): number;
|
|
9
|
+
};
|
|
10
|
+
/** @deprecated use Degrees.toRadians instead */
|
|
11
|
+
from: {
|
|
12
|
+
turns(t: number): number;
|
|
13
|
+
degrees(d: number): number;
|
|
14
|
+
arcseconds(a: number): number;
|
|
15
|
+
};
|
|
16
|
+
toDegrees(r: number): number;
|
|
17
|
+
toArcseconds(r: number): number;
|
|
18
|
+
toTurns(r: number): number;
|
|
19
|
+
circleDistance(radiansA: number, radiansB: number): number;
|
|
20
|
+
};
|
|
21
|
+
export declare const Turns: {
|
|
22
|
+
toRadians(t: number): number;
|
|
23
|
+
};
|
|
24
|
+
export declare const Arcseconds: {
|
|
25
|
+
toRadians(a: number): number;
|
|
26
|
+
};
|
|
27
|
+
export declare const Degrees: {
|
|
28
|
+
toRadians(d: number): number;
|
|
29
|
+
};
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import { Scalar } from "./scalar.js";
|
|
2
|
+
const pi = Math.PI;
|
|
3
|
+
const circle = 2 * Math.PI;
|
|
4
|
+
const to = {
|
|
5
|
+
degrees(r) {
|
|
6
|
+
return r * (180 / pi);
|
|
7
|
+
},
|
|
8
|
+
arcseconds(r) {
|
|
9
|
+
return to.degrees(r) * 3600;
|
|
10
|
+
},
|
|
11
|
+
turns(r) {
|
|
12
|
+
return r / circle;
|
|
13
|
+
},
|
|
14
|
+
};
|
|
15
|
+
const from = {
|
|
16
|
+
turns(t) {
|
|
17
|
+
return t * circle;
|
|
18
|
+
},
|
|
19
|
+
degrees(d) {
|
|
20
|
+
return d * (pi / 180);
|
|
21
|
+
},
|
|
22
|
+
arcseconds(a) {
|
|
23
|
+
return from.degrees(a / 3600);
|
|
24
|
+
}
|
|
25
|
+
};
|
|
26
|
+
export const Radians = {
|
|
27
|
+
pi,
|
|
28
|
+
circle,
|
|
29
|
+
/** @deprecated use Radians.toDegrees instead */
|
|
30
|
+
to,
|
|
31
|
+
/** @deprecated use Degrees.toRadians instead */
|
|
32
|
+
from,
|
|
33
|
+
toDegrees(r) {
|
|
34
|
+
return r * (180 / pi);
|
|
35
|
+
},
|
|
36
|
+
toArcseconds(r) {
|
|
37
|
+
return to.degrees(r) * 3600;
|
|
38
|
+
},
|
|
39
|
+
toTurns(r) {
|
|
40
|
+
return r / circle;
|
|
41
|
+
},
|
|
42
|
+
circleDistance(radiansA, radiansB) {
|
|
43
|
+
const diff = Math.abs(Scalar.wrap(radiansA - radiansB, 0, Radians.circle));
|
|
44
|
+
return Math.min(diff, Radians.circle - diff);
|
|
45
|
+
},
|
|
46
|
+
};
|
|
47
|
+
export const Turns = {
|
|
48
|
+
toRadians(t) {
|
|
49
|
+
return t * circle;
|
|
50
|
+
},
|
|
51
|
+
};
|
|
52
|
+
export const Arcseconds = {
|
|
53
|
+
toRadians(a) {
|
|
54
|
+
return from.degrees(a / 3600);
|
|
55
|
+
},
|
|
56
|
+
};
|
|
57
|
+
export const Degrees = {
|
|
58
|
+
toRadians(d) {
|
|
59
|
+
return d * (pi / 180);
|
|
60
|
+
},
|
|
61
|
+
};
|
|
62
|
+
//# sourceMappingUR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AM,CAAA;IAClB,CAAC;IAED,cAAc,CAAC,QAAgB,EAAE,QAAgB;QAChD,MAAM,IAAI,GAAG,IAAI,CAAC,GAAG,CAAC,MAAM,CAAC,IAAI,CAAC,QAAQ,GAAG,QAAQ,EAAE,CAAC,EAAE,OAAO,CAAC,MAAM,CAAC,CAAC,CAAA;QAC1E,OAAO,IAAI,CAAC,GAAG,CAAC,IAAI,EAAE,OAAO,CAAC,MAAM,GAAG,IAAI,CAAC,CAAA;IAC7C,CAAC;CACD,CAAA;AAED,MAAM,CAAC,MAAM,KAAK,GAAG;IACpB,SAAS,CAAC,CAAS;QAClB,OAAO,CAAC,GAAG,MAAM,CAAA;IAClB,CAAC;CACD,CAAA;AAED,MAAM,CAAC,MAAM,UAAU,GAAG;IACzB,SAAS,CAAC,CAAS;QAClB,OAAO,IAAI,CAAC,OAAO,CAAC,CAAC,GAAG,IAAI,CAAC,CAAA;IAC9B,CAAC;CACD,CAAA;AAED,MAAM,CAAC,MAAM,OAAO,GAAG;IACtB,SAAS,CAAC,CAAS;QAClB,OAAO,CAAC,GAAG,CAAC,EAAE,GAAG,GAAG,CAAC,CAAA;IACtB,CAAC;CACD,CAAA"}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
export declare class Circular {
|
|
2
|
+
x: number;
|
|
3
|
+
constructor(x: number);
|
|
4
|
+
clone(): Circular;
|
|
5
|
+
set(x: number): this;
|
|
6
|
+
static value(x: number | Circular): number;
|
|
7
|
+
static normalize(x: number): number;
|
|
8
|
+
normalize(): this;
|
|
9
|
+
static difference(x: number, y: number): number;
|
|
10
|
+
difference(y: number | Circular): number;
|
|
11
|
+
static lerp(x: number, y: number, fraction: number, max?: number): number;
|
|
12
|
+
lerp(y: number | Circular, fraction: number, max?: number): this;
|
|
13
|
+
static step(x: number, y: number, delta: number): number;
|
|
14
|
+
step(y: number | Circular, delta: number): this;
|
|
15
|
+
static approach(x: number, y: number, speed: number, deltaTime: number, speedLimit?: number): number;
|
|
16
|
+
approach(y: number | Circular, speed: number, deltaTime: number, speedLimit?: number): this;
|
|
17
|
+
}
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import { Scalar } from "./scalar.js";
|
|
2
|
+
export class Circular {
|
|
3
|
+
x;
|
|
4
|
+
constructor(x) {
|
|
5
|
+
this.x = x;
|
|
6
|
+
}
|
|
7
|
+
clone() {
|
|
8
|
+
return new Circular(this.x);
|
|
9
|
+
}
|
|
10
|
+
set(x) {
|
|
11
|
+
this.x = x;
|
|
12
|
+
return this;
|
|
13
|
+
}
|
|
14
|
+
static value(x) {
|
|
15
|
+
return typeof x === "number"
|
|
16
|
+
? x
|
|
17
|
+
: x.x;
|
|
18
|
+
}
|
|
19
|
+
static normalize(x) {
|
|
20
|
+
return Scalar.wrap(x, 0, 2 * Math.PI);
|
|
21
|
+
}
|
|
22
|
+
normalize() {
|
|
23
|
+
this.x = Circular.normalize(this.x);
|
|
24
|
+
return this;
|
|
25
|
+
}
|
|
26
|
+
static difference(x, y) {
|
|
27
|
+
x = this.normalize(x);
|
|
28
|
+
y = this.normalize(y);
|
|
29
|
+
let delta = y - x;
|
|
30
|
+
if (delta > Math.PI)
|
|
31
|
+
delta -= 2 * Math.PI;
|
|
32
|
+
if (delta < -Math.PI)
|
|
33
|
+
delta += 2 * Math.PI;
|
|
34
|
+
return delta;
|
|
35
|
+
}
|
|
36
|
+
difference(y) {
|
|
37
|
+
return Circular.difference(this.x, Circular.value(y));
|
|
38
|
+
}
|
|
39
|
+
static lerp(x, y, fraction, max) {
|
|
40
|
+
const difference = this.difference(x, y);
|
|
41
|
+
let delta = difference * fraction;
|
|
42
|
+
if (max !== undefined && Math.abs(delta) > max)
|
|
43
|
+
delta = Math.sign(delta) * max;
|
|
44
|
+
return this.normalize(x + delta);
|
|
45
|
+
}
|
|
46
|
+
lerp(y, fraction, max) {
|
|
47
|
+
this.x = Circular.lerp(this.x, Circular.value(y), fraction, max);
|
|
48
|
+
return this;
|
|
49
|
+
}
|
|
50
|
+
static step(x, y, delta) {
|
|
51
|
+
const difference = this.difference(x, y);
|
|
52
|
+
return this.normalize(Math.abs(difference) <= delta
|
|
53
|
+
? y
|
|
54
|
+
: x + (Math.sign(difference) * delta));
|
|
55
|
+
}
|
|
56
|
+
step(y, delta) {
|
|
57
|
+
this.x = Circular.step(this.x, Circular.value(y), delta);
|
|
58
|
+
return this;
|
|
59
|
+
}
|
|
60
|
+
static approach(x, y, speed, deltaTime, speedLimit) {
|
|
61
|
+
const difference = this.difference(x, y);
|
|
62
|
+
const change = Scalar.creep(difference, speed, deltaTime, speedLimit);
|
|
63
|
+
return this.normalize(x + change);
|
|
64
|
+
}
|
|
65
|
+
approach(y, speed, deltaTime, speedLimit) {
|
|
66
|
+
this.x = Circular.approach(this.x, Circular.value(y), speed, deltaTime, speedLimit);
|
|
67
|
+
return this;
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
//# sourceMappingURL=circular.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"circular.js","sourceRoot":"","sources":["../../s/concepts/circular.ts"],"names":[],"mappings":"AACA,OAAO,EAAC,MAAM,EAAC,MAAM,aAAa,CAAA;AAElC,MAAM,OAAO,QAAQ;IACD;IAAnB,YAAmB,CAAS;QAAT,MAAC,GAAD,CAAC,CAAQ;IAAG,CAAC;IAEhC,KAAK;QACJ,OAAO,IAAI,QAAQ,CAAC,IAAI,CAAC,CAAC,CAAC,CAAA;IAC5B,CAAC;IAED,GAAG,CAAC,CAAS;QACZ,IAAI,CAAC,CAAC,GAAG,CAAC,CAAA;QACV,OAAO,IAAI,CAAA;IACZ,CAAC;IAED,MAAM,CAAC,KAAK,CAAC,CAAoB;QAChC,OAAO,OAAO,CAAC,KAAK,QAAQ;YAC3B,CAAC,CAAC,CAAC;YACH,CAAC,CAAC,CAAC,CAAC,CAAC,CAAA;IACP,CAAC;IAED,MAAM,CAAC,SAAS,CAAC,CAAS;QACzB,OAAO,MAAM,CAAC,IAAI,CAAC,CAAC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AmB;QACrF,IAAI,CAAC,CAAC,GAAG,QAAQ,CAAC,QAAQ,CAAC,IAAI,CAAC,CAAC,EAAE,QAAQ,CAAC,KAAK,CAAC,CAAC,CAAC,EAAE,KAAK,EAAE,SAAS,EAAE,UAAU,CAAC,CAAA;QACnF,OAAO,IAAI,CAAA;IACZ,CAAC;CACD"}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { Random } from "./randy.js";
|
|
2
|
+
export declare class Noise {
|
|
3
|
+
#private;
|
|
4
|
+
readonly random: Random;
|
|
5
|
+
static seed(seed?: number): Noise;
|
|
6
|
+
constructor(random: Random);
|
|
7
|
+
/** sample the noise field, returning a number from 0 to 1. */
|
|
8
|
+
sample(x: number, y?: number, scale?: number): number;
|
|
9
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { Randy } from "./randy.js";
|
|
2
|
+
import { createNoise2D } from "simplex-noise";
|
|
3
|
+
export class Noise {
|
|
4
|
+
random;
|
|
5
|
+
static seed(seed = Randy.randomSeed()) {
|
|
6
|
+
const random = Randy.makeRandom(seed);
|
|
7
|
+
return new this(random);
|
|
8
|
+
}
|
|
9
|
+
#n2d;
|
|
10
|
+
constructor(random) {
|
|
11
|
+
this.random = random;
|
|
12
|
+
this.#n2d = createNoise2D(random);
|
|
13
|
+
}
|
|
14
|
+
/** sample the noise field, returning a number from 0 to 1. */
|
|
15
|
+
sample(x, y = 0, scale = 1) {
|
|
16
|
+
const s = this.#n2d(x * scale, y * scale);
|
|
17
|
+
return (s + 1) / 2;
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
//# sourceMappingURL=noise.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"noise.js","sourceRoot":"","sources":["../../s/concepts/noise.ts"],"names":[],"mappings":"AACA,OAAO,EAAC,KAAK,EAAS,MAAM,YAAY,CAAA;AACxC,OAAO,EAAC,aAAa,EAAC,MAAM,eAAe,CAAA;AAE3C,MAAM,OAAO,KAAK;IAQW;IAP5B,MAAM,CAAC,IAAI,CAAC,OAAe,KAAK,CAAC,UAAU,EAAE;QAC5C,MAAM,MAAM,GAAG,KAAK,CAAC,UAAU,CAAC,IAAI,CAAC,CAAA;QACrC,OAAO,IAAI,IAAI,CAAC,MAAM,CAAC,CAAA;IACxB,CAAC;IAED,IAAI,CAAkC;IAEtC,YAA4B,MAAc;QAAd,WAAM,GAAN,MAAM,CAAQ;QACzC,IAAI,CAAC,IAAI,GAAG,aAAa,CAAC,MAAM,CAAC,CAAA;IAClC,CAAC;IAED,8DAA8D;IAC9D,MAAM,CAAC,CAAS,EAAE,CAAC,GAAG,CAAC,EAAE,KAAK,GAAG,CAAC;QACjC,MAAM,CAAC,GAAG,IAAI,CAAC,IAAI,CAAC,CAAC,GAAG,KAAK,EAAE,CAAC,GAAG,KAAK,CAAC,CAAA;QACzC,OAAO,CAAC,CAAC,GAAG,CAAC,CAAC,GAAG,CAAC,CAAA;IACnB,CAAC;CACD"}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { Xyz } from "./vec3.js";
|
|
2
|
+
export type QuatArray = [number, number, number, number];
|
|
3
|
+
export type Xyzw = {
|
|
4
|
+
x: number;
|
|
5
|
+
y: number;
|
|
6
|
+
z: number;
|
|
7
|
+
w: number;
|
|
8
|
+
};
|
|
9
|
+
export declare class Quat {
|
|
10
|
+
x: number;
|
|
11
|
+
y: number;
|
|
12
|
+
z: number;
|
|
13
|
+
w: number;
|
|
14
|
+
constructor(x: number, y: number, z: number, w: number);
|
|
15
|
+
static new(x: number, y: number, z: number, w: number): Quat;
|
|
16
|
+
static identity(): Quat;
|
|
17
|
+
static array(q: QuatArray): Quat;
|
|
18
|
+
static import({ x, y, z, w }: Xyzw): Quat;
|
|
19
|
+
static from(q: QuatArray | Xyzw): Quat;
|
|
20
|
+
static rotate_(pitch: number, yaw: number, roll: number): Quat;
|
|
21
|
+
static rotate(vec: Xyz): Quat;
|
|
22
|
+
array(): QuatArray;
|
|
23
|
+
toString(): string;
|
|
24
|
+
clone(): Quat;
|
|
25
|
+
transform_(x: number, y: number, z: number, w: number, global?: boolean): Quat;
|
|
26
|
+
transform({ x, y, z, w }: Xyzw, global?: boolean): Quat;
|
|
27
|
+
rotate_(pitch: number, yaw: number, roll: number, global?: boolean): Quat;
|
|
28
|
+
rotate({ x: pitch, y: yaw, z: roll }: Xyz, global?: boolean): Quat;
|
|
29
|
+
rotateAroundAxis_(angle: number, axisX: number, axisY: number, axisZ: number, global?: boolean): Quat;
|
|
30
|
+
rotateAroundAxis(angle: number, axis: Xyz, global?: boolean): Quat;
|
|
31
|
+
set_(x: number, y: number, z: number, w: number): this;
|
|
32
|
+
set({ x, y, z, w }: Xyzw): this;
|
|
33
|
+
multiply_(x2: number, y2: number, z2: number, w2: number): Quat;
|
|
34
|
+
multiply(...quats: Quat[]): this;
|
|
35
|
+
}
|