Draw a clock: Difference between revisions

m
Replace deprecated functions
m (→‎{{header|Quackery}}: tweaked text)
m (Replace deprecated functions)
 
(8 intermediate revisions by 4 users not shown)
Line 1,997:
timer 0
</syntaxhighlight>
 
 
=={{header|Evaldraw}}==
 
A clock that shows hours, minutes and seconds all moving each millisecond.
 
[[File:Clock.png|Hour minute and second hands move on milli second ticks|right]]
 
<syntaxhighlight lang="C">
(){
cls(0);
drawclock(150);
}
drawclock(rad) {
hand_color = 0x6a6a6a;
seconds_color = 0xff0000;
radius_notches = rad*.95;
radius_numbers = rad*.85;
small_notch_size =rad*0.04;
big_notch_size = rad*0.08;
cx=rad+1;
cy=rad+1;
// Face background
setcol(0x5aaaaa); drawsph(cx,cy,rad);
 
// Highlight from stopwatch.kc
setcol(0xffffff); gldisable(GL_DEPTH_TEST);
glbegin(GL_COMPLEX); glsettex("whitepix.tga");
for(a=3.5;a<=4.5;a+=.25) { gltexcoord(0,0); glvertex(cos(a)*rad*.55+cx,sin(a)*rad*.55+cy); }
for(a=4.5;a>=3.5;a-=.25) { gltexcoord(0,0); glvertex(cos(a)*rad*.45+cx,sin(a)*rad*.45+cy); }
glend();
 
moveto(cx-4*6, cy-.5*rad);
setfont(6,8);
setcol(0); printf("Evaltime");
// Face
setcol(0x015151);
hr=0;
for(i=0; i<60; i++)
{
a = i/60*2*pi - pi/2;
ca=cos(a);
sa=sin(a);
if (i%5==0)
{
hour = hr; if (hour==0) hour=12;
draw_hour(cx,cy,ca,sa,radius_numbers,hour);
hr++;
r=radius_notches;
x=cx + ca*r; y=cy+sa*r;
drawcone(x,y,big_notch_size*.5,x+big_notch_size*ca,y+big_notch_size*sa,-big_notch_size*.5,0);
} else {
r=radius_notches;
x=cx + ca*r; y=cy+sa*r;
drawcone(x,y,small_notch_size*.5,x+small_notch_size*ca,y+small_notch_size*sa,-small_notch_size*.5,0);
}
}
int_hours = klock(6);
int_minutes = klock(7);
int_seconds = klock(8);
int_millis = klock(9);
// Hour and Minute hand
hours = (int_hours+int_minutes/60.0) / 24.0;
a = hours * 2*pi - pi/2;
drawhand(cx,cy,a, rad*.64, 6,4, hand_color);
a = ( (int_minutes+int_seconds/60+int_millis/1000/60) / 60) * 2*pi - pi/2;
drawhand(cx,cy,a, rad*.84, 4,2, hand_color);
a = ((int_seconds+int_millis/1000) / 60) * 2*pi - pi/2;
drawhand(cx,cy,a, rad*.9, 3,1, seconds_color);
}
 
draw_hour(cx,cy,ca,sa,r,hr) {
x=cx + ca*r; y=cy+sa*r;
ofs=0; if(hr>9 || hr==0) ofs=5;
setfont(9,16);
moveto(x-4.5-ofs,y-8);
printf("%g", hr);
}
drawhand(cx,cy,angle,forward,r0,r1,kolor) {
back = .19*forward;
sx=cx - cos(angle)*back;
sy=cy - sin(angle)*back;
ex=cx + cos(angle)*forward;
ey=cy + sin(angle)*forward;
setcol(0);
drawcone(sx,sy,r0+1,ex,ey,r1+1);
setcol(kolor);
drawsph(cx,cy,r0+3);
drawcone(sx,sy,-r0,ex,ey,r1);
}</syntaxhighlight>
 
=={{header|F_Sharp|F#}}==
Line 5,419 ⟶ 5,516:
<pre>⠀⢺⠀ ⢀⠔⡇ ⠀⠶⠀ ⠊⠉⡱ ⠊⣉⡱ ⠀⠶⠀ ⣏⣉⡉ ⡎⢉⢵
⢀⣸⣀ ⠉⠉⡏ ⠀⠶⠀ ⣔⣉⣀ ⢄⣀⡸ ⠀⠶⠀ ⢄⣀⡸ ⢗⣁⡸</pre>
 
A simpler version that does not clear the screen:
 
<syntaxhighlight lang=raku>constant @t = < ⡎⢉⢵ ⠀⢺⠀ ⠊⠉⡱ ⠊⣉⡱ ⢀⠔⡇ ⣏⣉⡉ ⣎⣉⡁ ⠊⢉⠝ ⢎⣉⡱ ⡎⠉⢱ ⠀⠶⠀>;
constant @b = < ⢗⣁⡸ ⢀⣸⣀ ⣔⣉⣀ ⢄⣀⡸ ⠉⠉⡏ ⢄⣀⡸ ⢇⣀⡸ ⢰⠁⠀ ⢇⣀⡸ ⢈⣉⡹ ⠀⠶⠀>;
 
signal(SIGINT).tap: { print "\e[?25h\n"; exit }
print "\e7\e[?25l"; # saves cursor position, make it invisible
loop {
my @x = DateTime.now.Str.substr(11,8).ords X- ord('0');
put ~.[@x] for @t, @b;
sleep 1;
print "\e8"; # restores cursor position
}</syntaxhighlight>
 
Finally a more minimalist version that shows three progress bars (hours, minutes, seconds):
<syntaxhighlight lang=raku>print "\e7";
loop {
my $time = DateTime.now;
put '#' x $_ ~ '.' x (24 - $_) given $time.hour.round;
put '#' x $_ ~ '.' x (60 - $_) given $time.minute.round;
put '#' x $_ ~ '.' x (60 - $_) given $time.second.round;
sleep 1;
print "\e8";
}
END put "\n";</syntaxhighlight>
 
=={{header|Red}}==
Line 6,075 ⟶ 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,117 ⟶ 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,520 ⟶ 6,643:
{{trans|Kotlin}}
{{libheader|DOME}}
<syntaxhighlight lang="ecmascriptwren">import "graphics" for Canvas, Color
import "dome" for Window
import "math" for Math
Line 6,598 ⟶ 6,721:
 
=={{header|Yabasic}}==
===digital clock===
<syntaxhighlight lang="yabasic">clear screen
open window 300,100
Line 6,632 ⟶ 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