Analog Clock using TurboC++ graphics

vicky_dev 0 Tallied Votes 2K Views Share

This program displays a simple analog clock to show the
current system time. Uses a Hand class to create objects - hour, minute and seconds hands.The 'hands' are just straight lines, and two circles make the 'frame' of the clock. Compiles correctly on TurboC++ v3.0

* clock.cpp
 * C++ Program to display an analog clock using Turbo C++ graphics
 * Written by : Vikram R. Bhat
 * This program displays a simple analog clock to show the
 * current system time.  Uses a Hand class to create objects - hour,
 * minte and seconds hands.The 'hands' are just straight lines, and two circles
 * make the 'frame' of the clock. Compiles correctly on TurboC++ v3.0
 * 
*/
#include <iostream.h>
#include <graphics.h>
#include <time.h>
#include <math.h>
#include <dos.h>	// for kbhit()
#include <stdlib.h>	// for exit()

	// Set the path where your graphics files are located here
#define BGI_PATH "c:\\tc3"

	// Constants
const double PI = 3.14159265;
const double DEGS_PER_SEC = 6.0; // Angle by which seconds hand moves in a sec
const double DEGS_PER_MIN = 6.0;
const double DEGS_PER_HOUR = 30.0;
const double SEC_PER_MIN = 60.0;
const double MIN_PER_HOUR = 60.0;

int maxx, maxy; 	// Maximum x and y coordinates for current graph mode

void Initialize( void );	// Init graphics system
void ShowFrame( void );		// Show clock frame

	// Convert degrees ro radians
inline double ToRadians( double theta )
{
	return (theta*PI)/180.0;
}

	// A hand class
class HAND
{
	int length, color;
	double angle,
		prev_angle;		// Holds prev position, needed to erase hand
 public:
	HAND( int len, int clr ) { length = len; color = clr; prev_angle = angle = 0;}	// Constructor
	void SetAngle( double theta ) { angle = theta; }
	void Copy(){ prev_angle = angle ;}
	int Change(){ return ( angle != prev_angle ); }
	void Display( int x, int y );
	void Erase( int x, int y );
};

	// Display hand with origin x,y
void HAND::Display( int x, int y  )
{
	int x1, y1;

	x1 = x + length*sin( angle );
	y1 = y - length*cos( angle );	// y increases from top to bottom

	setcolor( color );
	setfillstyle( EMPTY_FILL, color );
	line( x,y,x1,y1 );
}

	// Erase previously drawn hand
void HAND::Erase( int x, int y )
{

	int x1, y1;
	x1 = x + length*sin( prev_angle );
	y1 = y - length*cos( prev_angle );	// y increases from top to bottom

	setcolor( BLACK );		// Backgnd color
	line( x,y,x1,y1 );
}


int main()
{
	struct tm* ptr_curtime;	// Pointer to current time
	time_t curtime;			// Holds cuurent time

	HAND hour( 55, RED ), minute( 85, GREEN ), second( 70, BLUE );
	int h_angle, m_angle, s_angle;
	int center_x, center_y;		// Clock center

	Initialize();
	ShowFrame();

	center_x = (maxx+1)/2;
	center_y = (maxy+1)/2;

	while( !kbhit())
	{
		hour.Copy();	// Store previous values
		minute.Copy();
		second.Copy();

			// Get current time
		curtime = time(&curtime);
		ptr_curtime = localtime(&curtime);

			// Calculate the angles of hands
		h_angle = ( ptr_curtime->tm_hour%12 * DEGS_PER_HOUR ) + ( ptr_curtime->tm_min * ( DEGS_PER_HOUR / MIN_PER_HOUR ));	// Ignore change in seconds
		m_angle = ( ptr_curtime->tm_min * DEGS_PER_MIN ) + ( ptr_curtime->tm_sec *  ( DEGS_PER_MIN / SEC_PER_MIN )) ;	// Minute hand's position also changes with secs
		s_angle = ptr_curtime->tm_sec * DEGS_PER_SEC;


		hour.SetAngle( ToRadians(h_angle));
		minute.SetAngle( ToRadians( m_angle));
		second.SetAngle( ToRadians( s_angle));

			// Display all hands if seconds position has changed
		if( second.Change())
		{
			second.Erase( center_x, center_y );
			second.Display( center_x, center_y );

			minute.Erase( center_x, center_y );
			minute.Display( center_x, center_y );

			hour.Erase( center_x, center_y );
			hour.Display( center_x, center_y );

		}

	} // End of while( !kbhit())

	closegraph();
	restorecrtmode();
	return 0;
} // End of main

	// Initialize graphics system
void Initialize( void )
{
	int GraphDriver	= DETECT, GraphMode;
	int ErrorCode;

	initgraph( &GraphDriver, &GraphMode, BGI_PATH );
	ErrorCode = graphresult();
	if( ErrorCode != grOk )
	{
		cout<<"\nGraphics system error: "<<grapherrormsg( ErrorCode );
		exit( 1 );
	}
	maxx = getmaxx();
	maxy =  getmaxy();
}

	// Shows the clock frame
void ShowFrame( void )
{
      setcolor( WHITE );
      int x1,y1;
      circle( (maxx+1)/2, ( maxy+1)/2, 100 );
      circle( (maxx+1)/2, ( maxy+1)/2, 105 );

      for( int angle = 0; angle < 360; angle+=30 )
      {
	    x1 = (maxx+1)/2 + 90*cos(ToRadians(angle));
	    y1 = (maxy+1)/2 - 90*sin(ToRadians(angle));
	    putpixel( x1, y1, CYAN );
     }
}
faiz naghmi -1 Newbie Poster

Please send me Complete Programe on World Clock i have to submit it in my school as im a Class 12 th student i need it

commented: No, do your own work. -1
josatama 0 Newbie Poster

great man

andy1523 0 Newbie Poster

it might be wor or not/?

ipshita 0 Newbie Poster

i tried out the same program and when compiled it showed 9 warnings and 27 errors...kindly help

salman_7 0 Newbie Poster

Thank you dear Nice Work.I am new And my First Semster in BSSE.
My Project is Analog Clock.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.