Skip to content

Angle around a Point

atan2(dy, dx) is the direction from a centre point to the pixel: rays, spirals, and with distance, polar coordinates.

How it works

atan2(y - cy, x - cx) gives the angle from the centre to the pixel, from −π to π (−180° to 180°), measured from the line going right. Unlike atan(dy / dx) it knows which quarter the pixel is in, and it doesn't divide by zero straight above or below the centre.

A wave of the angle gives rays: wave(a * 12) goes round twelve times as the angle goes round once, because a full turn is 2π. Add the distance as well and each ray is pushed round a bit more the further out it is, which makes a spiral.

Angle and distance together are polar coordinates: another way of saying where a pixel is. Anything made from x and y also works with angle and distance, so the checkerboard from Modulo Repetition becomes a dartboard. Adding π first makes the angle 0..2π, so floor counts the slices from 0.

The C is for the raylib pixels starter.

Rays

static RGB shade(float x, float y, float t)
{
    float a = atan2f(y - 150, x - 150);
    return grey(wave(a * 12));
}
function shade(x, y, t) {
  let a = atan2f(y - 150, x - 150);
  return grey(wave(a * 12));
}

A spiral: angle plus distance

static RGB shade(float x, float y, float t)
{
    float a = atan2f(y - 150, x - 150);
    float d = dist(x, y, 150, 150);
    return grey(wave(a * 3 + d * 0.1f));
}
function shade(x, y, t) {
  let a = atan2f(y - 150, x - 150);
  let d = dist(x, y, 150, 150);
  return grey(wave(a * 3 + d * 0.1));
}

A polar checkerboard

static RGB shade(float x, float y, float t)
{
    float a = atan2f(y - 150, x - 150) + PI;   /* 0 .. 2π */
    float d = dist(x, y, 150, 150);
    return grey(((int)floorf(a / (PI / 8)) + (int)floorf(d / 20)) % 2);
}
function shade(x, y, t) {
  let a = atan2f(y - 150, x - 150) + PI;   // 0 .. 2π
  let d = dist(x, y, 150, 150);
  return grey((floorf(a / (PI / 8)) + floorf(d / 20)) % 2);
}

A turning spiral in colour

static RGB shade(float x, float y, float t)
{
    float a = atan2f(y - 150, x - 150);
    float d = dist(x, y, 150, 150);
    return rainbow(wave(a * 3 + d * 0.1f - t));
}
function shade(x, y, t) {
  let a = atan2f(y - 150, x - 150);
  let d = dist(x, y, 150, 150);
  return rainbow(wave(a * 3 + d * 0.1 - t));
}