|
4 | 4 | * The current time can be read with the second(), minute(), |
5 | 5 | * and hour() functions. In this example, sin() and cos() values |
6 | 6 | * are used to set the position of the hands. |
| 7 | + * |
| 8 | + * Updated 27 February 2010 to handle size() changes. |
7 | 9 | */ |
8 | 10 |
|
| 11 | +int cx, cy; |
| 12 | +float secondsRadius; |
| 13 | +float minutesRadius; |
| 14 | +float hoursRadius; |
| 15 | +float clockDiameter; |
| 16 | + |
9 | 17 | void setup() { |
10 | 18 | size(200, 200); |
11 | 19 | stroke(255); |
12 | 20 | smooth(); |
| 21 | + |
| 22 | + int radius = min(width, height) / 2; |
| 23 | + secondsRadius = radius * 0.72; |
| 24 | + minutesRadius = radius * 0.60; |
| 25 | + hoursRadius = radius * 0.50; |
| 26 | + clockDiameter = radius * 1.8; |
| 27 | + |
| 28 | + cx = width / 2; |
| 29 | + cy = height / 2; |
13 | 30 | } |
14 | | -void draw() { |
| 31 | + |
| 32 | +void draw2() { |
15 | 33 | background(0); |
| 34 | + |
| 35 | + // Draw the clock background |
16 | 36 | fill(80); |
17 | 37 | noStroke(); |
| 38 | + ellipse(cx, cy, clockDiameter, clockDiameter); |
| 39 | + |
18 | 40 | // Angles for sin() and cos() start at 3 o'clock; |
19 | 41 | // subtract HALF_PI to make them start at the top |
20 | | - ellipse(100, 100, 160, 160); |
21 | 42 | float s = map(second(), 0, 60, 0, TWO_PI) - HALF_PI; |
22 | 43 | float m = map(minute() + norm(second(), 0, 60), 0, 60, 0, TWO_PI) - HALF_PI; |
23 | 44 | float h = map(hour() + norm(minute(), 0, 60), 0, 24, 0, TWO_PI * 2) - HALF_PI; |
| 45 | + |
| 46 | + // Draw the hands of the clock |
24 | 47 | stroke(255); |
25 | 48 | strokeWeight(1); |
26 | | - line(100, 100, cos(s) * 72 + 100, sin(s) * 72 + 100); |
| 49 | + line(cx, cy, cx + cos(s) * secondsRadius, cy + sin(s) * secondsRadius); |
27 | 50 | strokeWeight(2); |
28 | | - line(100, 100, cos(m) * 60 + 100, sin(m) * 60 + 100); |
| 51 | + line(cx, cy, cx + cos(m) * minutesRadius, cy + sin(m) * minutesRadius); |
29 | 52 | strokeWeight(4); |
30 | | - line(100, 100, cos(h) * 50 + 100, sin(h) * 50 + 100); |
| 53 | + line(cx, cy, cx + cos(h) * hoursRadius, cy + sin(h) * hoursRadius); |
31 | 54 |
|
32 | 55 | // Draw the minute ticks |
33 | 56 | strokeWeight(2); |
| 57 | + beginShape(POINTS); |
34 | 58 | for (int a = 0; a < 360; a+=6) { |
35 | | - float x = 100 + ( cos(radians(a)) * 72 ); |
36 | | - float y = 100 + ( sin(radians(a)) * 72 ); |
37 | | - point(x, y); |
| 59 | + float x = cx + cos(radians(a)) * secondsRadius; |
| 60 | + float y = cy + sin(radians(a)) * secondsRadius; |
| 61 | + vertex(x, y); |
38 | 62 | } |
| 63 | + endShape(); |
39 | 64 | } |
0 commit comments