Draw a clock: Difference between revisions

m
Replace deprecated functions
m (→‎Evaldraw: edit image placement)
m (Replace deprecated functions)
(3 intermediate revisions by 2 users not shown)
Line 6,198:
clear(curr_win, BACKGROUND);
KEYBOARD := GRAPH_KEYBOARD;
command := busy_getcgetc(KEYBOARD, NO_WAIT);
while command <> 'q' do
start_time := truncToSecond(time(NOW));
Line 6,240:
fcircle(100, 100, 7, CLOCKCOLOR);
circle(100, 100, 7, FOREGROUND);
DRAW_FLUSHflushGraphic;
await(start_time + 1 . SECONDS);
command := busy_getcgetc(KEYBOARD, NO_WAIT);
end while;
end func;</syntaxhighlight>
Line 6,643:
{{trans|Kotlin}}
{{libheader|DOME}}
<syntaxhighlight lang="ecmascriptwren">import "graphics" for Canvas, Color
import "dome" for Window
import "math" for Math
Line 6,721:
 
=={{header|Yabasic}}==
===digital clock===
<syntaxhighlight lang="yabasic">clear screen
open window 300,100
Line 6,755 ⟶ 6,756:
exit
end if</syntaxhighlight>
 
===graphical analog clock===
<syntaxhighlight lang="yabasic">
#!/usr/bin/yabasic
REM yaclock
DEG_PER_RAD = 57.257751
 
winx = 480
winy = 480
 
radius = min(winx,winy) / 2 - 1
hx = (winx/2) - 1
hy = (winy/2) - 1
 
REM length of the hands (90% of the radius of the clock face)
shand = int(radius * .9)
mhand = int(radius * .9)
hhand = int(radius * .5)
 
REM drop coords by one since graphics are 0 based
 
winx = winx - 1
winy = winy - 1
 
clear screen
 
open window winx,winy
 
clockface()
 
do
 
hour = val(mid$(time$,1,2))
mins = val(mid$(time$,4,2))
sec = val(mid$(time$,7,2))
updatehand("sec")
updatehand("mins")
updatehand("hour")
 
pause .25
 
loop
 
sub updatehand(hand$)
switch(hand$)
case "sec"
h_len = shand
angle = sec * 6
width = 6
color 255,0,0
ox = osx
oy = osy
oxm1 = osxm1
oxm2 = osxm2
oym1 = osym1
oym2 = osym2
break
case "mins"
h_len = mhand
angle = mins * 6 + int(sec/10)
width = 12
color 0,255,0
ox = omx
oy = omy
oxm1 = omxm1
oxm2 = omxm2
oym1 = omym1
oym2 = omym2
break
case "hour"
h_len = hhand
angle = ((hour * 30) + (minutes / 12) * 6) + int(mins/2)
width = 15
color 0,0,255
ox = ohx
oy = ohy
oxm1 = ohxm1
oxm2 = ohxm2
oym1 = ohym1
oym2 = ohym2
break
end switch
 
h_angle1 = angle - width
if h_angle1 < 0 then
h_angle1 = h_angle1 + 360
endif
h_angle1 = h_angle1 / DEG_PER_RAD
h_angle2 = angle + width
if h_angle2 > 360 then
h_angle2 = h_angle2 - 360
endif
h_angle2 = h_angle2 / DEG_PER_RAD
angle = angle / DEG_PER_RAD
x = (hx + (sin(angle) * h_len))
xm1 = (hx + (sin(h_angle1) * int(h_len * .2)))
xm2 = (hx + (sin(h_angle2) * int(h_len * .2)))
 
y = (hy - (cos(angle) * h_len))
ym1 = (hy - (cos(h_angle1) * int(h_len * .2)))
ym2 = (hy - (cos(h_angle2) * int(h_len * .2)))
 
clear line hx,hy,oxm1,oym1
clear line hx,hy,oxm2,oym2
clear line oxm1,oym1,ox,oy
clear line oxm2,oym2,ox,oy
line hx,hy,xm1,ym1
line hx,hy,xm2,ym2
line xm1,ym1,x,y
line xm2,ym2,x,y
REM save off the old vals
switch(hand$)
case "sec"
osx = x
osy = y
osxm1 = xm1
osxm2 = xm2
osym1 = ym1
osym2 = ym2
break
case "mins"
omx = x
omy = y
omxm1 = xm1
omxm2 = xm2
omym1 = ym1
omym2 = ym2
break
case "hour"
ohx = x
ohy = y
ohxm1 = xm1
ohxm2 = xm2
ohym1 = ym1
ohym2 = ym2
break
end switch
end sub
 
sub clockface()
circle hx,hy,radius
htick = radius / 10
mtick = htick / 2
for z=0 to 360 step 6
REM Begin at zero deg and stop before 360 deg
 
REM draw the hour markers
angle = z
angle = angle / DEG_PER_RAD
x2 = (hx + (sin(angle) * radius))
y2 = (hy - (cos(angle) * radius))
if mod(z,30) = 0 then
tick = htick
else
tick = mtick
endif
x3 = (hx + (sin(angle) * (radius - tick)))
y3 = (hy - (cos(angle) * (radius - tick)))
color 255,0,0
line x2,y2,x3,y3
color 0,0,0
next z
end sub
 
</syntaxhighlight>
 
=={{header|zkl}}==
29

edits