13,056,966 members (43,904 online)
alternative version

#### Stats

73.7K views
26 bookmarked
Posted 8 Sep 2003

# A Resizable Analog Clock in C# using GDI+ & Windows Forms

, 8 Sep 2003
 Rate this:
An analog clock using anti-aliasing & double-buffering which can be resized, even into elliptical shapes.
<!-- Article image -->

## Introduction

Unlike other clock programs, this program possesses the following special features:

• Resizability: The main feature is that it can be resized according to the user's demands. Even an elliptical shape can be created. The hands, numerals and clock face all can have their shape changed at will.
• Code Size & Elegance: Special importance has been given to reduce the code size and create elegant code. This program is perhaps the smallest analog clock program in terms of number of lines.
• Double Buffering: It uses double buffering to eliminate flicker.
• Anti-aliasing: It uses anti-aliasing to reduce jaggedness.

## The Program

The main part of the program is the plotting of the hands. For this, we use polar co-ordinates with the origin at the center of the window and with angle starting at the twelve-o'clock position running clockwise. The semi-width or x-radius of the clock is taken to be one-third of that of the enclosing window, ie. `Width/3`. Consequently, the x value is given by the sum of the x co-ordinate of the center and the radius multiplied by the sine of the angle:

```int x_sec = Width/2  +
(int)(Width/3  * Math.Sin(2 * Math.PI * (double)i_sec/60));```

The angle is simply the fraction of a full circle, where 60 seconds correspond to 2 pi radians. In case of the y co-ordinate of the hand, we must take into account the fact that the 12 o'clock position is that of minimum y value, so that the cosine must be subtracted:

```int y_sec = Height/2 -
(int)(Height/3 * Math.Cos(2 * Math.PI * (double)i_sec/60));```

It is often helpful to draw a diagram to elucidate the above concepts. Of course, the program uses standard coding conventions, ie. i,x,y denote integers, g denotes Graphics and f denotes Font. The minutes hand is drawn in a similar fashion. In case of hours, it must be remembered that only 12 hours make a full circle.

In order to make the numerals resizable, the following for loop is used:

```for(int j=1; j <= 12; j++)   // draw numerals
{
g.DrawString(""+j, f, Brushes.Red,
Width/2 + (int)(Width/3 * Math.Sin( j * Math.PI/6))
- (int)g.MeasureString(""+j,f).Width/2,
Height/2 - (int)(Height/3 * Math.Cos( j * Math.PI/6))
- (int)g.MeasureString(""+j,f).Height/2);
}
}
```

Here, the fact that each numeral represents one-sixth of one pi or semicircle is used to plot the values. Moreover, the calculated half value of the string widths and heights are subtracted in order to provide correct centering of the numerals.

To compile and run, enter

```> cs time_ana.cs
> time_ana.exe```

## History

Project completed on 6th Sept. 2003.

A list of licenses authors might use can be found here

## Share

 Web Developer United States
No Biography provided

## You may also be interested in...

 Pro

 First Prev Next
 this is good alrsds23-Sep-09 17:18 alrsds 23-Sep-09 17:18
 create elegant code Carlos H. Perez9-Sep-03 13:18 Carlos H. Perez 9-Sep-03 13:18
 Re: create elegant code tedvg10-Sep-03 0:04 tedvg 10-Sep-03 0:04
 Re: create elegant code eanderson16-Sep-03 4:01 eanderson 16-Sep-03 4:01
 Re: create elegant code Keith Rule16-Sep-03 5:34 Keith Rule 16-Sep-03 5:34
 Re: create elegant code Torben Jakobsen20-Sep-03 21:59 Torben Jakobsen 20-Sep-03 21:59
 Re: create elegant code jeff.hill21-Feb-05 14:23 jeff.hill 21-Feb-05 14:23
 Why not using this? Hamed Mosavi9-Feb-09 7:29 Hamed Mosavi 9-Feb-09 7:29
 Re: create elegant code JayJeckel23-Nov-13 8:03 JayJeckel 23-Nov-13 8:03
 Last Visit: 31-Dec-99 18:00     Last Update: 29-Jul-17 3:17 Refresh 1