Making a circle using only points
Written 2022-07-17.
Search engines are awful nowadays, or maybe it's just the websites themselves that seem to think more is less.
Anyway, I wanted to make a circle with points. Here's how you do it.
Requirements
You will need:
- Radius
- Resolution of circle (how many points the circle will consist of)
- Center of circle
- An array of points / vectors to put the points in
Code
#include <math.h>
int radius = 10;
int resolution = 16; // number of points
Vector2 center = {200, 200};
Vector2 points[resolution] = {0};
for (int i = 0; i < resolution; i++)
{
float degrees = i * (360.f / resolution);
float radians = degrees * M_PI/180;
float x = center.x + radius * cosf(radians);
float y = center.y + radius * sinf(radians);
points[i].x = (int) x;
points[i].y = (int) y;
}
Some frameworks allow you to center a polygon made from points yourself, in that case you don't need the center of the circle.
Removing the center, we are left with a circle made with these points:
10.000000, 0.000000
9.000000, 3.000000
7.000000, 7.000000
3.000000, 9.000000
0.000000, 10.000000
-3.000000, 9.000000
-7.000000, 7.000000
-9.000000, 3.000000
-10.000000, 0.000000
-9.000000, -3.000000
-7.000000, -7.000000
-3.000000, -9.000000
0.000000, -10.000000
3.000000, -9.000000
7.000000, -7.000000
9.000000, -3.000000
Cheers.