I'm currently writing my own bitmap library to hone my computer graphics skills. I've been able to code the majority of it, however, I still have to code the functions that'll render solid shapes like discs, polygons...etc.
I'm having difficulties deciding how to tackle this. Should I use a boolean matrix to render the full thing using flood fill algorithm, then copying it back to the original bitmap data matrix? Or should I look for a "mathy" way to fill the shapes I want to render?
Here is my code to render polygons:
void Bitmap::polygon(Point point, const unsigned int radius, const unsigned int sides, const unsigned char red, const unsigned char green, const unsigned char blue)
{
float s = (float)sides;
if((s >= 3) && (0 < radius))
{
const double spi = PI / 2;
const double epi = -PI * 3 / 2;
int x1;
int y1;
int x2;
int y2;
s = (float)(PI * 2 / s);
x1 = point.x + (int)(cos(spi) * radius);
y1 = point.y + (int)(sin(spi) * radius);
{
int j = 0;
for(double i = spi - s; i >= epi; i -= s, j++)
{
x2 = point.x + (int)(cos(i) * radius);
y2 = point.y + (int)(sin(i) * radius);
bresenhamLine(this, x1, y1, x2, y2, red, green, blue);
x1 = x2;
y1 = y2;
}
}
x2 = point.x + (int)(cos(spi) * radius);
y2 = point.y + (int)(sin(spi) * radius);
bresenhamLine(this, x1, y1, x2, y2, red, green, blue);
}
}