/* You will need a copy of thomas boutell's excellent GD library to use this
   program. You can get one from http://www.boutell.com/gd/
   This is a quick toy when I was experimenting with CGI using GD (I wanted to
   make a 'live' background for the Today Page 
   (http://www.vossnet.co.uk/local/today/), but this never really happened. I
   have a newer version based on the clock characters in the windows WingDings
   font, which looks a lot cooler, but I can't find it...

   Have fun, and give credit where it's due (and I disclaim all the blame)
								   
       Howard Jones (hjones@vossnet.co.uk)

/* gcc -o clock clock.c -L. -lgd -lm */


#include <stdio.h>
#include <time.h>
#include <float.h>
#include <math.h>
#include "gd.h"
#include "gdfontl.h"
#include "gdfonts.h"

#ifndef PI
#define PI 3.141592654
#endif



float
get_dx(int radius,float angle)
{
	float temp;

	temp=radius*cos(angle);
	return temp;
}

float
get_dy(int radius,float angle)
{
	float temp;

	temp=radius*sin(angle);
	return temp;
}

int
main(int argc,char **argv)
{
	FILE *out;
	gdImagePtr im_out;
	int red,white,blue,green,yellow,black;
	int start;

	int i,cx,cy;
	int sdx,sdy,hdx,hdy,mdx,mdy;
        double mradius,hradius,mangle,hangle;
        double sangle,sradius,hours;
        time_t tim;
        struct tm *t;
        char szTemp[80];

	printf("Content-type: image/gif\n\n");

	cx=64;
        cy=64;
        mradius=48;
        hradius=32;
        sradius=40;

	/* Create output image, 128 by 128 pixels. */
        im_out = gdImageCreate(128, 140);

	/* First color allocated is background. */
        white = gdImageColorAllocate(im_out, 255, 255, 255);

        /* Set transparent color. */
        gdImageColorTransparent(im_out, white);

	red = gdImageColorAllocate(im_out, 255, 0, 0);
        green = gdImageColorAllocate(im_out, 0, 255, 0);
        blue = gdImageColorAllocate(im_out, 0, 0, 255);
	black = gdImageColorAllocate(im_out, 0, 0, 0);
	yellow = gdImageColorAllocate(im_out, 255, 255, 0);

	gdImageFilledRectangle(im_out,0,0,127,127,white);
	gdImageArc(im_out,64,64,128,128,0,360,black);
	

	for(i=0;i<12;i++)
        {
            sangle=(i+1)*(2.0*PI)/12.0;
            sradius=50;
            sdx=get_dx(sradius,sangle);
            sdy=get_dy(sradius,sangle);
            
	    gdImageSetPixel(im_out,(int)(cy-sdy),(int)(cx+sdx),black);
	}

	sradius=40;
        
            tim=time(0);
            t=localtime(&tim);

            hours=(t->tm_hour + (t->tm_min/60.0));
            if(hours>12.0) hours-=12.0;

            mangle=(t->tm_min)*(2*PI)/60.0;
            mdx=get_dx(mradius,mangle);
            mdy=get_dy(mradius,mangle);
            gdImageLine(im_out,cx,cy,cx+mdy,cy-mdx,green);

            hangle=(hours)*(2.0*PI)/12.0;
            hdx=get_dx(hradius,hangle);
            hdy=get_dy(hradius,hangle);
	    gdImageLine(im_out,cx,cy,cx+hdy,cy-hdx,red);

            sangle=(t->tm_sec%60)*(2.0*PI)/60.0;
            sdx=get_dx(sradius,sangle);
            sdy=get_dy(sradius,sangle);
            gdImageLine(im_out,cx,cy,cx+sdy,cy-sdx,yellow);

		sprintf(szTemp,"%02d:%02d:%02d",t->tm_hour,t->tm_min,t->tm_sec);
		gdImageString(im_out,gdFontSmall,40,128,szTemp,blue);

/*	gdImageInterlace(im_out, 1); *.
        
        /* Write GIF */
        gdImageGif(im_out, stdout);
        
        gdImageDestroy(im_out);

	return 0;
}

