Circles and ellipses
About the code
Polar circle
cos θ = x / R
sin θ = y / R
So we can easily retrieve our x and y:
x = R * cos θ
y = R * sin θ
Now we made a simplification by saying that the center of our circle is at the origin.
x = xC + R * cos θ
y = yC + R * sin θ
Then if we vary the angle from 0 to 360 degrees we should get all the points of our circle.
#define DEG_TO_RAD(_x) ((_x) * M_PI / 180.0)
Now we can try do draw a circle:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include "main.h"
#include "Graphics.h"
#include "System.h"
#define SCREEN_WIDTH 640
#define SCREEN_HEIGHT 480
#define DEG_TO_RAD(_x) ((_x) * M_PI / 180.0)
int main(int argc, char* argv[])
{
// init the window
gfx.init("Circle", SCREEN_WIDTH, SCREEN_HEIGHT);
gfx.init2D();
gfx.clearScreen(Color(0, 0, 0, SDL_ALPHA_OPAQUE));
int centerX = SCREEN_WIDTH / 2;
int centerY = SCREEN_HEIGHT / 2;
int radius = 200;
for (int i = 0; i < 360; i++)
{
float angle = DEG_TO_RAD(i);
int x = centerX + radius * cos(angle);
int y = centerY + radius * sin(angle);
gfx.setPixel(x, y, Color(255, 255, 255));
gfx.render();
sys.wait(20);
}
// wait until we quit
while (sys.isQuitRequested() == false)
{
gfx.render();
sys.wait(20);
sys.processEvents();
}
gfx.quit();
return EXIT_SUCCESS;
}
Download source code
Download executable for Windows
I added a small delay between each point.
Filling the gaps
gfx.setPixel(x, y, Color(255, 255, 255));
with
gfx.line(centerX, centerY, x, y, Color(255, 255, 255));
Download source code
Download executable for Windows
for (int i = 0; i < 360; i++)
{
float angle0 = DEG_TO_RAD(i);
int x0 = centerX + radius * cos(angle0);
int y0 = centerY + radius * sin(angle0);
float angle1 = DEG_TO_RAD(i + 1);
int x1 = centerX + radius * cos(angle1);
int y1 = centerY + radius * sin(angle1);
gfx.line(x0, y0, x1, y1, Color(255, 255, 255));
gfx.render();
sys.wait(20);
sys.processEvents();
}
Download source code
Download executable for Windows
And we get a nicer circle.
Changing the parameters
x = xC + R * cos θ
y = yC + R * sin θ
Now changing xC and yC will change the center of the circle.
int centerX = SCREEN_WIDTH / 2;
int centerY = SCREEN_HEIGHT / 2;
int radiusX = 200;
int radiusY = 100;
for (int i = 0; i < 360; i++)
{
float angle0 = DEG_TO_RAD(i);
int x0 = centerX + radiusX * cos(angle0);
int y0 = centerY + radiusY * sin(angle0);
float angle1 = DEG_TO_RAD(i + 1);
int x1 = centerX + radiusX * cos(angle1);
int y1 = centerY + radiusY * sin(angle1);
gfx.line(x0, y0, x1, y1, Color(255, 255, 255));
gfx.render();
sys.wait(20);
}
Download source code
Download executable for Windows
We get an ellipse.
int centerX = SCREEN_WIDTH / 2;
int centerY = SCREEN_HEIGHT / 2;
int radius = 200;
float angleAdd = DEG_TO_RAD(20);
for (int i = 0; i < 360; i++)
{
float angle0 = DEG_TO_RAD(i);
int x0 = centerX + radius * cos(angle0 + angleAdd);
int y0 = centerY + radius * sin(angle0);
float angle1 = DEG_TO_RAD(i + 1);
int x1 = centerX + radius * cos(angle1 + angleAdd);
int y1 = centerY + radius * sin(angle1);
gfx.line(x0, y0, x1, y1, Color(255, 255, 255));
gfx.render();
sys.wait(20);
}
Download source code
Download executable for Windows
Now we have a tilted ellipse.
for (int i = 0; i < 360; i++)
{
for (int j = -90; j <= 90; j += 10)
{
float angleAdd = DEG_TO_RAD(j);
float angle0 = DEG_TO_RAD(i);
int x0 = centerX + radius * cos(angle0 + angleAdd);
int y0 = centerY + radius * sin(angle0);
float angle1 = DEG_TO_RAD(i + 1);
int x1 = centerX + radius * cos(angle1 + angleAdd);
int y1 = centerY + radius * sin(angle1);
gfx.line(x0, y0, x1, y1, Color(255, 255, 255));
}
gfx.render();
sys.wait(20);
}
Download source code
Download executable for Windows