Author Topic: Analog (and digital) Clock  (Read 356 times)

B+

  • Full Member
  • ***
  • Posts: 215
    • View Profile
Analog (and digital) Clock
« on: September 13, 2016 »
Testing KSINK import. Works for me!

Code: [Select]
' clock.txt [B+=MGA] 2016-09-12
' test using this ksink library and dll

import "ksink.lib"
'needs ksink.dll too in same folder

constant:
XMAX = 400
YMAX = 400
XC = 200
YC = 200
HR# = 4.0
HL1# = 175.0
HL2# = 10.0
HhandR# = 15.0
MhandR# = 8.0
ShandR# = 5.0
HhandL# = 105.0
MhandL# = 150.0
ShandL# = 160.0
visible:

hidden:

set window 100, 40, XMAX, YMAX
set redraw false
Pstart = xy2P(XC, YC)
do
set colori 0
cls
'digital time
set colori 0xffffff
set caret 5, 5
h$ = str(GetHour() + 0)
if len(h$) = 1 then h$ = "0" + h$
m$ = str(GetMinute() + 0) 
if len(m$) = 1 then m$ = "0" + m$
s$ = str(GetSecond() + 0)
if len(s$) = 1 then s$ = "0" + s$
wln h$, ":", m$, ":", s$

'allow esc
set caret 360, 380
wln "esc"

'analog time
'draw hour markers
for i = 0 to 11
'this draws an invisble arm out to hour markers
aHR# = float(i * 30) - 90.0
set colori 0
Pnext = quad(Pstart, 1.0, aHR#, HL1# - 10.0, 1.0)
'use Pnext to locate hour marker starts
set colori 0xffffff
Pnext = quad(Pnext, HR#, aHR#, HL2#, HR#)
next

'draw the clock hands
'account for partial hour and minute passed with fractions
minFrac# = float(GetMinute() % 60) / 60.0
secFrac# = float(GetSecond() % 60) / 60.0

'arm angles 0 degrees is due east, so minus 90 is north = 0 for our clock
aHour# = (float(GetHour() % 12) + minFrac#)  / 12.0  * 360.0 - 90.0
aMin# =  (minFrac# + secFrac# / 60.0) * 360.0 - 90.0
aSec# =  secFrac# * 360.0 - 90.0

'draw arms with slightly different shades of gray
set colori 0xf0f0f0
Pnext = quad(Pstart, HhandR#, aHour#, HhandL#, HhandR# - 6.0)
set colori 0xa0a0a0
Pnext = quad(Pstart, MhandR#, aMin#, MhandL#, MhandR# - 3.5)
set colori 0x707070
Pnext = quad(Pstart, ShandR#, aSec#, ShandL#, ShandR# - 3.0)

'draw a center pin
set colori 0
_fcirc XC, YC, 2

redraw
wait 1
until keydown(27, true)


function quad(pStart, r1#, ang#, lng#, r2#)
'solid fill circles at both ends, use polygon 4 points to fill middle area
'draws from pStart at radius r1 down angle ang for length of leg
'with r2 being the radius at end point
'returns the x, y end point in P form for further pendage drawing

'constants XMAX, YMAX
'needs fcirc(x, y, r)
'needs xy2P(x, y)

'extract x, y start line of circles
  fx# = float(pStart % (XMAX + 1))
fy# = float(pStart) / float(XMAX)
x1# = fx# + r1# * cos(ang# + 90.0)
y1# = fy# + r1# * sin(ang# + 90.0)
x2# = fx# + r1# * cos(ang# - 90.0)
y2# = fy# + r1# * sin(ang# - 90.0)

xe# = fx# + lng# * cos(ang#)
ye# = fy# + lng# * sin(ang#)
x3# = xe# + r2# * cos(ang# + 90.0)
y3# = ye# + r2# * sin(ang# + 90.0)
x4# = xe# + r2# * cos(ang# - 90.0)
y4# = ye# + r2# * sin(ang# - 90.0)

_fcirc int(fx#), int(fy#), int(r1#)
_fcirc int(xe#), int(ye#), int(r2#)
'draw quad fill
draw poly[int(x1#), int(y1#), int(x2#), int(y2#), int(x4#), int(y4#), int(x3#), int(y3#)], true
return xy2P(int(xe#), int(ye#))
endfunc

procedure fcirc(x, y, r)
rsq = r * r
for a = 0 to r
b = int(sqr(float(rsq - a * a)))
x1 = x + a; x2 = x - a; y1 = y - b; y2 = y + b
draw line x1, y1, x1, y2
draw line x2, y1, x2, y2
next
endproc

function xy2P(x, y)
'note uses constants XMAX screen width
return y * (XMAX + 1) + x
endfunc


« Last Edit: September 13, 2016 by B+ »
B+

Rick3137

  • Sr. Member
  • ****
  • Posts: 341
  • May the Force be with You
    • View Profile
    • Rick's Programs
Re: Analog (and digital) Clock
« Reply #1 on: September 13, 2016 »
  Works good. Made me realize one thing.

  My computer is 6 seconds slow.