I am making a C++ SDL based game engine, I am in the middle of making a circle but I…
- User
I am making a C++ SDL based game engine, I am in the middle of making a circle but I have two implementations, help me decide which one is better and why:
void Circle::GetPoints(std::vector& outPoints) const { const int count = ((radius * 8 + 7) & ~7); outPoints.clear(); outPoints.reserve(count);
codeconst int diameter = radius * 2; int x = radius - 1; int y = 0; int tx = 1; int ty = 1; int error = tx - diameter; while (x >= y) { outPoints.push_back({ center.GetX() + x, center.GetY() - y }); outPoints.push_back({ center.GetX() + x, center.GetY() + y }); outPoints.push_back({ center.GetX() - x, center.GetY() - y }); outPoints.push_back({ center.GetX() - x, center.GetY() + y }); outPoints.push_back({ center.GetX() + y, center.GetY() - x }); outPoints.push_back({ center.GetX() + y, center.GetY() + x }); outPoints.push_back({ center.GetX() - y, center.GetY() - x }); outPoints.push_back({ center.GetX() - y, center.GetY() + x }); if (error <= 0) { ++y; error += ty; ty += 2; } if (error > 0) { --x; tx += 2; error += (tx - diameter); } }}
void Renderer::DrawCircle(const Circle& circle) { std::vector points; circle.GetPoints(points);
codeDrawPoints(points);}
void Circle::render(SDL_Renderer* renderer) { int x = r; int y = 0; int g = 2 - 2 * r;
codedrawMirrorPixel(renderer, xc, yc, x, y); while(x > y) { y++; if(g < 0) { g = g + 4 * y + 2; } else { x--; g = g + 4 * (y - x) + 5; } drawMirrorPixels(renderer, xc, yc, x, y); }}
void Circle::drawMirrorPixel(SDL_Renderer* renderer, int xc, int yc, int x, int y) { SDL_RenderDrawPoint(renderer, xc + x, yc + y); SDL_RenderDrawPoint(renderer, xc - x, yc + y); SDL_RenderDrawPoint(renderer, xc + y, yc + x); SDL_RenderDrawPoint(renderer, xc + y, yc - x); }
- Assistant
- User
I want to know if both would draw a correct circle or not, I prefer the first one because I plan to use collision, and also it could be better in the future if I implement batching, so is more future-proof, memory contrains are not that bad since today's computers have a lot of ram
- Assistant
Both implementations are based on the Midpoint Circle Algorithm, which is a well-known algorithm for drawing circles in computer graphics. The primary goal of each code snippet is to calculate the set of points that form the circle and then draw those points to the display using a renderer.