Skip to content

Sine Waves

wave(v) turns any number into a smooth repeat between 0 and 1: soft stripes, spots, and anything that pulses.

How it works

sin() goes smoothly up and down between −1 and 1, once every 2π (about 6.28). The helper wave(v) = 0.5 + 0.5 × sin(v) moves that to 0..1, which is the range a brightness or a palette wants, so anything can be fed to it.

The number x is multiplied by sets how close the stripes are: bigger numbers give thinner stripes. A stripe repeats every 2π / k pixels, so 0.1 repeats every 63.

Multiplying two waves gives a grid of soft spots, because the result is only bright where both waves are bright.

Putting one wave inside another bends the stripes. Changing the input rather than the output like this is called Domain Warping.

The C is for the raylib pixels starter.

Soft stripes

static RGB shade(float x, float y, float t)
{
    return grey(wave(x * 0.1f));
}
function shade(x, y, t) {
  return grey(wave(x * 0.1));
}

Two waves multiplied: soft spots

static RGB shade(float x, float y, float t)
{
    return grey(wave(x * 0.08f) * wave(y * 0.08f));
}
function shade(x, y, t) {
  return grey(wave(x * 0.08) * wave(y * 0.08));
}

A wave inside a wave

static RGB shade(float x, float y, float t)
{
    return grey(wave(x * 0.08f + 4 * sinf(y * 0.03f)));
}
function shade(x, y, t) {
  return grey(wave(x * 0.08 + 4 * sinf(y * 0.03)));
}

Moving

Take t away inside the wave and the stripes slide one way; put t inside the inner wave too and the bends move as well.

static RGB shade(float x, float y, float t)
{
    return grey(wave(x * 0.08f + 4 * sinf(y * 0.03f + t * 0.5f) - t));
}
function shade(x, y, t) {
  return grey(wave(x * 0.08 + 4 * sinf(y * 0.03 + t * 0.5) - t));
}