Sharp Clock : Visual C#
Tick Tock, Tick Tock. What is it? Another Clock program? Yes and No. Today’s project i.e. the Sharp Clock contains an analog clock developed in Visual C# which is just like our previous analog clock projects (Amazing Clock and WCB Clock) in appearance and functionality. However, the logic used in developing it and thus the source code, both are considerably different from the above mentioned projects. Therefore, I strongly recommend that you download this project and appreciate the difference.
As mentioned above, the Sharp Clock is not just another analog clock program. It is based upon an entirely different idea. The difference lies in the concept of how a clock program should be designed and the way it should work. You may know that most of the Clock projects you find on the web, including two of my own projects (Amazing Clock and WCB Clock) use trigonometric formulas for calculating the positions of hands, digits etc. Though such programs work flawlessly, they suffer from a serious drawback – they are too complicated for many programmers to understand. Though the formulas themselves might be simple, their application in the program makes them difficult to comprehend. Also, quite often there is too much code which doesn’t help make things any better.
Hence, I came up with this simple and easy to understand program. In this program, there are NO mathematical formulas and hence, no unnecessary and obscure code. This program simply contains two arrays which hold the list of points which contains the location of points and the three hands of the clock. To fully understand how this works, you need to read the source code. However, I have described the basic logic behind the working of this program below.
How does the Sharp Clock work?
Look carefully at the image above. As you can see clearly, the Sharp Clock has three hands : the RED hand indicates SECONDS, the GREEN hand indicates MINUTES and the BLUE hand indicates HOURS. The twelve white dots you see above have been drawn using the FillEllipse() method by explicitly specifying the location of the points which are used to draw it. This has been done by separately calculating their positions (manually) and storing them in two arrays (one for the x-coordinate of each point and the other for y-coordinate of those points) without using complicated code. And of course, you can appreciate the accuracy of the location of each point. The same two arrays have been used for drawing the hands as well.
Now, the part that confuses most programmers
Most programmers face this difficulty while designing their first clock program – How to control the motion of the hour hand? Yes, simple as it may seem it has always troubled most programmers. Before explaining how this program handles that task, it is crucial to know one thing.
The Sharp Clock program reads the current time from the system clock every second. This is one of the reasons why it’s source code is so simple. If it had been designed with the intention of reading the time once and then continuously, a few more lines of code would have been required making it more prone to errors.
Now, let’s get back to the main point. As you know, 60 second = 1 minute and 60 minutes = 1 hour. SIMPLE. But the problem is that while you can change the position of second hand every second, and the minute hand’s position every 60 seconds, a problem arises while moving the hour hand. The problem is :- The hour hand moves very slowly while other hands are moving and it takes 60 minutes (or 3600 seconds) for going from say, 1 to 2. Now, you can’t just move the hour hand instantly from 1 to 2. You have to do so in parts. In other words, you need to move the hour hand every 12 minutes. This poses a problem. Since the program is accessing the system clock every second, and the value of DateTime.Hour changes only every 60 minutes, you cannot just read the “hour value” of the system clock.
For this, there is a simple workaround. The hour hand has been treated like the minute hand (or the second hand) in the sense that it has to pass through 60 points (over 12 hours, of course). So, the “hour value” of the system clock is multiplied by 5 and then a number (which may be 0, 1, 2, 3 or 4) is added to it. It works as follows : We add 0 if the “minute value” is between 0 and 11 (both included), 1 if it is between 12 and 23 and so on till 4. This process gives a number which can be used to accurately indicate the position of the hour hand.
For Example, suppose the time is 09:30 AM. So, the position of the hour hand would be : 9 * 5 = 45 + 2 (30>23 and 30<35) = 47. Therefore, the hour hand would be at the same position where the minute hand would have been if the “minute hand” was 47. If you still find it difficult to understand what I mean, the source code would definitely help you understand.
Sharp Clock Downloads
I hope you liked this project. If you have any queries/suggestions pertaining to this project, let me know.