Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- analog_clock_without_graphics_v2.c Author: Dragan Milicev
- Draw a line in console window without graphics.
- Bresenham's line algorithm
- https://en.wikipedia.org/wiki/Bresenham%27s_line_algorithm
- Draw a circle in console window without graphics.
- Equation of a line that passes through points (x1,y1) and (x2,y2)
- y-y1 = (y2-y1)/(x2-x1) * (x-x1)
- Equation of a circle of radius r, whose center coordinates are p and q
- (x-p)^2 + (y-q)^2 = r^2
- Equation of a circle in polar coordinates:
- x = p + r * cos(f)
- y = q + r * sin(f)
- For digital clock:
- https://www.quora.com/How-do-I-print-a-digital-clock-in-C
- https://stackoverflow.com/questions/9253250/need-help-understanding-how-n-b-and-r-will-render-printf-output
- https://www.tutorialspoint.com/c-program-to-print-digital-clock-with-current-time
- size_t strftime(char *str, size_t maxsize, const char *format, const struct tm *timeptr)
- Formats the time represented in the structure timeptr
- according to the formatting rules defined in format and stored into str.
- https://www.tutorialspoint.com/c_standard_library/c_function_strftime.htm
- struct tm *localtime(const time_t *timer)
- Uses the time pointed by timer to fill a tm structure
- with the values that represent the corresponding local time.
- The value of timer is broken up into the structure tm
- and expressed in the local time zone.
- https://www.tutorialspoint.com/c_standard_library/c_function_localtime.htm
- You can find all my C programs at Dragan Milicev's pastebin:
- https://pastebin.com/u/dmilicev
- https://www.facebook.com/dmilicev
- */
- #include <stdio.h>
- #include <time.h> // for clock_t
- #include <math.h> // for round()
- #include <windows.h> // tracking cursor all the time, for cursor position functions
- // gotoxy(x,y) , wherex() , wherey(), showcursor(), hidecursor()
- #define PI 3.14159
- #define CENTER_X 40 // x coordinate of centre
- #define CENTER_Y 12 // y coordinate of centre
- #define CLOCK_R 11 // R is for radius
- #define SECOND_HAND_R 9
- #define MINUTE_HAND_R 9
- #define HOUR_HAND_R 8
- #define RATIO 1.7 // for aspect ratio, to avoid eclipse clock
- #define LEN 100
- #define ESC 27
- // wait (do nothing) for time in milliseconds
- void wait( int milliseconds )
- {
- clock_t start_time = clock(); // get start time
- // looping (do nothing) till required time is not acheived
- while ( clock() < start_time + milliseconds )
- ; // do nothing
- }
- // ----------CURSOR CONTROL FUNCTIONS------------------------------------------
- // make console cursor invisible
- void hidecursor()
- {
- HANDLE consoleHandle = GetStdHandle(STD_OUTPUT_HANDLE);
- CONSOLE_CURSOR_INFO info;
- info.dwSize = 20;
- info.bVisible = FALSE;
- SetConsoleCursorInfo(consoleHandle, &info);
- }
- // make console cursor invisible
- void showcursor()
- {
- HANDLE consoleHandle = GetStdHandle(STD_OUTPUT_HANDLE);
- CONSOLE_CURSOR_INFO info;
- info.dwSize = 20;
- info.bVisible = TRUE;
- SetConsoleCursorInfo(consoleHandle, &info);
- }
- // place cursor at position ( x, y ) = ( row, column )
- void gotoxy( int x, int y )
- {
- COORD coord;
- coord.X = x;
- coord.Y = y;
- SetConsoleCursorPosition( GetStdHandle(STD_OUTPUT_HANDLE), coord );
- }
- // return x coordinate (column) of current cursor position
- // on failure return -1
- int wherex()
- {
- CONSOLE_SCREEN_BUFFER_INFO csbi;
- if (!GetConsoleScreenBufferInfo( GetStdHandle(STD_OUTPUT_HANDLE), &csbi ) )
- return -1;
- return csbi.dwCursorPosition.X;
- }
- // return y coordinate (row) of current cursor position
- // on failure return -1
- int wherey()
- {
- CONSOLE_SCREEN_BUFFER_INFO csbi;
- if (!GetConsoleScreenBufferInfo( GetStdHandle( STD_OUTPUT_HANDLE ), &csbi ) )
- return -1;
- return csbi.dwCursorPosition.Y;
- }
- // END OF-----CURSOR CONTROL FUNCTIONS------------------------------------------
- // Draw a line between two points (x0, y0) and (x1, y1)
- // Bresenham's line algorithm
- // https://en.wikipedia.org/wiki/Bresenham%27s_line_algorithm
- void drawLine(const char *str, int x0, int y0, int x1, int y1)
- {
- int dx, dy, sx, sy, err, e2;
- dx = abs(x1-x0);
- sx = x0<x1 ? 1 : -1;
- dy = -abs(y1-y0);
- sy = y0<y1 ? 1 : -1;
- err = dx+dy; // error value e_xy
- while(1)
- {
- gotoxy( x0, y0 ); // move cursor to (column, row)
- fputs(str, stdout); // print string str without '\n'
- if (x0==x1 && y0==y1)
- break;
- e2 = 2*err;
- if (e2 >= dy)
- {
- err += dy; // e_xy+e_x > 0
- x0 += sx;
- }
- if (e2 <= dx) // e_xy+e_y < 0
- {
- err += dx;
- y0 += sy;
- }
- } // while(1)
- } // drawLine()
- // Draws clock second hand and minute hand
- void drawClockHand( char *str, float p, float q, float r, float minute )
- {
- float x, y, f = minute * (PI/30)-(PI/2); // f is angle of hand
- x = round( p + 1.7 * r * cos(f) );
- y = round( q + r * sin(f) );
- drawLine(str, p, q, x, y);
- } // drawClockHand()
- // Draws clock hour hand
- void drawClockHourHand( char *str, float p, float q, float r, int hr, float min )
- {
- float x, y, f; // f is angle of hand
- // determining the angle of the line with respect to vertical
- if(hr<=12)
- f=(hr*(PI/6)-(PI/2))+((min/12)*(PI/30));
- if(hr>12)
- f=((hr-12)*(PI/6)-(PI/2))+((min/12)*(PI/30));
- x = round( p + 1.7 * r * cos(f) );
- y = round( q + r * sin(f) );
- drawLine(str, p, q, x, y);
- } // drawClockHourHand()
- // Draws clock hours numbers, radius r, whose center coordinates are p and q
- void drawClockNumbers( float p, float q, float r )
- {
- int minute, hour=1;
- float x, y, f;
- char str[3];
- for( minute=5; minute<=60; minute+=5 ) // print hours
- {
- f = minute * (PI/30)-(PI/2);
- x = round( p + RATIO * r * cos(f) );
- y = round( q + r * sin(f) );
- sprintf( str, "%d", hour++ );
- gotoxy( x, y ); // move cursor to (column, row)
- fputs(str, stdout); // print string str without '\n'
- }
- } // drawClockNumbers()
- int main (void)
- {
- int i, memS, memM, memH, ch; // ch is for pressed key
- char buf[LEN]; // string buffer
- time_t curtime; // current local time from system
- struct tm *loc_time; // tm structure for local time
- hidecursor();
- gotoxy( 1, 22 ); // move cursor to (column, row)
- fputs("Escape for quit.", stdout); // print string str without '\n'
- drawClockNumbers( CENTER_X, CENTER_Y, CLOCK_R );
- while(1) // an infinite loop that exits with choice Ecsape, ESC (27)
- {
- while( !_kbhit() ) // rotates in that loop until some key is pressed
- {
- curtime = time (NULL); // reset
- loc_time = localtime (&curtime); // get current local time from system
- // print digital clock, date and time
- // fill buf with local date and time %I hour in 12h format (01-12)
- //strftime( buf, LEN, "\r Date is %d.%m.%Y. Time is %I:%M:%S %p", loc_time );
- // or
- // fill buf with local date and time %H hour in 24h format (00-23)
- strftime( buf, LEN, "Date is %d.%m.%Y.", loc_time );
- gotoxy( 1, 1 ); // move cursor to (column, row)
- fputs(buf, stdout); // print string str without '\n'
- // fill buf with local date and time %H hour in 24h format (00-23)
- strftime( buf, LEN, "Time is %H:%M:%S ", loc_time );
- gotoxy( 1, 3 ); // move cursor to (column, row)
- fputs(buf, stdout); // print string str without '\n'
- // print analog clock, date and time
- // fill buf with local time, minutes
- strftime( buf, LEN, "%M", loc_time );
- i = atoi(buf); // convert string to integer
- memM = i; // memorize minutes value
- // draw clock hour hand
- drawClockHand("M", CENTER_X, CENTER_Y, MINUTE_HAND_R, i );
- // fill buf with local time, hours
- strftime( buf, LEN, "%H", loc_time );
- i = atoi(buf); // convert string to integer
- memH = i; // memorize hour value
- // draw clock hour hand
- //drawClockHand("H", CENTER_X, CENTER_Y, HOUR_HAND_R, i );
- drawClockHourHand("H", CENTER_X, CENTER_Y, HOUR_HAND_R, i, (float)memM );
- // fill buf with local time, seconds
- strftime( buf, LEN, "%S", loc_time );
- i = atoi(buf); // convert string to integer
- memS = i; // memorize seconds value
- // draw clock hour hand
- drawClockHand(".", CENTER_X, CENTER_Y, SECOND_HAND_R, i );
- wait(1000); // wait 1 second = 1000 ms
- // erase clock hands
- drawClockHand(" ", CENTER_X, CENTER_Y, HOUR_HAND_R, memH );
- drawClockHand(" ", CENTER_X, CENTER_Y, MINUTE_HAND_R, memM );
- drawClockHand(" ", CENTER_X, CENTER_Y, SECOND_HAND_R, memS );
- } // while( !_kbhit() )
- ch = _getch(); // now key is pressed, read the keyboard
- if( ch == 0 || ch == 224 ) // if is pressed function key with leading 0 or 224
- ch = _getch(); // let's empty that 0 or 224
- if( ch == ESC ) // if is pressed ESC
- {
- gotoxy(0,22); // move cursor to (column, row)
- showcursor();
- return(0); // end of work, have to return something
- }
- } // while(1)
- //return 0; // this line is never executed and is therefore commented
- }
Advertisement
Add Comment
Please, Sign In to add comment