|
| 1 | +// line, circle, setcolor,initgraph |
| 2 | +#include <graphics.h> |
| 3 | +// sin, cos, M_PI |
| 4 | +#include <math.h> |
| 5 | +// getch |
| 6 | +#include <conio.h> |
| 7 | +// delay |
| 8 | +#include <dos.h> |
| 9 | +// time, localtime |
| 10 | +#include <time.h> |
| 11 | +// NULL |
| 12 | +#include <stdio.h> |
| 13 | +// system |
| 14 | +#include <stdlib.h> |
| 15 | + |
| 16 | +// color time bars |
| 17 | +#define SEC_COLOR BLUE |
| 18 | +#define MIN_COLOR GREEN |
| 19 | +#define HOUR_COLOR RED |
| 20 | +#define HOUR_POINTS_COLOR RED |
| 21 | +#define SEC_MIN_POINTS_COLOR YELLOW |
| 22 | +// center point |
| 23 | +#define CENTER_POINT_COLOR MAGENTA |
| 24 | +#define CENTER_POINT_SIZE 8 |
| 25 | +// dont change this |
| 26 | +#define SHOW 1 |
| 27 | +#define HIDE 0 |
| 28 | + |
| 29 | +void draw_clock(float cx, float cy, float angle, |
| 30 | + float radius, float delay_t,int state){ |
| 31 | + |
| 32 | + // get time |
| 33 | + time_t t = time(NULL); |
| 34 | + struct tm tm = *localtime(&t); |
| 35 | + |
| 36 | + // x,y position for circles and lines |
| 37 | + float x, y; |
| 38 | + |
| 39 | + // highlight point, for counter |
| 40 | + int h_point, i, hour; |
| 41 | + |
| 42 | + // convert hour 24h to 12h |
| 43 | + hour = tm.tm_hour; |
| 44 | + hour += (tm.tm_hour>=12)?-12:0; |
| 45 | + |
| 46 | + if(state){ |
| 47 | + // sec color |
| 48 | + setcolor(SEC_COLOR); |
| 49 | + outtextxy(10, 10, "second color"); |
| 50 | + // min color |
| 51 | + setcolor(MIN_COLOR); |
| 52 | + outtextxy(10, 30, "minute color"); |
| 53 | + // hour color |
| 54 | + setcolor(HOUR_COLOR); |
| 55 | + outtextxy(10, 50, "hour color"); |
| 56 | + } |
| 57 | + |
| 58 | + // draw clock |
| 59 | + for(i=-90; i < 270 ;i+=6){ |
| 60 | + x = cx + cos(i* M_PI / 180) * angle; |
| 61 | + y = cy + sin(i* M_PI / 180) * angle; |
| 62 | + |
| 63 | + // draw clock with time |
| 64 | + if(state){ |
| 65 | + // dont hightlight this point |
| 66 | + h_point = 0; |
| 67 | + |
| 68 | + // center point |
| 69 | + setcolor(CENTER_POINT_COLOR); |
| 70 | + circle(cx, cy, radius+4); |
| 71 | + |
| 72 | + // default bar color |
| 73 | + setcolor(BLACK); |
| 74 | + |
| 75 | + // time lines |
| 76 | + // hour line; slice circle to 360/30 = 12 |
| 77 | + if(hour == (i + 90) / 30.0){ |
| 78 | + setcolor(HOUR_COLOR); |
| 79 | + h_point = 1; |
| 80 | + } |
| 81 | + // min line; slice circle to 360/6 = 60 |
| 82 | + if(tm.tm_min == (i + 90) / 6){ |
| 83 | + setcolor(MIN_COLOR); |
| 84 | + h_point = 1; |
| 85 | + } |
| 86 | + // sec line; slice circle to 360/6 = 60 |
| 87 | + if(tm.tm_sec == (i + 90) / 6){ |
| 88 | + setcolor(SEC_COLOR); |
| 89 | + h_point = 1; |
| 90 | + } |
| 91 | + |
| 92 | + // draw lines |
| 93 | + line(cx, cy, x, y); |
| 94 | + |
| 95 | + // circles; if 360 % 30 == 0 its a hour point |
| 96 | + if((i+90)%30==0){ |
| 97 | + if(!h_point) // highlight this point if a bar in this section |
| 98 | + setcolor(HOUR_POINTS_COLOR); |
| 99 | + circle(x, y, radius+5); |
| 100 | + } |
| 101 | + else { |
| 102 | + if(!h_point) // highlight this point if a bar in this section |
| 103 | + setcolor(SEC_MIN_POINTS_COLOR); |
| 104 | + circle(x, y, radius+1); |
| 105 | + } |
| 106 | + |
| 107 | + } else { |
| 108 | + // animations section |
| 109 | + // lines |
| 110 | + setcolor(RED); |
| 111 | + line(cx, cy, x, y); |
| 112 | + // circles |
| 113 | + setcolor(CYAN); |
| 114 | + circle(x, y, radius); |
| 115 | + } |
| 116 | + // its like sleep function |
| 117 | + delay(delay_t); |
| 118 | + } |
| 119 | +} |
| 120 | + |
| 121 | + |
| 122 | +void main(){ |
| 123 | + float x, y, cx, cy, angle, radius; |
| 124 | + int graphdriver = DETECT, graphmode; |
| 125 | + |
| 126 | + initgraph(&graphdriver, &graphmode, "..\\BGI"); |
| 127 | + // center screen |
| 128 | + cx = getmaxx() / 2.0; |
| 129 | + cy = getmaxy() / 2.0; |
| 130 | + |
| 131 | + angle = 200; |
| 132 | + |
| 133 | + radius = 1; |
| 134 | + |
| 135 | + // clock style animation |
| 136 | + draw_clock(cx, cy, angle, radius, 20, HIDE); |
| 137 | + |
| 138 | + // draw clock |
| 139 | + draw_clock(cx, cy, angle, radius, 10, SHOW); |
| 140 | + // update clock ; if you press any key program stoped. |
| 141 | + while(!kbhit()) |
| 142 | + draw_clock(cx, cy, angle, radius, 0, SHOW); |
| 143 | + |
| 144 | + // wait for a key and exit |
| 145 | + getch(); |
| 146 | + closegraph(); |
| 147 | +} |
0 commit comments