F How can we simulate the moving second’s hand of a clock using C ? | CodeTheta

How can we simulate the moving second’s hand of a clock using C ?

May 18, 2013

Don't forget to set proper path

#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#define GPATH "C:\\TC\\BGI"
void mp(int, int, int);
void cpp(int, int, int, int);
void dda(int, int, int, int);
void main()
{
int xc, yc, r;
int gd=DETECT, gm;
initgraph(&gd, &gm, GPATH);
r=100;
xc=getmaxx()/2;
yc=getmaxy()/2;
while(!kbhit())
{
mp(r, xc, yc); dda(xc, yc, xc, yc-r); delay(800); cleardevice();
mp(r, xc, yc); dda(xc, yc, xc+r, yc); delay(800); cleardevice();
mp(r, xc, yc); dda(xc, yc, xc, yc+r); delay(800); cleardevice();
mp(r, xc, yc); dda(xc, yc, xc-r, yc); delay(800); cleardevice();
}
getch();
}
void dda(int x1, int y1, int x2, int y2)
{
float x=x1, y=y1, dx, dy;
int len, i;
putpixel(x1, y1, WHITE);
abs(x2-x1)>=abs(y2-y1)?(len=abs(x2-x1)):(len=abs(y2-y1));
dx=(float)(x2-x1)/len;
dy=(float)(y2-y1)/len;
for(i=1; i<=len; i++)
{
x+=dx;
y+=dy;
putpixel((int) x, (int) y, WHITE);
}
return;
}
void mp(int r, int xc, int yc)
{
int x, y, p;
x=0;
y=r;
p=1-r;
cpp(xc, yc, x, y);
while(x<=y)
{
x++;
if(p<0)
p+=2*(x-1);
else
{
p+=2*(x-y)+1;
y--;
}
cpp(xc, yc, x, y);
}
}
void cpp(int xc, int yc, int x, int y)
{
putpixel(xc+x, yc+y, WHITE);
putpixel(xc+y, yc+x, WHITE);
putpixel(xc-x, yc+y, WHITE);
putpixel(xc+y, yc-x, WHITE);
putpixel(xc+x, yc-y, WHITE);
putpixel(xc-y, yc+x, WHITE);
putpixel(xc-x, yc-y, WHITE);
putpixel(xc-y, yc-x, WHITE);
}
Try this code in your computer for better understanding. Enjoy the code. If you have any Question you can contact us or mail us

Post a Comment