The code you posted seemed to work.
Anyway, try this:
- Code:
void circle(int x, int y, int r)
{
int dx=r;
int dy=0;
int r2=r*r;
int x2=r2;
int y2=0;
while (dx>dy)
{
plot(x+dx,y+dy,0xFFFF);
plot(x+dx,y-dy,0xFFFF);
plot(x-dx,y+dy,0xFFFF);
plot(x-dx,y-dy,0xFFFF);
plot(x+dy,y+dx,0xFFFF);
plot(x+dy,y-dx,0xFFFF);
plot(x-dy,y+dx,0xFFFF);
plot(x-dy,y-dx,0xFFFF);
dy--;
y2-=(dy<<1);
while (x2+y2 > r2)
{
dx--;
x2-=dx<<1;
}
}
}
Basically, this works the same way:
1. Start out at the right side of the circle
2. draw with symmetry
3. go up
4. does the increased y value make us too far from the center?
5. if yes, go left until it doesn't
6. repeat until y=x (the rest of the circle can be found using symmetry).
A couple notes, instead of squaring x and y each time, we just integrate the derivitive (if f(x)=x^2, then f'(x)=2*x, so instead of calculating x^2, we add 2*x to the last value).
If you want to draw something with a different radius, you will have to either:
a.) if you are scaling along an axis, split the loop into two, one for the first octant, and one for the second, use the symmetry of this to draw the other three quadrants.
b.) if the scaling is non-uniform, but the shape would still be a conic section (i.e. it is representably as a*x^2+b*x*y+c*y^2=r^2), you should be able to use the same theory to create a function for it (Some optimizations wouldn't be possibly). This may or may not be worth the extra trouble.
c.) if the scaling factor is not on an axis, you will have to find a way of figuring out the scale for x and y based on the x and y values of the current point. (the angle doesn't change at a constant rate, but you may be able to work out an approximation to atan(y/x) using calculus, but that probably wouldn't be worth the air you breath while doing it)