audiohijack [Lexaloffle Blog Feed] analog clock example <p> <table><tr><td> <a href="/bbs/?pid=20178#p"> <img src="/bbs/thumbs/pico20176.png" style="height:256px"></a> </td><td width=10></td><td valign=top> <a href="/bbs/?pid=20178#p"> analog clock example</a><br><br> by <a href="/bbs/?uid=12682"> audiohijack</a> <br><br><br> <a href="/bbs/?pid=20178#p"> [Click to Play]</a> </td></tr></table> </p> <p>Just an example of an analog clock you are welcome to use in any project.</p> <p>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.</p> <p>The basic code (see cart for full detail, though):</p> <div> <div class=scrollable_with_touch style="width:100%; max-width:800px; overflow:auto; margin-bottom:12px"> <table style="width:100%" cellspacing=0 cellpadding=0> <tr><td background=/gfx/code_bg1.png width=16><div style="width:16px;display:block"></div></td> <td background=/gfx/code_bg0.png> <div style="font-family : courier; color: #000000; display:absolute; padding-left:10px; padding-top:4px; padding-bottom:4px; "> <pre> 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 &amp; y by `m` x*=m y*=m -- offset from originating coords x+=o_x y+=o_y return x,y end </pre></div></td> <td background=/gfx/code_bg1.png width=16><div style="width:16px;display:block"></div></td> </tr></table></div></div> Wed, 04 May 2016 18:32:27 UTC