Filled circles
About the code
Filling the circle
void circle(int centerX, int centerY, int radius, Color c)
{
int x = 0;
int y = radius;
int m = 5 - 4 * radius;
while (x <= y)
{
gfx.line(centerX - x, centerY - y, centerX + x, centerY - y, c);
gfx.line(centerX - y, centerY - x, centerX + y, centerY - x, c);
gfx.line(centerX - y, centerY + x, centerX + y, centerY + x, c);
gfx.line(centerX - x, centerY + y, centerX + x, centerY + y, c);
if (m > 0)
{
y--;
m -= 8 * y;
}
x++;
m += 8 * x + 4;
}
}
And as in the previous article, we will time the drawing of 10000 circles:
// time 10000 circles
sys.StartPerfCounter();
for (int i = 0; i < 10000; i++)
circle(SCREEN_WIDTH / 2, SCREEN_HEIGHT / 2, 150, Color(255, 255, 255));
printf("%f\n", sys.StopPerfCounter());
Download source code
Download executable for Windows
This code takes about 480 ms on my computer.
Avoiding overdrawing
while (x <= y)
{
gfx.line(centerX - y, centerY - x, centerX + y, centerY - x, c);
gfx.line(centerX - y, centerY + x, centerX + y, centerY + x, c);
if (m > 0)
{
gfx.line(centerX - x, centerY - y, centerX + x, centerY - y, c);
gfx.line(centerX - x, centerY + y, centerX + x, centerY + y, c);
y--;
m -= 8 * y;
}
x++;
m += 8 * x + 4;
}
Download source code
Download executable for Windows
This code takes about 435 ms.
Using rects
while (x <= y)
{
gfx.rectFill(centerX - y, centerY - x, centerX + y + 1, centerY - x + 1, c);
gfx.rectFill(centerX - y, centerY + x, centerX + y + 1, centerY + x + 1, c);
if (m > 0)
{
gfx.rectFill(centerX - x, centerY - y, centerX + x + 1, centerY - y + 1, c);
gfx.rectFill(centerX - x, centerY + y, centerX + x + 1, centerY + y + 1, c);
y--;
m -= 8 * y;
}
Download source code
Download executable for Windows
This code is a little bit faster. It takes about 410 ms.
Using simple loops
while (x <= y)
{
for (int xx = centerX - y; xx <= centerX + y; xx++)
gfx.setPixel(xx, centerY - x, c);
for (int xx = centerX - y; xx <= centerX + y; xx++)
gfx.setPixel(xx, centerY + x, c);
if (m > 0)
{
for (int xx = centerX - x; xx <= centerX + x; xx++)
gfx.setPixel(xx, centerY - y, c);
for (int xx = centerX - x; xx <= centerX + x; xx++)
gfx.setPixel(xx, centerY + y, c);
y--;
m -= 8 * y;
}
Download source code
Download executable for Windows
Again, this is a little bit faster. This version runs in about 400 ms.
Reducing the loops
while (x <= y)
{
for (int xx = centerX - y; xx <= centerX + y; xx++)
{
gfx.setPixel(xx, centerY - x, c);
gfx.setPixel(xx, centerY + x, c);
}
if (m > 0)
{
for (int xx = centerX - x; xx <= centerX + x; xx++)
{
gfx.setPixel(xx, centerY - y, c);
gfx.setPixel(xx, centerY + y, c);
}
y--;
m -= 8 * y;
}
Download source code
Download executable for Windows
This version takes between 360 and 370 ms.
Using indexes
inline void setPixel(int pos, Color c)
{
mPixels2D[pos] = c;
}
And our drawing loop will now become:
while (x <= y)
{
int pos1 = gfx.pixelPos2D(centerX - y, centerY - x);
int pos2 = gfx.pixelPos2D(centerX - y, centerY + x);
for (int xx = centerX - y; xx <= centerX + y; xx++)
{
gfx.setPixel(pos1++, c);
gfx.setPixel(pos2++, c);
}
if (m > 0)
{
int pos1 = gfx.pixelPos2D(centerX - x, centerY - y);
int pos2 = gfx.pixelPos2D(centerX - x, centerY + y);
for (int xx = centerX - x; xx <= centerX + x; xx++)
{
gfx.setPixel(pos1++, c);
gfx.setPixel(pos2++, c);
}
y--;
m -= 8 * y;
}
Download source code
Download executable for Windows
This version is just a little bit faster, taking between 350 and 360 ms.
Splitting the loops
while (x <= y)
{
int pos = gfx.pixelPos2D(centerX - y, centerY - x);
for (int xx = centerX - y; xx <= centerX + y; xx++)
gfx.setPixel(pos++, c);
pos = gfx.pixelPos2D(centerX - y, centerY + x);
for (int xx = centerX - y; xx <= centerX + y; xx++)
gfx.setPixel(pos++, c);
if (m > 0)
{
pos = gfx.pixelPos2D(centerX - x, centerY - y);
for (int xx = centerX - x; xx <= centerX + x; xx++)
gfx.setPixel(pos++, c);
pos = gfx.pixelPos2D(centerX - x, centerY + y);
for (int xx = centerX - x; xx <= centerX + x; xx++)
gfx.setPixel(pos++, c);
y--;
m -= 8 * y;
}
Download source code
Download executable for Windows
This code takes around 285 ms.
Using pointers
while (x <= y)
{
Color* ptr = &gfx.mPixels2D[gfx.pixelPos2D(centerX - y, centerY - x)];
for (int xx = centerX - y; xx <= centerX + y; xx++)
*ptr++ = c;
ptr = &gfx.mPixels2D[gfx.pixelPos2D(centerX - y, centerY + x)];
for (int xx = centerX - y; xx <= centerX + y; xx++)
*ptr++ = c;
if (m > 0)
{
ptr = &gfx.mPixels2D[gfx.pixelPos2D(centerX - x, centerY - y)];
for (int xx = centerX - x; xx <= centerX + x; xx++)
*ptr++ = c;
ptr = &gfx.mPixels2D[gfx.pixelPos2D(centerX - x, centerY + y)];
for (int xx = centerX - x; xx <= centerX + x; xx++)
*ptr++ = c;
y--;
m -= 8 * y;
}
Download source code
Download executable for Windows
This final version takes around 240 ms, 2 times faster than the first one.