An Analog Clock

To code an analog clock, some trigonometry needs to be acquired. A snapshot of the analog clock running is shown below.

The function RotatePoints rotates a specified array of points a given angle in the clockwise direction. The equations for rotating the point(x,y) through an angle 'a' (anticlockwise) are

x' = x*cos(a) - y*sin(a);
y' = y*sin(a) + x*cos(a);

These are the standard linear algebra results obtained by a 2x2 matrix multiplication. To rotate clockwise, one must substitute -a for a to get:

x' =  x*cos(a) + y*sin(a);  // noting that sin(-a) == -sin(a)
y' = -y*sin(a) + x*cos(a);  //        and  cos(-a) ==  cos(a)

These formulae are used in the functions RotatePoint and RotatePoints shown below.

    IPlusPlus.Point RotatePoint(IPlusPlus.Point point,
                                int angle)
        double Radians = 2 * Maths.Pi * angle / 360;

        double Sine = Math.Sin(Radians);
        double Cosine = Math.Cos(Radians);

        XForm Rotation = new XForm((double)Cosine, (double)Sine, (double)-Sine, (double)Cosine);

        return Rotation * point;

    Array<Point> RotatePoints(Array<Point> points,
                             int angle)
        Array<Point> Out = new Array<Point>();
        int Length = (int)points.Length;
        for (int i = 0; i < Length; i++)
            Out[i] = RotatePoint(points[i], angle);
        return Out;

The function RotatePoints is used for drawing the 60 dots surrounding the clock face as well as the hands of the clock contained therein. The function DrawClock draws the 60 ellipses at intervals of 6 degrees, varying the size of the dots at five minute intervals. The code that does this is contained in the sample file.

The function DrawHands performs similar rotations, where the rotational amount is calculated by the time (in hours, minutes and seconds). The initial shapes of the hands are held in arrays. Three angles are calculated based upon the current time.