Even though GD has the ability to draw arcs, sometimes it is preferable to render them manually, in order to gain additional functionality. This example will do just that.
Please type this in, do not copy and paste, and ask questions along the way:
#include <stdio.h> #include <gd.h> #include <math.h> #define PI 3.1415926535897 int main() { FILE *out; char outfile[] = "image.png"; gdImagePtr img; unsigned int current; unsigned short int wide, high; int degree, x, y, r = 0, red = 0, green = 0, blue = 0; float radian; wide = 800; high = 800; img = gdImageCreate(wide, high); current = gdImageColorAllocate(img, 0x00, 0x00, 0x00); gdImageFilledRectangle(img, 0, 0, wide, high, current);
This sets us up and gets us a clean slate.
This part will do the actual grunt work chugging out a circle (using units of squares):
r = 200; green = 0xFF; for(degree = 0; degree < 360; degree++) { radian = degree * (PI / 180); x = (wide / 2) + r * sin(radian); y = (high / 2) + r * cos(radian); current = gdImageColorAllocate(img, 0, green, 0); gdImageFilledRectangle(img, x, y, x+20, y+20, current); }
And close everything up nicely:
// Output the data // out = fopen(outfile, "wb"); gdImagePngEx(img, out, -1); // Close things up // fclose(out); gdImageDestroy(img); return(0); }
Since we're using both the math and GD libraries, don't forget to link against them:
lab46:~/src/gdstuff$ gcc -o circle circle.c -lm -lgd
This will once again produce an image file that will have to be viewed using an appropriate program.