#!/usr/local/bin/ruby
#
# pdfclock.rb
#
# A little PDFlib application to draw an analog clock.
#


require 'pdflib'

RADIUS = 200.0
MARGIN = 20.0

pdf = PDF.open('pdfclock_rb.pdf')

pdf.set_info(PDF::INFO_CREATOR, "pdfclock.rb")
pdf.set_info(PDF::INFO_AUTHOR, "TAKAHASHI Hitoshi")
pdf.set_info(PDF::INFO_TITLE, "PDF clock (Ruby)")

pdf.begin_page(2 * (RADIUS + MARGIN), 2 * (RADIUS + MARGIN))

pdf.translate(RADIUS + MARGIN, RADIUS + MARGIN)
pdf.setrgbcolor(0, 0, 1)

# minute strokes
pdf.save
pdf.setlinewidth(2)
0.step(360, 6) do |alpha|
  pdf.rotate(6)
  pdf.moveto(RADIUS, 0)
  pdf.lineto(RADIUS - MARGIN / 3, 0)
  pdf.stroke
end
pdf.restore

# 5 minute strokes
pdf.save
pdf.setlinewidth(3)
0.step(360, 30) do |alpha|
  pdf.rotate(30)
  pdf.moveto(RADIUS, 0)
  pdf.lineto(RADIUS - MARGIN, 0)
  pdf.stroke
end
pdf.restore

time = Time.now

# draw hour hand
pdf.save
pdf.rotate(-(time.min / 60.0 + time.hour) * 30 + 90)
pdf.moveto(-RADIUS / 10, -RADIUS / 20)
pdf.lineto(RADIUS / 2, 0)
pdf.lineto(-RADIUS / 10, RADIUS / 20)
pdf.closepath
pdf.fill
pdf.restore

# draw minute hand
pdf.save
pdf.rotate(-(time.sec / 60.0 + time.min) * 6 + 90)
pdf.moveto(-RADIUS / 10, -RADIUS / 20)
pdf.lineto(RADIUS * 0.8, 0)
pdf.lineto(-RADIUS / 10, RADIUS / 20)
pdf.closepath
pdf.fill
pdf.restore

# draw second hand
pdf.setrgbcolor(1, 0, 0)
pdf.setlinewidth(2)
pdf.save
pdf.rotate(-time.sec * 6 + 90)
pdf.moveto(-RADIUS / 5, 0)
pdf.lineto(RADIUS, 0)
pdf.stroke
pdf.restore

# draw little circle at center
pdf.circle(0, 0, RADIUS / 30)
pdf.fill

#pdf.restore

pdf.end_page

pdf.close
