Log In  

Cart #20176 | 2016-05-04 | Code ▽ | Embed ▽ | License: CC4-BY-NC-SA
1

Just an example of an analog clock you are welcome to use in any project.

Will work in a realtime fashion, like the demo. Or could be passed a static hour/minute and work as a nice background prop in a game. Obviously you could style things up a bit better.

The basic code (see cart for full detail, though):

clock_size=10 --radius of clock
clock_color=12 --blue
hr_hand_color=10 --yellow
min_hand_color=7 --white

-- draw_clock(minute, hour, origin x, origin y, radius)
-- ex: `draw_clock(45,3,50,50,10)` would draw a clock at 50,50 with a 
--    radius of 10 pixels and the hands set accordingly
function draw_clock(m,h,x,y,r)
  -- min hand 1 px smaller than the clock radius
  local mh_len=(r-1)*1.0
  -- hr hand 2 px smaller than the clock radius
  local hh_len=(r-3)*1.0

  m_x,m_y=clock_hand_position(m,x,y,mh_len)
  -- multiply the hour value by 5 for hand position
  h_x,h_y=clock_hand_position(h*5,x,y,hh_len)

  -- draw the outline of the clock
  circ(x,y,r,clock_color)

  -- draw the minute hand
  line(x,y,m_x,m_y,min_hand_color)

  -- draw the hour hand
  line(x,y,h_x,h_y,hr_hand_color)
end

-- clock_hand_position(tick, origin x, origin y, multiplier for hand length)
function clock_hand_position(t,o_x,o_y,m)
  -- rusty trig ahead. beware.
  t-=15
  t=t%60 -- handle 60 tick factors
  t=60-t
  -- determine x,y from cos/sin
  local x=cos(t/60.0)
  local y=sin(t/60.0)
  -- multiply the x & y by `m`
  x*=m
  y*=m
  -- offset from originating coords
  x+=o_x
  y+=o_y
  return x,y
end
P#20178 2016-05-04 18:32 ( Edited 2016-05-04 22:42)


[Please log in to post a comment]

Follow Lexaloffle:          
Generated 2024-03-29 01:08:13 | 0.012s | Q:13