- Basic Graphics Program In C
- Programs In Computer Graphics Using Cards
- C For Graphics
- Computer Graphics Notes
- Mini Projects In Computer Graphics Using Opengl
C Graphics programming is very easy and interesting. You can use graphics programming for developing your games, in making projects, for animation etc. It's not like traditional C programming in which you have to apply complex logic in your program and then you end up with a lot of errors and warnings in your program.
In this tutorial you will learn how you can create an analog clock in C using graphics. This tutorial is written in a way that a beginner C graphics programmer can also understand.Before getting into the main let me explain the functions I have used in the program.
Also Read: C/C++ Program to Create a Digital Stopwatch
Also Read: Simple program to create a circular loading bar using graphics
clockLayout()
I’ve used this function to print the clock layout i.e. clock dial and the markings on the clock. If we observe clearly, the clock has hours marking each separated by 30 degrees and each hour is divided into 5 markings each making an angle of 6 degrees. So, iterating the markings for every 30 degrees gives hours and iterating markings with 6 degrees give minutes markings on the clock. The clock would look like this after executing this function.
secHand()
It is clear from the name that this gonna do something with the seconds hand. This function is going to get the present second from the system clock and incline the line according to a particular angle. Eg: if the present seconds is 5 then the angle of the seconds hand with respect to the vertical must be 30 degrees, i.e. 5*6=30.
minHand()
This function fulfills the task of moving the minutes hand based on the system clock. The minutes hand must be inclined 6 degrees for every minute passing. Eg: if the elapsed minutes are 30 then the minutes hand angle must be making 180 degrees with the vertical.
hrHand()
This function is going to print an inclined hours line. The function is designed to get the present hour and also the no. of elapsed minutes from the system clock and incline the line according to a particular angle. For every hour elapsed the hour hand moves 30 degrees and every 12 minutes it moves 6 degrees.
main()
The first lines in main are graphic initialization, you must change the path “c:turboc3bgi” to your compiler’s BGI file path otherwise program will not work. Coming to the while loop, the while loop iterates for every 100 milliseconds reprinting all the functions. This program is like getting the static picture of clock every second and combining all the pictures to make a moving analog clock.
Also Read: Simple program to create a moving car in graphics
Check out this video for demo
Basic Graphics Program In C
Programs In Computer Graphics Using Cards
Program for Analog Clock in C
2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40 42 44 46 48 50 52 54 56 58 60 62 64 66 68 70 72 74 76 78 80 82 84 86 88 90 92 94 96 98 100 102 104 106 108 110 112 114 116 118 120 122 124 126 128 | /*Note press ctrl+pause break to stop the clock while executing in TC*/ #include<stdio.h> #include<stdlib.h> #include<dos.h> voidsecHand(); voidminHand(); { initgraph(&gdriver,&gmode,'c:turboc3bgi'); if(error!=grOk) printf('Error ingraphics,code=%d',grapherrormsg(error)); } while(1) clockLayout(); minHand(); sleep(1);/* pausing the outputscreen for 1 sec */ cleardevice();/* clearing the previous picture of clock */ } voidclockLayout() inti,x,y,r; maxx=getmaxx(); {/* printing a round ring with outer radius of 5 pixel */ circle(maxx/2,maxy/2,120-i); pieslice(maxx/2,maxy/2,0,360,5);/* displaying a circle in the middle of clock */ r=100; {/* marking the hours for every 30 degrees */ x=(maxx/2)+r*cos(j); } x=maxx/2+100;y=maxy/2; setcolor(RED); for(j=PI/30;j<=(2*PI);j+=(PI/30)) pieslice(x,y,0,360,2); y=(maxy/2)+r*sin(j); } voidsecHand() structtimet; floatO; maxx=getmaxx();maxy=getmaxy(); gettime(&t);/*getting the seconds in system clock */ O=sec*(PI/30)-(PI/2);/* determining the angle of the line with respect to vertical */ line(maxx/2,maxy/2,x+r*cos(O),y+r*sin(O)); { intx,y; floatO; maxx=getmaxx(); x=maxx/2,y=maxy/2; gettime(&t);/*getting the seconds in system clock */ min=t.ti_min; /* determining the angle of the line with respect to vertical */ if(hr<=12)O=(hr*(PI/6)-(PI/2))+((min/12)*(PI/30)); if(hr>12)O=((hr-12)*(PI/6)-(PI/2))+((min/12)*(PI/30)); line(maxx/2,maxy/2,x+r*cos(O),y+r*sin(O)); { intx,y; structtimet; maxy=getmaxy(); y=maxy/2; gettime(&t);/*getting the seconds in system clock */ O=(min*(PI/30)-(PI/2));/* determining the angle of the line with respect to vertical */ line(maxx/2,maxy/2,x+r*cos(O),y+r*sin(O)); |
C For Graphics
Computer Graphics Notes
About AuthorPrathap is a passionate blogger and a very good programmer presently studying B.Tech in Computer Science. He is fascinated of latest technology, gadgets and also love to exploit technology and learn new tips and tricks in internet. He is the founder of Tech Google.