-
-
Notifications
You must be signed in to change notification settings - Fork 145
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(transducers): add line(), curve()
- Loading branch information
1 parent
315cbf8
commit 31bd5b9
Showing
5 changed files
with
76 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import { expFactor, fit } from "@thi.ng/math"; | ||
|
||
/** | ||
* Iterator yielding `steps` + 1 interpolated values along an | ||
* exponential curve in the closed `[start .. end]` interval. The | ||
* curvature can be controlled via `falloff` (default: 1), which is used | ||
* as a power of 10 to compute the exponential decay / growth factor. | ||
* Use `falloff` < 0 for ease in or > 0 for ease out. | ||
* | ||
* @example | ||
* ```ts | ||
* [...curve(50, 100, 10, 2)] | ||
* // [ | ||
* // 50, | ||
* // 70.228, | ||
* // 82.354, | ||
* // 89.624, | ||
* // 93.982, | ||
* // 96.594, | ||
* // 98.160, | ||
* // 99.099, | ||
* // 99.662, | ||
* // 100 | ||
* // ] | ||
* ``` | ||
* | ||
* @param start - | ||
* @param end - | ||
* @param steps - | ||
* @param falloff - | ||
*/ | ||
export function* curve(start: number, end: number, steps = 10, falloff = 1) { | ||
const fe = 1 / 10 ** falloff; | ||
const f = expFactor(1, fe, steps - 1); | ||
let t = 1; | ||
for (; --steps >= 0; ) { | ||
yield fit(t, 1, fe, start, end); | ||
t *= f; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import { map } from "../xform/map"; | ||
import { normRange } from "./norm-range"; | ||
|
||
/** | ||
* Iterator yielding `steps` + 1 interpolate values on a line in the | ||
* closed `[start .. end]` interval. | ||
* | ||
* @remarks | ||
* This is similar to {@link range}, but potentially provides more | ||
* precise values (by avoiding the accumulation of floating point errors | ||
* during iteration). | ||
* | ||
* @example | ||
* ```ts | ||
* [...line(50, 100, 10)] | ||
* // [ | ||
* // 50, 55, 60, 65, 70, | ||
* // 75, 80, 85, 90, 95, | ||
* // 100 | ||
* // ] | ||
* ``` | ||
* | ||
* @param start - | ||
* @param end - | ||
* @param steps - | ||
*/ | ||
export const line = (start: number, end: number, steps = 10) => { | ||
const delta = end - start; | ||
return map((t) => start + delta * t, normRange(steps)); | ||
}; |