User manual
74
Three variables determine the coordinates of the midpoint; all other graphical elements, the dial and the
hands are in alignment with the midpoint. The variables
MX and MY contain the x- and y-coordinates of the
midpoint, the variable
MP the midpoint point as dot, as used for graphics functions.
Next comes the definition of an important function that calculates dots based on the distance to the centre
and angle points in the coordinate system. This function is called up several times by the programme to
represent both, the dial and the clock hands.
def point(A, W):
w1 = radians(W * 6 – 90); x1 = int(MX + A * cos(w1))
y1 = int(MY + A * sin(w1)); return((x1, y1))
The function uses two parameters: A is the distance of the desired dot from the centre, W is the angle relative
to the centre. To keep in the case the representation of the clock simple, we will calculate the clockwise
angle relative to the vertical 12-clock direction. The angle is expressed in minutes and not in degree; 1/60 of a
full circle is passed on to the function. Such assumptions save a lot of intermediate calculations
Python measures, just like most programming languages, angle units in radians and not in degrees. The
function
radian() from the library math converts degrees to radians. For this, the angle indication used in
the program is multiplied by 6 minutes to arrive at the degree, and then 90 degrees will be subtracted, so
that the 0-direction is pointing vertically upward, as the 0 minute at every hour. This angle converted to
radians is stored for further calculations within the function in the variable
w1.
Displaying a clock relies on the trigonometric functions sine and cosine. Thus from the angle of a point in
radians relative to the midpoint
w1, the coordinates in the rectangular coordinate system x1 and y1) will be
determined. The coordinates of the midpoint are taken from the variables
MX and MY which are defined
outside the function and are globally applied. The distance of the dot from the midpoint will be transferred
to the parameter
A of the function. The function int() calculates from the result the integer value (integer),
because pixel coordinates are only specified as an integer.
The return value of the function is a geometric point with the calculated coordinates
x1 and y1, which is
embraced by double brackets like all points are.
Following the definition of this function, the dial is drawn.
for i in range(60):
pygame.draw.circle(FIELD, BLACK, point(190, i), 2)
A loop draws now the 60 minute dots on a circle, one after the other. All points are identified by the function
point(). They have the same distance from the centre which is 190 pixels and is within the four quadrants
exactly 10 pixels away from the window border. The points have a radius of
2pixels.
for i in range(12):
pygame.draw.circle(FIELD,BLACK, point(190, i * 5), 4)
A second loop draws 12 larger circles that will mark the hours on the dial. These have a radius of 4 pixels,
are drawn simply on the existing circles and overlap them completely. The hour symbols follow in