Distance from a Point
How far each pixel is from a centre point. On its own it makes a round gradient; put through a wave it makes rings.
How it works¶
dist(x, y, cx, cy) is Pythagoras: sqrt((x - cx)² + (y - cy)²). In a pixel sketch every pixel gets its own distance, so the picture becomes a map of "how far from the centre": 0 at the centre, growing at the same rate in every direction. Anything worked out from that one number comes out round.
To use it as a brightness it has to be between 0 and 1. On a 300 × 300 picture the farthest any pixel can be from the middle is a corner, sqrt(150² + 150²) ≈ 212, so dividing by 212 does it.
Put the distance through wave() instead and it repeats, which gives rings. The number the distance is multiplied by sets how close together they are: 0.2 gives a ring every 2π / 0.2 ≈ 31 pixels.
Two sets of rings from two centres, averaged, behave like ripples from two stones dropped in a pond. Where two crests meet the picture is brighter, and where a crest meets a trough they cancel out. That is interference.
The C below is for the raylib pixels starter: each one replaces the shade() in its "Your sketch" block.
Distance as brightness¶
function shade(x, y, t) {
return grey(1 - dist(x, y, 150, 150) / 212);
}
Rings¶
function shade(x, y, t) {
return grey(wave(dist(x, y, 150, 150) * 0.2));
}
Interference¶
static RGB shade(float x, float y, float t)
{
float d1 = dist(x, y, 100, 150);
float d2 = dist(x, y, 200, 150);
return grey((wave(d1 * 0.3f) + wave(d2 * 0.3f)) / 2);
}
function shade(x, y, t) {
var d1 = dist(x, y, 100, 150);
var d2 = dist(x, y, 200, 150);
return grey((wave(d1 * 0.3) + wave(d2 * 0.3)) / 2);
}
Make it move¶
Take t away inside the wave and the rings travel outwards; add it and they travel in.
static RGB shade(float x, float y, float t)
{
return rainbow(wave(dist(x, y, 150, 150) * 0.2f - t));
}
function shade(x, y, t) {
return rainbow(wave(dist(x, y, 150, 150) * 0.2 - t));
}
Related¶
- Sine Waves:
wave()is what turns a distance into rings. - Angle around a Point: the other half of polar coordinates.
- Domain Warping: add noise to the distance and the rings wobble.
- Cosine Palette: where
rainbow()comes from. - Vector Operations: the same length formula, for vectors.