@altpsyche/maths 0.2.1 → 0.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +52 -1
- package/dist/figure/axis.d.ts +75 -0
- package/dist/figure/axis.js +133 -0
- package/dist/figure/path-data.d.ts +29 -0
- package/dist/figure/path-data.js +283 -0
- package/dist/figure/plot.d.ts +100 -0
- package/dist/figure/plot.js +261 -0
- package/dist/figure/scale.d.ts +32 -0
- package/dist/figure/scale.js +32 -0
- package/dist/figure/ticks.d.ts +46 -0
- package/dist/figure/ticks.js +97 -0
- package/dist/index.d.ts +11 -0
- package/dist/index.js +6 -0
- package/dist/values/interval.d.ts +42 -0
- package/dist/values/interval.js +66 -0
- package/package.json +2 -1
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A run of numbers from one bound to the other, and the arithmetic that reads
|
|
3
|
+
* one against another.
|
|
4
|
+
*
|
|
5
|
+
* This is what a graph's own units are given in, and what a figure's units are
|
|
6
|
+
* given in, so mapping a value from a graph onto a picture is one interval read
|
|
7
|
+
* against another rather than four loose numbers passed around together.
|
|
8
|
+
*
|
|
9
|
+
* A bound above its partner is allowed everywhere here. An axis that counts down
|
|
10
|
+
* the screen and a scale that reverses a direction are both an interval given
|
|
11
|
+
* the other way round, and neither is a mistake to be corrected.
|
|
12
|
+
*/
|
|
13
|
+
import { clamp, lerp, remap as remapNumber } from './scalar.js';
|
|
14
|
+
function makeInterval(from, to) {
|
|
15
|
+
return { from, to };
|
|
16
|
+
}
|
|
17
|
+
/** How far the interval reaches, without a sign, so an interval given either way
|
|
18
|
+
* round reports the same width. */
|
|
19
|
+
function span(interval) {
|
|
20
|
+
return Math.abs(interval.to - interval.from);
|
|
21
|
+
}
|
|
22
|
+
/** Both bounds counting as inside, whichever way round they were given. */
|
|
23
|
+
function holds(interval, value) {
|
|
24
|
+
const low = Math.min(interval.from, interval.to);
|
|
25
|
+
const high = Math.max(interval.from, interval.to);
|
|
26
|
+
return value >= low && value <= high;
|
|
27
|
+
}
|
|
28
|
+
/** The same two bounds with the lower one first, for anything that has to walk
|
|
29
|
+
* from one end to the other and would otherwise step backwards. */
|
|
30
|
+
function ordered(interval) {
|
|
31
|
+
return interval.from <= interval.to ? interval : { from: interval.to, to: interval.from };
|
|
32
|
+
}
|
|
33
|
+
/** A fraction of the way along, and past either bound when the fraction is
|
|
34
|
+
* outside zero to one. */
|
|
35
|
+
function at(interval, along) {
|
|
36
|
+
return lerp(interval.from, interval.to, along);
|
|
37
|
+
}
|
|
38
|
+
/** Held inside the two bounds, whichever way round they were given. */
|
|
39
|
+
function clampTo(interval, value) {
|
|
40
|
+
return clamp(value, interval.from, interval.to);
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* The place a value holds in one interval, read at the same place in another.
|
|
44
|
+
*
|
|
45
|
+
* A source of no width has no place to read, so this hands back the target's
|
|
46
|
+
* first bound rather than an infinity that then spreads through every coordinate
|
|
47
|
+
* built on it.
|
|
48
|
+
*/
|
|
49
|
+
function remap(value, source, target) {
|
|
50
|
+
return remapNumber(value, source.from, source.to, target.from, target.to);
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* The interval calls under one name, so a call site says which kind of thing it
|
|
54
|
+
* is reading and an import line says what these operate on.
|
|
55
|
+
*
|
|
56
|
+
* The width is not called `length`: a function's own `length` is how many
|
|
57
|
+
* arguments it takes, it is not writable, and assigning one throws.
|
|
58
|
+
*/
|
|
59
|
+
export const interval = Object.assign(makeInterval, {
|
|
60
|
+
span,
|
|
61
|
+
holds,
|
|
62
|
+
ordered,
|
|
63
|
+
at,
|
|
64
|
+
clampTo,
|
|
65
|
+
remap,
|
|
66
|
+
});
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@altpsyche/maths",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.4.0",
|
|
4
4
|
"description": "The mathematics AltPsyche's figures are drawn from: vectors, matrices, curves, and a value walked over time.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"author": "Siva",
|
|
@@ -36,6 +36,7 @@
|
|
|
36
36
|
"scripts": {
|
|
37
37
|
"build": "tsc -p tsconfig.build.json",
|
|
38
38
|
"test": "vitest run",
|
|
39
|
+
"demos": "tsc -p tsconfig.demos.json && node .demos/demos/write.js",
|
|
39
40
|
"type-check": "tsc --noEmit -p tsconfig.json",
|
|
40
41
|
"prepack": "npm run build"
|
|
41
42
|
},
|