Drawing optimized circles on an Agar surface with 8-way symmetry

From Agar

Jump to: navigation, search

This code is based on the 8-way symmetry algorithm by Wu. This code is in the public domain. It uses the Optimized Integer Table Square Root Function.

/*
 * 8-way symmetric circle drawing
 * Public domain
 */
void
circle(AG_Surface *s, int xCenter, int yCenter, int radius, Uint32 c)
{
        int x, y, r2;
 
        r2 = radius * radius;
        AG_PUT_PIXEL2(s, xCenter, yCenter + radius, c);
        AG_PUT_PIXEL2(s, xCenter, yCenter - radius, c);
        AG_PUT_PIXEL2(s, xCenter + radius, yCenter, c);
        AG_PUT_PIXEL2(s, xCenter - radius, yCenter, c);
 
        y = radius;
        x = 1;
        y = (int) (sqrt(r2 - 1) + 0.5);
        while (x < y) {
            AG_PUT_PIXEL2(s, xCenter + x, yCenter + y, c);
            AG_PUT_PIXEL2(s, xCenter + x, yCenter - y, c);
            AG_PUT_PIXEL2(s, xCenter - x, yCenter + y, c);
            AG_PUT_PIXEL2(s, xCenter - x, yCenter - y, c);
            AG_PUT_PIXEL2(s, xCenter + y, yCenter + x, c);
            AG_PUT_PIXEL2(s, xCenter + y, yCenter - x, c);
            AG_PUT_PIXEL2(s, xCenter - y, yCenter + x, c);
            AG_PUT_PIXEL2(s, xCenter - y, yCenter - x, c);
            x += 1;
            y = (int) (sqrt(r2 - x*x) + 0.5);
        }
        if (x == y) {
            AG_PUT_PIXEL2(s, xCenter + x, yCenter + y, c);
            AG_PUT_PIXEL2(s, xCenter + x, yCenter - y, c);
            AG_PUT_PIXEL2(s, xCenter - x, yCenter + y, c);
            AG_PUT_PIXEL2(s, xCenter - x, yCenter - y, c);
        }
}

See also

Personal tools