# Clock.

Clock.

The current time can be read with the second(), minute(), and hour() functions. In this example, sin() and cos() values are used to set the position of the hands.

```
int cx, cy;
float clockDiameter;

void setup() {
size(640, 360);
stroke(255);

int radius = min(width, height) / 2;

cx = width / 2;
cy = height / 2;
}

void draw() {
background(0);

// Draw the clock background
fill(80);
noStroke();
ellipse(cx, cy, clockDiameter, clockDiameter);

// Angles for sin() and cos() start at 3 o'clock;
// subtract HALF_PI to make them start at the top
float s = map(second(), 0, 60, 0, TWO_PI) - HALF_PI;
float m = map(minute() + norm(second(), 0, 60), 0, 60, 0, TWO_PI) - HALF_PI;
float h = map(hour() + norm(minute(), 0, 60), 0, 24, 0, TWO_PI * 2) - HALF_PI;

// Draw the hands of the clock
stroke(255);
strokeWeight(1);
line(cx, cy, cx + cos(s) * secondsRadius, cy + sin(s) * secondsRadius);
strokeWeight(2);
line(cx, cy, cx + cos(m) * minutesRadius, cy + sin(m) * minutesRadius);
strokeWeight(4);
line(cx, cy, cx + cos(h) * hoursRadius, cy + sin(h) * hoursRadius);

// Draw the minute ticks
strokeWeight(2);
beginShape(POINTS);
for (int a = 0; a < 360; a+=6) {
float x = cx + cos(angle) * secondsRadius;
float y = cy + sin(angle) * secondsRadius;
vertex(x, y);
}
endShape();
}
```

## Functions Used

min()

Determines the smallest value in a sequence of numbers, and then returns that value

ellipse()

Draws an ellipse (oval) to the screen

Converts a degree measurement to its corresponding value in radians

fill()

Sets the color used to fill shapes

size()

Defines the dimension of the display window width and height in units of pixels

cos()

Calculates the cosine of an angle

minute()

Processing communicates with the clock on your computer

line()

Draws a line (a direct path between two points) to the screen

stroke()

Sets the color used to draw lines and borders around shapes

vertex()

All shapes are constructed by connecting a series of vertices

sin()

Calculates the sine of an angle

beginShape()

Using the beginShape() and endShape() functions allow creating more complex forms

setup()

The setup() function is run once, when the program starts

strokeWeight()

Sets the width of the stroke used for lines, points, and the border around shapes

draw()

Called directly after setup(), the draw() function continuously executes the lines of code contained inside its block until the program is stopped or noLoop() is called

hour()

Processing communicates with the clock on your computer

endShape()

The endShape() function is the companion to beginShape() and may only be called after beginShape()

background()

The background() function sets the color used for the background of the Processing window

map()

Re-maps a number from one range to another

norm()

Normalizes a number from another range into a value between 0 and 1