Reputation: 31
I've tried to draw a filled circle with SDL2 and I used Pythagoras to calculate the points.
The problem is that I'm getting different results depending if I'm calculating the X coordinate or the Y coordinate. Someone could explain me why when I'm calculating the X coordinate the circle is not drawn correctly?
200 and 500 are hardcoded positions.
void DrawBall()
{
for(int y = 1; y <= this->iRadius; y++)
{
int x = sqrt((this->iRadius * this->iRadius) - (y * y));
SDL_RenderDrawLine(this->renderer, 200 - x, 200 + y , 200 + x, 200 + y);
SDL_RenderDrawLine(this->renderer, 200 - x, 200 - y + 1, 200 + x, 200 - y + 1);
}
for (int x = 1; x <= this->iRadius; x++) {
int y = sqrt((this->iRadius * this->iRadius) - (x * x));
SDL_RenderDrawLine(this->renderer, 500 - x, 500 + y, 500 + x, 500 + y);
SDL_RenderDrawLine(this->renderer, 500 - x, 500 - y + 1, 500 + x, 500 - y + 1);
}
}
Top-left calculating x, bottom-right calculating y
Upvotes: 1
Views: 316
Reputation: 80187
If you output x/y values for the second case and radius 10, you'll see dense y-values for small x and sparse y-values for larger x. Some lines (y=9) are drawn many times, some horizontals aren't drawn at all (y=1,2,5 here)
9.9 9.8 9.5 9.1 8.6 8.0 7.1 6.0 4.3 0
So horizontal line drawing shows such picture. But you can draw vertical lines - in this case you fill all the circle. (I omitted +1 in code).
Also note that for circle with radius R you should draw 2*r+1
lines, not 2*r
.
SDL_RenderDrawLine(this->renderer, 500 - x, 500 - y, 500 - x, 500 + y);
SDL_RenderDrawLine(this->renderer, 500 + x, 500 - y, 500 + x, 500 + y);
Upvotes: 2