Draw a clock: Difference between revisions
Content added Content deleted
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
|||
Line 21:
=={{header|ActionScript}}==
<syntaxhighlight lang="actionscript">
package {
Line 160:
}
</syntaxhighlight>
=={{header|Ada}}==
{{libheader|SDLAda}}
<
with Ada.Calendar.Formatting;
with Ada.Calendar.Time_Zones;
Line 262:
Window.Finalize;
SDL.Finalise;
end Draw_A_Clock;</
=={{header|AutoHotkey}}==
Line 268:
this code from http://www.autohotkey.com/forum/viewtopic.php?p=231836#231836
draws a very nice clock with GDI+
<
; Parts based on examples from Tic's GDI+ Tutorials and of course on his GDIP.ahk
Line 412:
Gdip_Shutdown(pToken)
ExitApp
Return</
=={{header|AWK}}==
<syntaxhighlight lang="awk">
# syntax: GAWK -f DRAW_A_CLOCK.AWK [-v xc="*"]
BEGIN {
Line 466:
}
}
</syntaxhighlight>
{{out|Sample run and output}}
<pre>
Line 485:
==={{header|AmigaBASIC}}===
<
CIRCLE (xp,yp),size,,,,.5
lasth=0:lastm=0:lasts=0
Line 516:
LINE (xp,yp)-(xp+2*ms*SIN(lastm),yp-ms*COS(lastm)),1
LINE (xp,yp)-(xp+2*ss*SIN(lasts),yp-ss*COS(lasts)),2
RETURN</
==={{header|BaCon}}===
Using GTK3 as a graphical toolkit.
<
PRAGMA GUI gtk3
Line 594:
ENDSUB
</syntaxhighlight>
==={{header|BBC BASIC}}===
<
CLS
xp=320:yp=160:size=150
Line 634:
LINE xp, yp, xp+2*ss*SIN(lasts), yp+ss*COS(lasts)
ENDPROC
</syntaxhighlight>
==={{header|Commodore BASIC}}===
To be entered in upper/lowercase mode but run in uppercase + graphics mode.
<
20 ti$ = "123456"
25 rem do some other stuff after this line
Line 670:
1540 z$(4) = "B B B B B B BB BB B B"
1550 z$(5) = "JCKCCCJCCCCK BCCKJCKJCK CK"
1560 return</
==={{header|IS-BASIC}}===
<
110 OPTION ANGLE DEGREES
120 LET CH=1:LET CH2=2
Line 693:
290 CLOSE #2
300 CLOSE #1
310 TEXT</
=={{header|Batch File}}==
<
::Batch File Implementation
::
Line 802:
echo.
timeout /t 1 /nobreak >nul
goto :clock_loop</
{{Out}}
<pre>
Line 814:
=={{header|C}}==
Draws a crude clock in terminal. C99, compiled with <code>gcc -std=c99</code>.
<
#include <stdlib.h>
#include <math.h>
Line 906:
draw(s);
return 0;
}</
===Clock in xlib (for X windows)===
<
// # Makefile
Line 1,121:
}
// END</
=={{header|C sharp|C#}}==
<
using System.Drawing;
using System.Drawing.Drawing2D;
Line 1,210:
Application.Run(new Clock());
}
}</
=={{header|C++}}==
[[File:clock_cpp.png]]
<
#include <windows.h>
#include <string>
Line 1,545:
}
//--------------------------------------------------------------------------------------------------
</syntaxhighlight>
=={{header|ContextFree}}==
<syntaxhighlight lang="text">
startshape START
Line 1,564:
}
</syntaxhighlight>
=={{header|Delphi}}==
Line 1,576:
Form application
<syntaxhighlight lang="delphi">
unit main;
Line 1,767:
end.
</syntaxhighlight>
Resources:
<syntaxhighlight lang="delphi">
object Clock: TClock
Left = 0
Line 1,795:
end
end
</syntaxhighlight>
Line 1,805:
[https://easylang.online/apps/clock.html Run it]
<syntaxhighlight lang="text"># Clock
#
func draw hour min sec . .
Line 1,857:
timer 0.1
.
timer 0</
=={{header|F_Sharp|F#}}==
<
let numberTemplate = """
Line 1,902:
System.Console.ReadLine() |> ignore // Until return is hit
showTime ()
0</
{{out}}
<pre> _ _ _ _ __
Line 1,915:
3. Assumes a 16 bit CPU.<BR>
4. Assumes big-endian memory organization.<BR>
<syntaxhighlight lang="forth">
HEX
8379 CONSTANT TICKER \ address of 1/60 second counter
Line 1,979:
?TERMINAL
UNTIL
2DROP ;</
=={{header|Fortran}}==
Line 1,985:
Uses system commands to clear the screen, sleep and obtain time
<syntaxhighlight lang="fortran">
!Digital Text implemented as in C version - Anant Dixit (Oct, 2014)
program clock
Line 2,102:
end subroutine
</syntaxhighlight>
Preview:
Line 2,120:
=={{header|FreeBASIC}}==
<
' compile with: fbc -s gui
Line 2,202:
ImageDestroy(clockdial)
End</
=={{header|FunL}}==
<
val ROW = 10
Line 2,272:
if not $os.startsWith( 'Windows' )
print( '\u001B[?25h' )</
{{out}}
Line 2,282:
=={{header|Go}}==
<
import (
Line 2,369:
y := float64(radius) * math.Sin(theta)
return int(x) + 256, int(y) + 256
}</
The following html file, 'clock.html', should be in the same folder as the wsclock binary.
<
<meta charset="utf-8" />
<title>Clock</title>
Line 2,471:
</body>
</html></
=={{header|GUISS}}==
<syntaxhighlight lang
<syntaxhighlight lang
=={{header|Haskell}}==
{{libheader|ansi-terminal}}
<
import Data.List
import System.Time
Line 2,594:
setCursorColumn 0
cursorUp 5
main</
Output:<pre> ██ ██████ ██████ ██████ ██████ ██████
██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██
Line 2,608:
1. Clock using conventional Graphics
<
link graphics
Line 2,722:
end
</syntaxhighlight>
2. Clock using Turtle Graphics
<
link graphics, turtle
Line 2,907:
draw("r", xsize/2 * 0.25, 8 * xsize / 800, ws - 180)
end
</syntaxhighlight>
=={{header|J}}==
Graphical clock (tested on jqt -- j903):
<syntaxhighlight lang="j">{{
require'gl2'
coinsert 'jgl2'
Line 2,945:
pshow;
}}
}}1</
Some alternatives:
<syntaxhighlight lang="j">
Note'rudimentary 4 second clock'
advances an arrow at roughly 1 second intervals,
Line 2,970:
tic=: (>. draw Pass_y <.) ([: seconds 0 $ delay@1:)
</syntaxhighlight>
The result of 3.18... is the session time at which the example began.
<pre>
Line 2,987:
Here's a graphical variant (caution: this update mechanism fails on newer J implementations, partially because of version drift in the underlying Qt mechanisms and how those shortcomings have resulted in interface changes):
<
N=:0.01*i.629
O=: [: j./ 1 2 o./ ]
Line 2,993:
delay=:6!:3 NB. "sleep"
clock=: [: plot (O N),N*/~0.07 0.11 0.15(*O) 2r24p1 2r60p1 2r60p1*_3{.6!:0 bind ''
delay@1:@clock^:9e99''</
=={{header|Java}}==
{{works with|Java|8}}
<
import java.awt.event.*;
import static java.lang.Math.*;
Line 3,077:
});
}
}</
=={{header|JavaScript}}==
Tested on Gecko. Put the following in a <script> tag somewhere, and call <code>init_clock()</code> after body load.
<
function update_clock() {
var t = new Date();
Line 3,127:
window.setInterval(update_clock, 200);
}</
=== digital ===
<
<html lang="en">
<head>
Line 3,262:
</body>
</html></
=={{header|Julia}}==
<
using Gtk, Colors, Graphics, Dates
Line 3,349:
sleep(1.0)
end
</syntaxhighlight>
=={{header|Kotlin}}==
{{trans|Java}}
<
import java.awt.*
Line 3,423:
f.isVisible = true
}
}</
=={{header|Lambdatalk}}==
The {watch} expression displays three thick arc of circles, red for hours, green for minutes and blue for seconds, growing from 0° to 360° according to the time, and the full date inside following this format yyy/mm/dd hh:mm:ss. The output can be seen in http://lambdaway.free.fr/lambdawalks/?view=watch
<
1) lambdatalk code
{watch} // displays the watch
Line 3,501:
};
}
</syntaxhighlight>
=={{header|Liberty BASIC}}==
LB has a timer to call a routine at regular intervals. The example is a cut-down version of the full clock supplied with LB as an example.
<syntaxhighlight lang="lb">
WindowWidth =120
WindowHeight =144
Line 3,545:
end
</syntaxhighlight>
=={{header|Locomotive Basic}}==
Line 3,551:
Because the Amstrad CPC does not have an RTC, we first have to ask the user for the current time. The seconds hand is drawn in XOR ink mode so that it can be removed without affecting the other hands.
<
20 input "Current time (HH:MM)";t$
30 h=val(mid$(t$,1,2))
Line 3,596:
440 if a>0 then frame:move 0,0:draw .8*r*sin(a-6),.8*r*cos(a-6),3,1
450 frame:move 0,0:draw .8*r*sin(a),.8*r*cos(a),3,1
460 return</
=={{header|Lua}}==
Line 3,603:
=={{header|Mathematica}} / {{header|Wolfram Language}}==
<
=={{header|MATLAB}} / {{header|Octave}}==
<
while(1)
s = mod(now*60*24,1)*2*pi;
plot([0,sin(s)],[0,cos(s)],'-',sin(u),cos(u),'k-');
pause(1);
end;</
=={{header|MiniScript}}==
Line 3,618:
This solution works with [https://miniscript.org/MiniMicro Mini Micro], and uses its default SpriteDisplay.
<
gfx.clear color.clear
gfx.fillPoly [[60,5], [64,10], [128,5], [64,0]], color.yellow
Line 3,643:
hand.rotation = 90 - floor(time) % 60 * 6
wait
end while</
=={{header|NetRexx}}==
{{trans|Java}}
<
options replace format comments java crossref symbols binary
Line 3,749:
repaint()
return
</syntaxhighlight>
=={{header|Nim}}==
==={{header|Text}}===
{{trans|Raku}}
<
const
Line 3,767:
for c in x: stdout.write b[c.ord - '0'.ord]
echo ""
sleep 1000</
==={{header|Using SDL}}===
{{libheader|SDL2}}
<
import sdl2, times, math
Line 3,833:
destroy render
destroy window</
=={{header|OCaml}}==
Line 3,839:
Using only the standard library of OCaml with its [http://caml.inria.fr/pub/docs/manual-ocaml/libref/Graphics.html Graphics] module:
<
#load "unix.cma"
#load "graphics.cma"
Line 3,903:
in
try loop ()
with _ -> close_graph ()</
Line 3,919:
unix.cmxa lablgtk.cmxa cairo.cmxa cairo_lablgtk.cmxa gtkInit.cmx gtkclock.ml
<
let angle v max = float v /. max *. 2.0 *. pi
Line 3,984:
animate area;
w#show ();
GMain.main ()</
=={{header|ooRexx}}==
Line 3,991:
<br>https://www.dropbox.com/sh/h0dycdshv04c5lz/5oHFfI3t14?n=132389230
<br>It runs nicely on Windows 7 with ooRexx installed.
<
* 09.02.2014 Walter Pachl with a little, well considerable, help from
* a friend (Mark Miesfeld)
Line 4,269:
Parse Arg x,y
If y=0 Then Return '??'
Else Return x/y</
===version 2 runs under Windows, Linux, and MacOSX===
Line 4,275:
A screenshot of this clock can be seen on my dropbox (clocka.jpg)
<br>https://www.dropbox.com/sh/h0dycdshv04c5lz/5oHFfI3t14?n=132389230
<
Name: clock.rxj
Purpose: create a graphical clock that shows the current time
Line 4,417:
::method actionPerformed -- this event will be caused every second by the swing Timer
use arg eventObj, slotDir
slotDir~userData~repaint -- fetch the Java object and send it the repaint message</
[[out}}
<pre>... [2017-01-26T17:17:51.527000] Rexx main program, now waiting until JFrame gets closed ...
Line 4,424:
=={{header|Perl}}==
{{trans|Raku}}
<
binmode STDOUT, ':utf8'; # allow printing wide chars without warning
$|++; # disable output buffering
Line 4,452:
sub clear { print "\e[H\e[J" }
sub position { printf "\e[%d;%dH", shift, shift }</
{{out}}
Line 4,466:
Resizeable, appearance similar to Mathematica.
You can run this online [http://phix.x10.mx/p2js/clock.htm here].
<!--<
<span style="color: #000080;font-style:italic;">--
-- demo\rosetta\Clock.exw
Line 4,593:
<span style="color: #000000;">main</span><span style="color: #0000FF;">()</span>
<!--</
The distribution also contains demo\tinEWGdemo\tindemo\clock.exw, which is a win32-only digital affair (whereas the above should be fine on 32/64 and win/lnx).
=={{header|PicoLisp}}==
This is an animated ASCII drawing of the "Berlin-Uhr", a clock built to display the time according to the principles of set theory, which is installed in Berlin since 1975. See [http://www.surveyor.in-berlin.de/berlin/uhr/indexe.html www.surveyor.in-berlin.de/berlin/uhr/indexe.html].<
(for L Lst
(for X L
Line 4,644:
(bigBox (% (cadr Time) 5))
(draw (+ (10 . -) + (10 . -) + (10 . -) + (10 . -) +)) )
(wait 1000) )</
<pre> _____
/ \
Line 4,667:
This simple example of an analog wall clock uses the Processing built-in time functions second(), minute(), and hour(). For each hand it rotates the sketch canvas and then draws a straight line.
<
drawClock();
}
Line 4,685:
strokeWeight(4);
line(0, 0, 0, -width*0.2);
}</
The sketch redraws at Processing's default 60fps. To redraw the screen only when the second hand changes, add a global variable and change draw() as follows:
<
void draw() {
if (lastSec!=second()) {
Line 4,695:
lastSec=second();
}
}</
One of the official Processing language examples is a more graphically detailed [https://processing.org/examples/clock.html Clock example].
==={{header|Processing Python mode}}===
<
last_sec = second()
Line 4,723:
rotate(-m + h)
strokeWeight(4)
line(0, 0, 0, -width * 0.2)</
=={{header|PureBasic}}==
[[File:PureBasic_Clock.png|thumb|Sample display of PureBasic solution]]
<
#MiddleY = #MiddleX
#len_sh = (#MiddleX - 8) * 0.97 ;length of second-hand
Line 4,777:
SetGadgetState(#clock_gad, ImageID(#clockFace_img))
EndIf
Until event = #PB_Event_CloseWindow</
=={{header|Python}}==
Line 4,784:
===Textmode===
<
def chunks(l, n=5):
Line 4,814:
print bin_bit(y[1])
print
print secs(s)</
==={{libheader|VPython}}===
Line 4,824:
Draws an analog clock in a new GUI window:
<
#lang racket/gui
Line 4,880:
(send f show #t)
</syntaxhighlight>
=={{header|Raku}}==
(formerly Perl 6)
<syntaxhighlight lang="raku"
my $v = floor $rows / 2;
my $h = floor $cols / 2 - 16;
Line 4,900:
print "\e[H";
sleep 1;
}</
{{out}}
<pre>⠀⢺⠀ ⢀⠔⡇ ⠀⠶⠀ ⠊⠉⡱ ⠊⣉⡱ ⠀⠶⠀ ⣏⣉⡉ ⡎⢉⢵
Line 4,907:
=={{header|Red}}==
===Minimalistic===
<
view [t: h4 rate 1 on-time [t/data: now/time]]
</syntaxhighlight>
{{out}}
[https://raw.githubusercontent.com/Palaing/redlib/master/games/images/miniclock.png mini clock image]
===Analog===
<
Needs: 'View
Purpose: {simple analog clock based on Nenad Rakocevic's eve-clock.red,
Line 4,931:
sec/2: 6 * time/second
]]
</syntaxhighlight>
{{out}}
[https://raw.githubusercontent.com/Palaing/redlib/master/games/images/analogclock.png analog clock image]
Line 4,950:
:::* '''ROO'''
the color of the display can be specified.
<
trace off /*turn off tracing/possible host errors*/
parse arg ! /*obtain optional arguments from the CL*/
Line 5,027:
noValue: !sigl= sigl; call er 17,!fid(2) !fid(3) !sigl condition('D') sourceline(!sigl)
p: return word( arg(1), 1)
syntax: !sigl= sigl; call er 13,!fid(2) !fid(3) !sigl !cal() condition('D') sourceline(!sigl)</
;Programming notes:
The '''$CLOCK.REX''' REXX program makes use of '''$T.REX''' REXX program which is used to display text and/or create big blocked characters.
Line 5,081:
{{libheader|Shoes}}
[[File:shoes_clock.png|thumb|Sample display of Ruby solution]]
<
def draw_ray(width, start, stop, ratio)
angle = Math::PI * 2 * ratio - Math::PI/2
Line 5,121:
animate(5) {update}
end</
Inspired by the PicoLisp solution, here's an implementation of the Berlin-Uhr clock.
[[File:berlin_uhr.rb.png|thumb|Berlin-Uhr clock]]
<
background lightgrey
Line 5,169:
end
end
end</
{{libheader|RubyGems}}
{{libheader|JRubyArt}}
JRubyArt is port of processing to ruby
<
def setup
sketch_title 'Clock'
Line 5,221:
end
</syntaxhighlight>
=={{header|Run BASIC}}==
[[File:Rb_clock.png|thumb|Sample display of RB solution]]
<
' clock. I got nothing but time
' ---------------------------------------------
Line 5,319:
#g2 circle(2)
#g2 line(100,100,px,py)
RETURN</
=={{header|Rust}}==
<
extern crate time;
Line 5,350:
fn clear_screen() {
println!("{}[H{}[J", 27 as char, 27 as char);
}</
=={{header|Scala}}==
===Circular ASCII clock===
Generates and prints a simple ASCII clock every second
<
import java.time.LocalTime
import scala.math._
Line 5,407:
}
(new Timer).schedule(timerTask, 0, 1000)
}</
===Berliner Uhr===
See [[http://en.wikipedia.org/wiki/Mengenlehreuhr The Berlin set theory clock]]
<
import java.awt.{ Color, Graphics }
Line 5,459:
}
}
}</
=={{header|Scheme}}==
Line 5,467:
The program displays an analogue clock with three hands, updating once a second.
<syntaxhighlight lang="scheme">
(import (scheme base)
(scheme inexact)
Line 5,527:
(hands canvas))
(tk-event-loop tk))
</syntaxhighlight>
=={{header|Scratch}}==
Line 5,536:
=={{header|Seed7}}==
The example program clock3.sd7 from the Seed7 package can be used for this task.
<
include "float.s7i";
include "math.s7i";
Line 5,606:
command := busy_getc(KEYBOARD);
end while;
end func;</
=={{header|Sidef}}==
{{trans|Perl}}
<
var (rows, cols) = `stty size`.nums...
Line 5,640:
print position(1, 1)
Sys.sleep(0.1)
}</
{{out}}
<pre>
Line 5,649:
=={{header|SVG}}==
<
<circle cx="50" cy="50" r="48" style="fill:peru; stroke:black; stroke-width:2" />
<g transform="translate(50,50) rotate(0)" style="fill:none; stroke-linecap:round">
Line 5,683:
<circle cx="50" cy="50" r="4" style="fill:gold; stroke:black; stroke-width:1" />
</svg>
</syntaxhighlight>
=={{header|Tcl}}==
[[File:Clock tcltk.png|thumb|Sample display of Tcl solution]]
{{libheader|Tk}}
<
package require Tk
Line 5,720:
}
}
ticker</
Note that though this code does poll the system timer approximately four times a second, this is a cheap operation; the GUI update (the relatively expensive part) only happens once a second. The amount of system processing power consumed by this code isn't noticeable on my system; it vanishes with respect to the other processing normally happening.
=={{header|VBScript}}==
The only way to do animation in VBScript is to use ANSI codes in the console. The program will work only in Windows 10 or up. Should be invoked from cscript
<syntaxhighlight lang="vb">
'ANSI Clock
Line 5,797:
wend
</syntaxhighlight>
{{out}}
<pre>
Line 5,831:
{{trans|Kotlin}}
{{libheader|DOME}}
<
import "dome" for Window
import "math" for Math
Line 5,906:
}
var Game = Clock.new(0, 0, 0) // start at midnight</
=={{header|Yabasic}}==
<
open window 300,100
backcolor 0, 0, 0
Line 5,942:
until(upper$(inkey$(.01))="ESC")
exit
end if</
=={{header|zkl}}==
{{trans|Nim}}
<
t=T("⡎⢉⢵","⠀⢺⠀","⠊⠉⡱","⠊⣉⡱","⢀⠔⡇","⣏⣉⡉","⣎⣉⡁","⠊⢉⠝","⢎⣉⡱","⡎⠉⢱","⠀⠶⠀"),
b=T("⢗⣁⡸","⢀⣸⣀","⣔⣉⣀","⢄⣀⡸","⠉⠉⡏","⢄⣀⡸","⢇⣀⡸","⢰⠁⠀","⢇⣀⡸","⢈⣉⡹","⠀⠶ ");
Line 5,955:
println(x.pump(String,t.get),"\n",x.pump(String,b.get));
Atomic.sleep(1);
}</
{{out}}
<pre>
Line 5,964:
=={{header|ZX Spectrum Basic}}==
Chapter 18 of the BASIC manual supplied with the ZX Spectrum includes two programs to implement a clock - each uses different timing methods. The first - using a PAUSE command to hold for a second - is far less accurate, while the second - reading the three-byte system frames counter - is more CPU hungry (since ZX Spectrum Basic can't multitask, this doesn't really matter). With a tweak, the second is shown below.
<
20 FOR n=1 TO 12
30 PRINT AT 10-10*COS (n/6*PI),16+10*SIN (n/6*PI);n
Line 5,977:
210 IF INT t<=INT t1 THEN GO TO 200: REM wait for time for next hand; the INTs were not in the original but force it to wait for the next second
220 PLOT 131,91: DRAW OVER 1;sx,sy: REM rub out old hand
230 LET t1=t: GO TO 120</
{{omit from|ACL2|No access to system time}}
|