Points on a Circle
For an angle a, (cx + r × cos(a), cy + r × sin(a)) is on a circle of radius r around (cx, cy): the start of almost everything round or spinning.
How it works¶
cos(a) and sin(a) are the x and y of a point on a circle of radius 1, an angle a round from the line going right. Multiply both by r for a bigger circle and add the centre to move it. Because y goes down the screen, increasing the angle goes clockwise.
To place n things evenly, use a = i / n × 2π for i from 0 to n − 1.
If the radius changes with the angle you get other curves. r = cos(5a) gives a rose with 5 petals (an even number gives twice as many).
Turn by the golden angle, π × (3 − √5), about 137.5°, for each new dot, and let the radius grow with the square root of the count: the dots pack like the seeds in a sunflower. This is phyllotaxis. The golden angle works because it is as far from a simple fraction of a turn as an angle can be, so no two dots ever line up.
The C is for the raylib shapes starter (600 × 600; W, H, PAPER and INK come from it).
Dots on a circle¶
static void draw(float t)
{
clear(PAPER);
for (int i = 0; i < 24; i++) {
float a = i / 24.0f * 2 * PI;
DrawCircleV((Vector2){ W / 2 + 220 * cosf(a), H / 2 + 220 * sinf(a) }, 10, INK);
}
}
function draw(t) {
clear(PAPER);
for (let i = 0; i < 24; i++) {
let a = i / 24 * 2 * PI;
DrawCircleV(Vector2(W / 2 + 220 * cosf(a), H / 2 + 220 * sinf(a)), 10, INK);
}
}
A rose: r = cos(5a)¶
static void draw(float t)
{
clear(PAPER);
Vector2 last = { 0 };
for (int i = 0; i <= 630; i++) {
float a = i * 0.01f;
float r = 240 * cosf(5 * a);
Vector2 p = { W / 2 + r * cosf(a), H / 2 + r * sinf(a) };
if (i) DrawLineEx(last, p, 3, INK);
last = p;
}
}
function draw(t) {
clear(PAPER);
let last = Vector2(0, 0);
for (let i = 0; i <= 630; i++) {
let a = i * 0.01;
let r = 240 * cosf(5 * a);
let p = Vector2(W / 2 + r * cosf(a), H / 2 + r * sinf(a));
if (i) DrawLineEx(last, p, 3, INK);
last = p;
}
}
A sunflower: the golden angle¶
static void draw(float t)
{
float golden = PI * (3 - sqrtf(5)); /* about 137.5 degrees */
clear(PAPER);
for (int i = 0; i < 600; i++) {
float a = i * golden;
float r = 11 * sqrtf(i);
DrawCircleV((Vector2){ W / 2 + r * cosf(a), H / 2 + r * sinf(a) }, 6, INK);
}
}
function draw(t) {
let golden = PI * (3 - sqrtf(5)); // about 137.5 degrees
clear(PAPER);
for (let i = 0; i < 600; i++) {
let a = i * golden;
let r = 11 * sqrtf(i);
DrawCircleV(Vector2(W / 2 + r * cosf(a), H / 2 + r * sinf(a)), 6, INK);
}
}
Breathing dots¶
Add time to the angle and the ring turns; use a sine of time for the size and each dot pulses, a little behind the one before.
static void draw(float t)
{
clear(PAPER);
for (int i = 0; i < 24; i++) {
float a = i / 24.0f * 2 * PI + t * 0.2f;
float r = 6 + 4 * sinf(t + i);
DrawCircleV((Vector2){ W / 2 + 220 * cosf(a), H / 2 + 220 * sinf(a) }, r, INK);
}
}
function draw(t) {
clear(PAPER);
for (let i = 0; i < 24; i++) {
let a = i / 24 * 2 * PI + t * 0.2;
let r = 6 + 4 * sinf(t + i);
DrawCircleV(Vector2(W / 2 + 220 * cosf(a), H / 2 + 220 * sinf(a)), r, INK);
}
}
Related¶
- Angle around a Point: the other way round, from x and y to an angle.
- Lissajous Curves: a different speed for x and y.
- 2D Rotation around a point: turning a point by an angle.