Skip to content

Instantly share code, notes, and snippets.

@mgalushka
Created March 3, 2019 12:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mgalushka/ef971454faf0c160f3a7da683985dc2a to your computer and use it in GitHub Desktop.
Save mgalushka/ef971454faf0c160f3a7da683985dc2a to your computer and use it in GitHub Desktop.
Clock (processing)
W = 800
H = 800
class Clock:
def __init__(self, hours, minutes, radius = 2 * W / 3, cx = W / 2, cy = H / 2):
self.hours = hours
self.minutes = minutes
self.radius = radius
self.cx = cx
self.cy = cy
def draw(self):
cx = self.cx
cy = self.cy
# clock
stroke(0);
strokeWeight(10);
fill(255)
ellipse(cx, cy, self.radius, self.radius);
ticksRadius = self.radius * 0.45;
minutesRadius = self.radius * 0.38;
hoursRadius = self.radius * 0.25;
# clock centre
ellipse(cx, cy, 8, 8);
m = map(self.minutes, 0, 60, 0, TWO_PI) - HALF_PI;
h = map(self.hours + norm(self.minutes, 0, 60), 0, 24, 0, TWO_PI * 2) - HALF_PI;
strokeWeight(6);
line(cx, cy, cx + cos(m) * minutesRadius, cy + sin(m) * minutesRadius);
strokeWeight(10);
line(cx, cy, cx + cos(h) * hoursRadius, cy + sin(h) * hoursRadius);
# Draw the minute ticks
strokeWeight(10);
beginShape(POINTS);
for a in range(0, 12):
angle = radians(a * 30);
x = cx + cos(angle) * ticksRadius;
y = cy + sin(angle) * ticksRadius;
vertex(x, y);
endShape();
textSize(24);
fill(0);
text("{h:02d}:{m:02d}".format(h=self.hours, m=self.minutes), cx - 30, cy + 0.6 * self.radius);
def setup():
size(W, H)
background(255)
frameRate(6)
smooth()
rectMode(CENTER)
noLoop()
def draw():
R = W / 3
c1 = Clock(8, 22, R, W / 4, H / 4)
c1.draw()
c2 = Clock(9, 19, R, W / 4 + R + 50, H / 4)
c2.draw()
c1 = Clock(9, 32, R, W / 4, H / 4 + R + 70)
c1.draw()
c2 = Clock(10, 00, R, W / 4 + R + 50, H / 4 + R + 70)
c2.draw()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment