The Clock Program
For all the coders out there and those who are looking for crazy stuff in coding,this clock program is a brain riddle and gets you crazy enough.
Have you ever tried writing a program that can display a classic clock that actually works?So,here it is. The clock that runs with "C".
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <time.h>
struct hand{
int x;
int y;
};
int only_star;
char getSymbol(int angle );
int main(int argc, char *argv[]) {
char plane[220][500];
int t,k,x,y,size,clck_x,clck_y,radius;
double val = 3.14159 / 180;
//Input the dimention of the clock.
printf("Enter the size of the clock (max -> 200)(best visible at range 30-60) : ");
scanf("%d",&size);
printf("\nDo you want star symbol for hands (yes -> 1)(no -> 0) : ");
scanf("%d",&only_star);
//set the dementions of the clock layout.
clck_x = 2*size + 10;
clck_y = size;
radius = size/2;
//initialize the clock layout.
for(y=0; y<clck_y; y++)
for(x=0; x<clck_x; x++)
plane[y][x] = ' ';
//initialize the circle in the clock layout.
/*
from equation of circle where radius R and centre (x,y) are given,
coordinates of point on circle can be obtained by
X = R*cos(t) + x and
Y = R*sin(t) + y.
As we create circle in the 4th quadrent of the axis, our Y values are considered in negitive.
As padding is greater vertically when compared to horizontal. we multiply x coordinates with 2
angle t has to be converted from degree to radian, so multiply by val.
*/
for(t=0;t<360;t++){
y = -1*(int)(radius * sin(val*t) - radius);
x = 2*(int)(radius * cos(val*t) + radius);
plane[y][x] = '*';
}
//labels on the watch.
for(t = 1; t<=12 ; t++){
y = -1*(int)(radius * sin(-1*val*(t*30-90)) - radius);
x = 2*(int)(radius * cos(-1*val*(t*30-90)) + radius);
if(t>0 && t<10){
plane[y][x] = (char)t + 48; //48 -- 57
plane[y][x-1] = '(';
plane[y][x+1] = ')';
}
else{
plane[y][x] = (char)t/10 + 48;
plane[y][x+1] = (char)t%10 + 48;
plane[y][x-1] = '(';
plane[y][x+2] = ')';
}
}
//get the current time from the system.
time_t rawtime;
time (&rawtime);
struct tm *tm_struct = localtime(&rawtime);
//get hour, min from tm_struct.
int hour = tm_struct->tm_hour;
int min = tm_struct->tm_min;
int sec = tm_struct->tm_sec;
//declare sec_hand which stores the coordinates of second hand of current moment.
//declare min_hand which stores the coordinates of minute hand of current moment.
//declare hour_hand which stores the coordinates of hour hand of current moment.
struct hand min_hand[100], hour_hand[100], sec_hand[100];
//set sizes for the hands.
int min_hand_size = radius*60/100, hour_hand_size = radius*35/100, sec_hand_size = radius*80/100;
//convert the min, hour to angle.
float min_angle=(min/60.0)*360, hour_angle=((hour%12)/12.0)*360, sec_angle = (sec/60.0)*360;
//each time a clock tic`s it moves a certain angle which is used to increment the angle of a hand for every tic or toc.
float hand_inc_angle = 60.0/360;
/*
we have to compute for the coordinates of the hands at one particular instance of time.
we accomplish the task by first considering a vertical line { (0,1), (0,2), (0,3),...(0,size_of_hand) },
rotating the axis with the angle of the hand and find new coordinates of rotated axis.
X = x*cos(t) + y*sin(t)
Y = y*cos(t) - x*sin(t)
(x,y) -> (0,k)
X = k*sin(t) , Y = k*cos(t)
later, we transfer the axis from (0,0) to (radius,-radius)
X` = X + radius.
Y` = Y - radius.
later, store the coordinates of the hands in to its respective hands structure.
*/
int compute_min = 1, compute_hour = 1;
while(1){
sec_angle += hand_inc_angle; //increment the angle of second`s hand.
//Boundary conditions of second hand`s angle.
if(sec_angle>=360){
sec_angle = sec_angle - 360;
min_angle += hand_inc_angle ; //increment the angle of minute hand.
compute_min = 1;
//Boundary conditions of minute hand`s angle.
if(min_angle >= 360){
min_angle = min_angle - 360;
hour_angle += hand_inc_angle ; //increment the angle of hour hand.
compute_hour = 1;
//Boundary conditions of hour hand`s angle.
if(hour_angle >= 360){
hour_angle = hour_angle - 360;
}
}
}
//find the (x,y) coordinates of the second`s hand.
for(k=0; k < sec_hand_size; k++){
sec_hand[k].x = 2*(int)(k*sin(val*sec_angle) + radius) ;
sec_hand[k].y = -1*(int)(k*cos(val*sec_angle) - radius) ;
plane[sec_hand[k].y][sec_hand[k].x] = getSymbol((int)sec_angle);
}
if(compute_min == 1)
for(k=0; k < min_hand_size; k++){
min_hand[k].x = 2*(int)(k*sin(val*min_angle) + radius) ;
min_hand[k].y = -1*(int)(k*cos(val*min_angle) - radius) ;
compute_min = 0;
}
for(k=0;k < min_hand_size; k++)
plane[min_hand[k].y][min_hand[k].x] = getSymbol((int)min_angle);
if(compute_hour == 1)
for(k=0;k < hour_hand_size; k++){
hour_hand[k].x = 2*(int)(k*sin(val*hour_angle) + radius) ;
hour_hand[k].y = -1*(int)(k*cos(val*hour_angle) - radius) ;
compute_hour = 0;
}
for(k=0;k < hour_hand_size; k++)
plane[hour_hand[k].y][hour_hand[k].x] = getSymbol((int)hour_angle);
//clear the screen. in linux. for windows use "clr"
system("clear");
//print the clock layout along with hands.
for(y=0; y < clck_y; y++){
printf("\t\t");
for(x=0; x < clck_x; x++)
printf("%c",plane[y][x]);
printf("\n");
}
//remove the hands from the clock layout, so that in next loop new coordinates of hands can be stored.
for(k=0; k < sec_hand_size ; k++)
plane[sec_hand[k].y][sec_hand[k].x] = ' ';
for(k=0; k < min_hand_size ; k++)
plane[min_hand[k].y][min_hand[k].x] = ' ';
for(k=0; k < hour_hand_size ; k++)
plane[hour_hand[k].y][hour_hand[k].x] = ' ';
/*
sleep for a second. usleep accepts input in microseconds pow(10,6).
but the system requires some time to process all the above statements and loops. therefore, keeping the point in mind we have to make the program sleep for a little time lesser then a secound.
*/
usleep(100000);
}
return 0;
}
char getSymbol(int angle ){
if(only_star == 1)
return '*';
if(angle >= 340 || angle <= 20)
return '|';
else if(angle >= 20 && angle <= 70 )
return '/';
else if(angle >= 70 && angle <= 110 )
return '_';
else if(angle >= 110 && angle <= 160 )
return '\\';
else if(angle >= 160 && angle <= 200 )
return '|';
else if(angle >= 200 && angle <= 250 )
return '/';
else if(angle >= 250 && angle <= 290 )
return '_';
else if(angle >= 290 && angle <= 340 )
return '\\';
else
return '*';
}
OUTPUT
For all the coders out there and those who are looking for crazy stuff in coding,this clock program is a brain riddle and gets you crazy enough.
Have you ever tried writing a program that can display a classic clock that actually works?So,here it is. The clock that runs with "C".
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <time.h>
struct hand{
int x;
int y;
};
int only_star;
char getSymbol(int angle );
int main(int argc, char *argv[]) {
char plane[220][500];
int t,k,x,y,size,clck_x,clck_y,radius;
double val = 3.14159 / 180;
//Input the dimention of the clock.
printf("Enter the size of the clock (max -> 200)(best visible at range 30-60) : ");
scanf("%d",&size);
printf("\nDo you want star symbol for hands (yes -> 1)(no -> 0) : ");
scanf("%d",&only_star);
//set the dementions of the clock layout.
clck_x = 2*size + 10;
clck_y = size;
radius = size/2;
//initialize the clock layout.
for(y=0; y<clck_y; y++)
for(x=0; x<clck_x; x++)
plane[y][x] = ' ';
//initialize the circle in the clock layout.
/*
from equation of circle where radius R and centre (x,y) are given,
coordinates of point on circle can be obtained by
X = R*cos(t) + x and
Y = R*sin(t) + y.
As we create circle in the 4th quadrent of the axis, our Y values are considered in negitive.
As padding is greater vertically when compared to horizontal. we multiply x coordinates with 2
angle t has to be converted from degree to radian, so multiply by val.
*/
for(t=0;t<360;t++){
y = -1*(int)(radius * sin(val*t) - radius);
x = 2*(int)(radius * cos(val*t) + radius);
plane[y][x] = '*';
}
//labels on the watch.
for(t = 1; t<=12 ; t++){
y = -1*(int)(radius * sin(-1*val*(t*30-90)) - radius);
x = 2*(int)(radius * cos(-1*val*(t*30-90)) + radius);
if(t>0 && t<10){
plane[y][x] = (char)t + 48; //48 -- 57
plane[y][x-1] = '(';
plane[y][x+1] = ')';
}
else{
plane[y][x] = (char)t/10 + 48;
plane[y][x+1] = (char)t%10 + 48;
plane[y][x-1] = '(';
plane[y][x+2] = ')';
}
}
//get the current time from the system.
time_t rawtime;
time (&rawtime);
struct tm *tm_struct = localtime(&rawtime);
//get hour, min from tm_struct.
int hour = tm_struct->tm_hour;
int min = tm_struct->tm_min;
int sec = tm_struct->tm_sec;
//declare sec_hand which stores the coordinates of second hand of current moment.
//declare min_hand which stores the coordinates of minute hand of current moment.
//declare hour_hand which stores the coordinates of hour hand of current moment.
struct hand min_hand[100], hour_hand[100], sec_hand[100];
//set sizes for the hands.
int min_hand_size = radius*60/100, hour_hand_size = radius*35/100, sec_hand_size = radius*80/100;
//convert the min, hour to angle.
float min_angle=(min/60.0)*360, hour_angle=((hour%12)/12.0)*360, sec_angle = (sec/60.0)*360;
//each time a clock tic`s it moves a certain angle which is used to increment the angle of a hand for every tic or toc.
float hand_inc_angle = 60.0/360;
/*
we have to compute for the coordinates of the hands at one particular instance of time.
we accomplish the task by first considering a vertical line { (0,1), (0,2), (0,3),...(0,size_of_hand) },
rotating the axis with the angle of the hand and find new coordinates of rotated axis.
X = x*cos(t) + y*sin(t)
Y = y*cos(t) - x*sin(t)
(x,y) -> (0,k)
X = k*sin(t) , Y = k*cos(t)
later, we transfer the axis from (0,0) to (radius,-radius)
X` = X + radius.
Y` = Y - radius.
later, store the coordinates of the hands in to its respective hands structure.
*/
int compute_min = 1, compute_hour = 1;
while(1){
sec_angle += hand_inc_angle; //increment the angle of second`s hand.
//Boundary conditions of second hand`s angle.
if(sec_angle>=360){
sec_angle = sec_angle - 360;
min_angle += hand_inc_angle ; //increment the angle of minute hand.
compute_min = 1;
//Boundary conditions of minute hand`s angle.
if(min_angle >= 360){
min_angle = min_angle - 360;
hour_angle += hand_inc_angle ; //increment the angle of hour hand.
compute_hour = 1;
//Boundary conditions of hour hand`s angle.
if(hour_angle >= 360){
hour_angle = hour_angle - 360;
}
}
}
//find the (x,y) coordinates of the second`s hand.
for(k=0; k < sec_hand_size; k++){
sec_hand[k].x = 2*(int)(k*sin(val*sec_angle) + radius) ;
sec_hand[k].y = -1*(int)(k*cos(val*sec_angle) - radius) ;
plane[sec_hand[k].y][sec_hand[k].x] = getSymbol((int)sec_angle);
}
if(compute_min == 1)
for(k=0; k < min_hand_size; k++){
min_hand[k].x = 2*(int)(k*sin(val*min_angle) + radius) ;
min_hand[k].y = -1*(int)(k*cos(val*min_angle) - radius) ;
compute_min = 0;
}
for(k=0;k < min_hand_size; k++)
plane[min_hand[k].y][min_hand[k].x] = getSymbol((int)min_angle);
if(compute_hour == 1)
for(k=0;k < hour_hand_size; k++){
hour_hand[k].x = 2*(int)(k*sin(val*hour_angle) + radius) ;
hour_hand[k].y = -1*(int)(k*cos(val*hour_angle) - radius) ;
compute_hour = 0;
}
for(k=0;k < hour_hand_size; k++)
plane[hour_hand[k].y][hour_hand[k].x] = getSymbol((int)hour_angle);
//clear the screen. in linux. for windows use "clr"
system("clear");
//print the clock layout along with hands.
for(y=0; y < clck_y; y++){
printf("\t\t");
for(x=0; x < clck_x; x++)
printf("%c",plane[y][x]);
printf("\n");
}
//remove the hands from the clock layout, so that in next loop new coordinates of hands can be stored.
for(k=0; k < sec_hand_size ; k++)
plane[sec_hand[k].y][sec_hand[k].x] = ' ';
for(k=0; k < min_hand_size ; k++)
plane[min_hand[k].y][min_hand[k].x] = ' ';
for(k=0; k < hour_hand_size ; k++)
plane[hour_hand[k].y][hour_hand[k].x] = ' ';
/*
sleep for a second. usleep accepts input in microseconds pow(10,6).
but the system requires some time to process all the above statements and loops. therefore, keeping the point in mind we have to make the program sleep for a little time lesser then a secound.
*/
usleep(100000);
}
return 0;
}
char getSymbol(int angle ){
if(only_star == 1)
return '*';
if(angle >= 340 || angle <= 20)
return '|';
else if(angle >= 20 && angle <= 70 )
return '/';
else if(angle >= 70 && angle <= 110 )
return '_';
else if(angle >= 110 && angle <= 160 )
return '\\';
else if(angle >= 160 && angle <= 200 )
return '|';
else if(angle >= 200 && angle <= 250 )
return '/';
else if(angle >= 250 && angle <= 290 )
return '_';
else if(angle >= 290 && angle <= 340 )
return '\\';
else
return '*';
}
OUTPUT
Comments
Post a Comment