Tuesday, July 19, 2011

build an analog clock in c++ using graphics

today,i am going to show you some thing amazing and adventurous.
you have a clock on your wall.but today im going to show how to make a similar clock in c++ using graphics.
before continuing i would expect that you have a little knowledge of vector algebra.without vector algebra this would be nothing.

please ensure that you know how to run graphics mode and must be able to do simple thing's in graphics.if you want to learn please go here onecore-Turbo C++ graphics programming

download sourcecode+exe program
require header files
#include <graphics.h> #include <stdlib.h> #include <stdio.h> #include <conio.h> #include <dos.h> #include<math.h>
int clock_x; int clock_y; int clock_radius; void hand(int color,int thickness,float angle,int radius); void main(void) { /*======================request auto detection=============================*/ int gdriver = DETECT, gmode, errorcode; int midx, midy; /*==================initialize graphics and local variables================*/ initgraph(&gdriver, &gmode, "bgi"); /*==================read result of initialization==========================*/ errorcode = graphresult(); if (errorcode != grOk) /* an error occurred */ { printf("Graphics error: %s\n", grapherrormsg(errorcode)); printf("Press any key to halt:"); getch(); exit(1); /* terminate with an error code */ } /*====================initializing settings==============================*/ clock_x=getmaxx()/2; clock_y=getmaxy()/2; clock_radius=(getmaxx()+getmaxy())/6; while(!kbhit()) { /*============clear the device===============*/ cleardevice(); /*===============set backcolor============-*/ setbkcolor(7); /*=========--print message============--*/ setcolor(4); settextstyle(0,0,1); outtextxy(10,10,"press any key to exit...."); /*=========-printing the clock numbers=========*/ float angle=-M_PI_2; char *num[]={"1","2","3","4","5","6","7","8","9","10","11","12"}; setcolor(1); for(int i=0;i<12;i++) { angle+=M_PI/6; outtextxy(clock_x+(clock_radius*cos(angle)),clock_y+(clock_radius*sin(angle)),num[i]); } /*===============-retrieving time from system===============-*/ struct time t; gettime(&t); /*--------print the am/pm---------*/ setcolor(8); settextstyle(0,0,4); if(t.ti_hour>12) outtextxy(clock_x-(textwidth("PM")/2),clock_y+textheight("PM"),"PM"); else outtextxy(clock_x-(textwidth("AM")/2),clock_y+textheight("AM"),"AM"); /*=====================printing the hour hand==================*/ angle=(M_PI/6)*(t.ti_hour); /*hour are in 24 system*/ hand(4,3,angle,clock_radius-50); /*===-printing the min hand===--*/ angle=(M_PI/30)*(t.ti_min); hand(5,2,angle,clock_radius-10); /*===-printing the second hand===--*/ angle=(M_PI/30)*(t.ti_sec); hand(15,1,angle,clock_radius-10); /*print tail of second hand*/ hand(15,1,angle-M_PI,20); /*print head circle of second*/ setlinestyle(SOLID_LINE,SOLID_FILL,1); setcolor(15); pieslice(clock_x,clock_y,0,360,3); sound(700); /*make a 700 hz tone that resembles the tik-tik sound*/ delay(50); /*delay for 50 Milli-seconds*/ nosound(); /*stop tone*/ delay(950); /*delay for 950 milliseconds*/ } /*=========-clear the device===============--*/ cleardevice(); } void hand(int color,int thickness,float angle,int radius) { /*set the thickness of hand*/ setlinestyle(SOLID_LINE,SOLID_FILL,thickness); /*set hand color*/ setcolor(color); /*calculate the x position of the hand using vector algebra*/ int num_x=clock_x+(radius*cos(angle-M_PI_2)); /*calculate the y position of the hand using vector algebra*/ int num_y=clock_y+(radius*sin(angle-M_PI_2)); line(clock_x,clock_y,num_x,num_y); /*note that M_PI_2 is for making the line 90 degree backward*/ }
that's all folk,i will return again with a new and exiting project.
please leave comment.report any bugs.

Labels: , , , ,


Post a Comment

Subscribe to Post Comments [Atom]

Links to this post:

Create a Link

<< Home