diff options
| author | user <user@clank> | 2026-08-16 15:58:11 +0200 |
|---|---|---|
| committer | user <user@clank> | 2026-08-16 15:58:11 +0200 |
| commit | 72ffc4cc5cc5f9b95d21a115e761de6a632d68a5 (patch) | |
| tree | 860336fd32f5aa62676eff0fcdbd5e72f214908a /tests/expr_test.c | |
| parent | sl0ppty: bits (diff) | |
| download | slosh-72ffc4cc5cc5f9b95d21a115e761de6a632d68a5.tar.gz slosh-72ffc4cc5cc5f9b95d21a115e761de6a632d68a5.tar.xz slosh-72ffc4cc5cc5f9b95d21a115e761de6a632d68a5.zip | |
sl0ppty: sin and cos
128 + sin(t / 4) / 2 a breathe that is actually a sine
abs(sin(x * 6 + t / 5)) a wave travelling along a frame
abs(sin(dist(x, y, curx, cury) * 20 - t / 4)) / 2 ripples from the cursor
Degrees in, -255..255 out. Two decisions, both for the same reason -- this is a
language people type into a config file, not one they read a manual for.
Degrees because that is what anybody guesses, and 255 because it is the scale
every strength here already uses: `128 + sin(t / 4) / 2` is a breathe centred
half way up with nothing to rescale, and `abs(sin(...))` is an amount as it
stands.
Every angle wraps, negative ones included. `t / 4` does not stop at 360 and an
expression that died at one turn would be no use for the one thing this is for.
A 91-entry table of round(255*sin) at one-degree steps, three quadrants by
symmetry, no interpolation -- the argument is an integer and there is nothing
between two degrees to interpolate. A table and not a polynomial because
`contrib/shadertoy.html` reimplements this language in JavaScript so the preview
can run in a browser, and `tests/test_shadertoy.py` compares the two on every
expression: identical data with identical reduction cannot drift the way two
approximations of one curve would. The JS carries the same 91 numbers.
Until now the only oscillator was `abs(t / 6 % 510 - 255)`, a triangle wave
written as arithmetic. It works and it looks like what it is -- a ramp with a
corner in it. A sine has no corner, which is the whole difference between a pane
that pulses and a pane that flickers.
Verified: 19 cases in `tests/expr_test.c` (the cardinal angles, quadrant
symmetry, wrapping past a turn and below zero, and the breathe idiom); the
cross-check now agrees with the compiler on 660 evaluations including generated
trig; the repl's completion drift test caught `sin`/`cos` missing from its list
before I did, which is what it is for. Then six chains in a real pane -- breathe,
travelling wave, clipped crests, plasma, rolling scanlines, cursor ripples --
each measured over eight distinct frames.
Diffstat (limited to 'tests/expr_test.c')
| -rw-r--r-- | tests/expr_test.c | 26 |
1 files changed, 26 insertions, 0 deletions
diff --git a/tests/expr_test.c b/tests/expr_test.c index abb3335..b84e355 100644 --- a/tests/expr_test.c +++ b/tests/expr_test.c @@ -106,6 +106,32 @@ int main(void) { eq("x ^ y", 7); eq("(x ^ y) & 1", 1); + printf("\n-- sin and cos: degrees in, -255..255 out\n"); + eq("sin(0)", 0); + eq("sin(30)", 127); /* round(255 * 0.5) is 127, not 128: 127.5 rounds down */ + eq("sin(90)", 255); + eq("sin(180)", 0); + eq("sin(270)", -255); + eq("cos(0)", 255); + eq("cos(90)", 0); + eq("cos(180)", -255); + /* Symmetry, so the four quadrants are one curve and not four. */ + eq("sin(45) - sin(135)", 0); + eq("sin(30) + sin(210)", 0); + eq("cos(60) - sin(30)", 0); + /* Any angle at all: wrapped, including negative ones, because `t / 4` does not + * stop at 360 and an expression that dies at one turn is no use for animation. */ + eq("sin(360)", 0); + eq("sin(450)", 255); + eq("sin(0 - 90)", -255); + eq("sin(0 - 450)", -255); + eq("sin(36000 + 90)", 255); + /* The shape a strength actually wants: a breathe centred half way up, which is + * why the range is the same 255 the amounts use. */ + eq("128 + sin(90) / 2", 255); + eq("128 + sin(270) / 2", 1); /* -255/2 truncates toward zero, so 128 - 127 */ + eq("abs(sin(270))", 255); + printf("\n-- variables\n"); eq("x", 3); eq("y", 4); |
