i have a problem connected to Bresenham’s ellpse algorithm. I was looking for a code of that on the internet. I found two simmilar implementaions, one from Chat gpt and second from an university page. But when i tried to simple draw it on the piece of paper it turned out that the shape does not look like an ellipse at all. It generates point out of ellipse, for example when central point is at (50, 50) and radius_X is 30 and radius_Y is 20 it gives me point like (110, 56) which is stupid and far away from ellipse. Here is my code. Do you know how to fix it? I think that equation x = xRadius is a little bit suspicious.
#include <stdio.h>
void PlotEllipsePoints(int x, int y, int xCenter, int yCenter) {
printf("(%d, %d)\n", xCenter + x, yCenter + y); // Plot the point in the first quadrant
printf("(%d, %d)\n", xCenter - x, yCenter + y); // Plot the point in the second quadrant
printf("(%d, %d)\n", xCenter - x, yCenter - y); // Plot the point in the third quadrant
printf("(%d, %d)\n", xCenter + x, yCenter - y); // Plot the point in the fourth quadrant
}
void BresenhamEllipse(int xCenter, int yCenter, int xRadius, int yRadius) {
int x = 0, y = yRadius;
int xChange = 2 * yRadius * yRadius * (1 - 2 * xRadius);
int yChange = xRadius * xRadius;
int ellipseError = 0;
int stoppingX = yRadius * yRadius * 2;
int stoppingY = 0;
int TwoXRadiusSquare = 2 * xRadius * xRadius;
int TwoYRadiusSquare = 2 * yRadius * yRadius;
while (stoppingX >= stoppingY) {
PlotEllipsePoints(x, y, xCenter, yCenter);
y++;
stoppingY += TwoXRadiusSquare;
ellipseError += yChange;
yChange += TwoXRadiusSquare;
if (2 * ellipseError + xChange > 0) {
x--;
stoppingX -= TwoYRadiusSquare;
ellipseError += xChange;
xChange += TwoYRadiusSquare;
}
}
x = xRadius;
y = 0;
xChange = yRadius * yRadius;
yChange = TwoXRadiusSquare * (1 - 2 * yRadius);
ellipseError = 0;
stoppingX = 0;
stoppingY = TwoXRadiusSquare * yRadius;
while (stoppingX <= stoppingY) {
PlotEllipsePoints(x, y, xCenter, yCenter);
x++;
stoppingX += TwoYRadiusSquare;
ellipseError += xChange;
xChange += TwoYRadiusSquare;
if (2 * ellipseError + yChange > 0) {
y--;
stoppingY -= TwoXRadiusSquare;
ellipseError += yChange;
yChange += TwoXRadiusSquare;
}
}
}
int main() {
int xCenter = 50, yCenter = 50;
int xRadius = 30, yRadius = 20;
BresenhamEllipse(xCenter, yCenter, xRadius, yRadius);
return 0;
}
user24771014 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
1