2016 Update
This commit is contained in:
parent
948b86eafa
commit
dcf5d15da3
7965 changed files with 139854 additions and 31002 deletions
|
|
@ -1,7 +1,17 @@
|
|||
'''Task''': draw a clock. More specific:
|
||||
:# Draw a time keeping device. It can be a stopwatch, hourglass, sundial, a mouth counting "one thousand and one", anything. Only showing the seconds is required, e.g. a watch with just a second hand will suffice. However, it must clearly change every second, and the change must cycle every so often (one minute, 30 seconds, etc.) It must be ''drawn''; printing a string of numbers to your terminal doesn't qualify. Both text-based and graphical drawing are OK.
|
||||
:# The clock is unlikely to be used to control space flights, so it needs not be hyper-accurate, but it should be usable, meaning if one can read the seconds off the clock, it must agree with the system clock.
|
||||
:# A clock is rarely (never?) a major application: don't be a CPU hog and poll the system timer every microsecond, use a proper timer/signal/event from your system or language instead. For a bad example, many OpenGL programs update the framebuffer in a busy loop even if no redraw is needed, which is very undesirable for this task.
|
||||
:# A clock is rarely (never?) a major application: try to keep your code simple and to the point. Don't write something too elaborate or convoluted, instead do whatever is natural, concise and clear in your language.
|
||||
;Task:
|
||||
Draw a clock.
|
||||
|
||||
Key points: animate simple object; timed event; polling system resources; code clarity.
|
||||
|
||||
More specific:
|
||||
# Draw a time keeping device. It can be a stopwatch, hourglass, sundial, a mouth counting "one thousand and one", anything. Only showing the seconds is required, e.g.: a watch with just a second hand will suffice. However, it must clearly change every second, and the change must cycle every so often (one minute, 30 seconds, etc.) It must be ''drawn''; printing a string of numbers to your terminal doesn't qualify. Both text-based and graphical drawing are OK.
|
||||
# The clock is unlikely to be used to control space flights, so it needs not be hyper-accurate, but it should be usable, meaning if one can read the seconds off the clock, it must agree with the system clock.
|
||||
# A clock is rarely (never?) a major application: don't be a CPU hog and poll the system timer every microsecond, use a proper timer/signal/event from your system or language instead. For a bad example, many OpenGL programs update the frame-buffer in a busy loop even if no redraw is needed, which is very undesirable for this task.
|
||||
# A clock is rarely (never?) a major application: try to keep your code simple and to the point. Don't write something too elaborate or convoluted, instead do whatever is natural, concise and clear in your language.
|
||||
|
||||
<br>
|
||||
;Key points
|
||||
* animate simple object
|
||||
* timed event
|
||||
* polling system resources
|
||||
* code clarity
|
||||
<br><br>
|
||||
|
|
|
|||
112
Task/Draw-a-clock/Icon/draw-a-clock-1.icon
Normal file
112
Task/Draw-a-clock/Icon/draw-a-clock-1.icon
Normal file
|
|
@ -0,0 +1,112 @@
|
|||
link graphics
|
||||
|
||||
global xsize,
|
||||
ysize,
|
||||
fontsize
|
||||
|
||||
procedure main(args)
|
||||
if *args > 0 then xsize := ysize := numeric(args[1])
|
||||
|
||||
/xsize := /ysize := 200
|
||||
WIN := WOpen("size=" || xsize || "," || ysize, "label=Clock", "resize=on") | stop("Fenster geht nicht auf!", image(xsize), " - ", image(ysize))
|
||||
ziffernblatt()
|
||||
|
||||
repeat
|
||||
{ write(&time)
|
||||
|
||||
if *Pending(WIN) > 1 then while *Pending() > 0 do
|
||||
{ e := Event()
|
||||
ziffernblatt()
|
||||
}
|
||||
|
||||
Fg("#CFB53B")
|
||||
FillCircle(xsize/2, ysize/2, xsize/2 * 0.81)
|
||||
Fg("black")
|
||||
|
||||
clock := &clock
|
||||
sec := clock[7:0]
|
||||
min := clock[4:6]
|
||||
hour := clock[1:3]
|
||||
|
||||
if fontsize > 7 then
|
||||
{ #Fg("yellow")
|
||||
EraseArea(10,0, TextWidth(clock),WAttrib("fheight"))
|
||||
DrawString(10,fontsize, clock)
|
||||
}
|
||||
|
||||
draw_zeiger(hour, min, sec)
|
||||
|
||||
WFlush()
|
||||
delay(100)
|
||||
}
|
||||
end
|
||||
|
||||
procedure ziffernblatt()
|
||||
xsize := WAttrib("width")
|
||||
ysize := WAttrib("height")
|
||||
if xsize < ysize then ysize := xsize
|
||||
if ysize < xsize then xsize := ysize
|
||||
|
||||
EraseArea(0,0,WAttrib("width"),WAttrib("height"))
|
||||
|
||||
Fg("#CFB53B")
|
||||
FillCircle(xsize/2, ysize/2, xsize/2)
|
||||
Fg("black")
|
||||
fontsize := fontsize := 30 * xsize / 800.0
|
||||
|
||||
every i := 1 to 60 do
|
||||
{ winkel := 6 * i / 180.0 * &pi
|
||||
if i % 5 = 0 then
|
||||
{ laenge := 0.95
|
||||
if fontsize > 15 then
|
||||
{ Font("mono," || integer(fontsize) || ",bold")
|
||||
WAttrib("linewidth=3")
|
||||
}
|
||||
if fontsize > 8 then
|
||||
{ Font("sans," || integer(fontsize))
|
||||
WAttrib("linewidth=2")
|
||||
}
|
||||
|
||||
if fontsize > 8 then DrawString(xsize/2 + 0.90 * xsize/2 * sin(winkel) - fontsize / 2, ysize/2 - 0.90 * ysize/2 * cos(winkel) + fontsize/2, (i/5)("I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX", "X", "XI", "XII"))
|
||||
}
|
||||
else laenge := 0.98
|
||||
if fontsize >= 5 then DrawLine(xsize/2 + laenge * xsize/2 * sin(winkel), ysize/2 - laenge * ysize/2 * cos(winkel), xsize/2 + 0.99 * xsize/2 * sin(winkel), ysize/2 - 0.99 * ysize/2 * cos(winkel))
|
||||
if fontsize < 5 then if i % 5 = 0 then
|
||||
{ WAttrib("linewidth=1")
|
||||
DrawLine(xsize/2 + laenge * xsize/2 * sin(winkel), ysize/2 - laenge * ysize/2 * cos(winkel), xsize/2 + 0.99 * xsize/2 * sin(winkel), ysize/2 - 0.99 * ysize/2 * cos(winkel))
|
||||
}
|
||||
}
|
||||
clock := &clock
|
||||
sec := clock[7:0]
|
||||
min := clock[4:6]
|
||||
hour := clock[1:3]
|
||||
|
||||
if fontsize > 7 then
|
||||
{ EraseArea(10,0, TextWidth(clock),WAttrib("fheight"))
|
||||
DrawString(10,fontsize, clock)
|
||||
}
|
||||
draw_zeiger(hour, min, sec)
|
||||
|
||||
Fg("#D4AF37")
|
||||
FillCircle(xsize/2, ysize/2, 5)
|
||||
Fg("black")
|
||||
WAttrib("linewidth=2")
|
||||
|
||||
DrawCircle(xsize/2, ysize/2,5)
|
||||
|
||||
end
|
||||
|
||||
procedure draw(laenge, breite, winkel)
|
||||
WAttrib("linewidth=" || breite)
|
||||
DrawLine(xsize/2,ysize/2,xsize/2 + laenge * sin(winkel), ysize/2 - laenge * cos(winkel))
|
||||
end
|
||||
|
||||
procedure draw_zeiger(h, m, s)
|
||||
wh := 30 * ((h % 12) + m / 60.0 + s / 3600.0) / 180 * &pi
|
||||
wm := 6 * (m + s / 60.0) / 180.0 * &pi
|
||||
ws := 6 * s / 180.0 * &pi
|
||||
|
||||
draw(xsize/2 * 0.5, 5, wh) # Stundenzeiger
|
||||
draw(xsize/2 * 0.65, 3, wm) # Minutenzeiger
|
||||
draw(xsize/2 * 0.80, 1, ws) # Sekundenzeiger
|
||||
end
|
||||
181
Task/Draw-a-clock/Icon/draw-a-clock-2.icon
Normal file
181
Task/Draw-a-clock/Icon/draw-a-clock-2.icon
Normal file
|
|
@ -0,0 +1,181 @@
|
|||
link graphics, turtle
|
||||
|
||||
global xsize,
|
||||
ysize,
|
||||
fontsize
|
||||
|
||||
procedure main(args)
|
||||
if *args > 0 then xsize := ysize := numeric(args[1])
|
||||
|
||||
/xsize := /ysize := 200
|
||||
WIN := WOpen("size=" || xsize || "," || ysize, "label=Clock", "resize=on") | stop("Fenster geht nicht auf!", image(xsize), " - ", image(ysize))
|
||||
ziffernblatt()
|
||||
|
||||
TInit()
|
||||
|
||||
# clocker := create((right("0" || (0 to 23), 2) || ":" || right("0" || (0 to 59), 2) || ":" || right("0" || (0 to 59), 2))) # simul_clock()
|
||||
|
||||
repeat
|
||||
{ write(&time)
|
||||
|
||||
if *Pending(WIN) > 1 then
|
||||
{ while *Pending() > 0 do e := Event()
|
||||
ziffernblatt()
|
||||
}
|
||||
|
||||
Fg("#CFB53B")
|
||||
FillCircle(xsize/2, ysize/2, xsize/2 * 0.81)
|
||||
Fg("black")
|
||||
|
||||
clock := &clock #clock := @clocker
|
||||
sec := clock[7:0]
|
||||
min := clock[4:6]
|
||||
hour := clock[1:3]
|
||||
|
||||
if fontsize > 7 then
|
||||
{ altfg := Fg()
|
||||
Fg("blue")
|
||||
altbg := Bg()
|
||||
Bg("black")
|
||||
|
||||
if fh := open("/etc/timezone", "r") then
|
||||
{ timezone := read(fh)
|
||||
close(fh)
|
||||
}
|
||||
|
||||
erase := TextWidth(clock)
|
||||
erase <:= TextWidth(&date)
|
||||
erase <:= TextWidth(timezone)
|
||||
|
||||
EraseArea(xsize/2 - erase / 2, ysize * 7 / 8, erase, WAttrib("fheight"))
|
||||
DrawString(xsize/2 - TextWidth(clock) / 2,ysize * 7 / 8 + WAttrib("fheight") - WAttrib("descent"), clock)
|
||||
|
||||
EraseArea(xsize/2 - erase / 2, ysize * 7 / 8 - WAttrib("fheight"), erase,WAttrib("fheight"))
|
||||
DrawString(xsize/2 - TextWidth(&date) / 2,ysize * 7 / 8 - WAttrib("fheight") + WAttrib("fheight") - WAttrib("descent"), &date)
|
||||
|
||||
|
||||
EraseArea(xsize/2 - erase / 2, ysize * 7 / 8 - 2 * WAttrib("fheight"), erase,WAttrib("fheight"))
|
||||
DrawString(xsize/2 - TextWidth(timezone) / 2,ysize * 7 / 8 - 2 * WAttrib("fheight") + WAttrib("fheight") - WAttrib("descent"), timezone)
|
||||
|
||||
Bg(altbg)
|
||||
Fg(altfg)
|
||||
}
|
||||
|
||||
draw_zeiger(hour, min, sec)
|
||||
|
||||
Fg("#D4AF37")
|
||||
FillCircle(xsize/2, ysize/2, 5 * xsize / 400.0)
|
||||
Fg("black")
|
||||
WAttrib("linewidth=" || 2 * xsize / 400)
|
||||
|
||||
DrawCircle(xsize/2, ysize/2, 5 * xsize / 400.0)
|
||||
|
||||
WAttrib("linewidth=1")
|
||||
|
||||
WFlush()
|
||||
delay(50)
|
||||
}
|
||||
end
|
||||
|
||||
procedure ziffernblatt()
|
||||
xsize := WAttrib("width")
|
||||
ysize := WAttrib("height")
|
||||
if xsize < ysize then ysize := xsize
|
||||
if ysize < xsize then xsize := ysize
|
||||
|
||||
EraseArea(0,0,WAttrib("width"),WAttrib("height"))
|
||||
|
||||
Fg("#CFB53B")
|
||||
FillCircle(xsize/2, ysize/2, xsize/2)
|
||||
Fg("black")
|
||||
fontsize := fontsize := 30 * xsize / 800.0
|
||||
WAttrib("linewidth=1")
|
||||
|
||||
every i := 1 to 60 do
|
||||
{ winkel := 6 * i / 180.0 * &pi
|
||||
TX(xsize/2)
|
||||
TY(ysize/2)
|
||||
THeading(i * 6)
|
||||
|
||||
if i % 5 = 0 then
|
||||
{ laenge := 0.95
|
||||
if fontsize > 15 then
|
||||
{ Font("mono," || integer(fontsize) || ",bold")
|
||||
WAttrib("linewidth=3")
|
||||
}
|
||||
if fontsize > 8 then
|
||||
{ Font("sans," || integer(fontsize))
|
||||
WAttrib("linewidth=2")
|
||||
}
|
||||
|
||||
if fontsize > 8 then DrawString(xsize/2 + 0.90 * xsize/2 * sin(winkel) - fontsize / 2, ysize/2 - 0.90 * ysize/2 * cos(winkel) + fontsize/2, (i/5)("I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX", "X", "XI", "XII"))
|
||||
}
|
||||
else
|
||||
{ laenge := 0.98
|
||||
if fontsize > 15 then WAttrib("linewidth=3")
|
||||
if fontsize > 8 then WAttrib("linewidth=2")
|
||||
if fontsize < 5 then WAttrib("linewidth=1")
|
||||
|
||||
}
|
||||
if fontsize >= 5 then {TSkip(laenge * xsize/2); TDraw((0.99-laenge) * xsize / 2)} #DrawLine(xsize/2 + laenge * xsize/2 * sin(winkel), ysize/2 - laenge * ysize/2 * cos(winkel), xsize/2 + 0.99 * xsize/2 * sin(winkel), ysize/2 - 0.99 * ysize/2 * cos(winkel))
|
||||
if fontsize < 5 then if i % 5 = 0 then
|
||||
{ WAttrib("linewidth=1")
|
||||
TSkip(laenge * xsize/2); TDraw((0.99-laenge) * xsize / 2) #DrawLine(xsize/2 + laenge * xsize/2 * sin(winkel), ysize/2 - laenge * ysize/2 * cos(winkel), xsize/2 + 0.99 * xsize/2 * sin(winkel), ysize/2 - 0.99 * ysize/2 * cos(winkel))
|
||||
}
|
||||
}
|
||||
clock := &clock
|
||||
sec := clock[7:0]
|
||||
min := clock[4:6]
|
||||
hour := clock[1:3]
|
||||
|
||||
#if fontsize > 7 then
|
||||
#{ EraseArea(10,0, TextWidth(clock),WAttrib("fheight"))
|
||||
# DrawString(10,fontsize, clock)
|
||||
#}
|
||||
draw_zeiger(hour, min, sec)
|
||||
end
|
||||
|
||||
procedure draw(zeiger, laenge, breite, winkel)
|
||||
TX(xsize/2); TY(ysize/2); THeading(winkel - 90)
|
||||
WAttrib("linewidth=" || breite)
|
||||
TDraw(laenge)
|
||||
if zeiger == ("h" | "m" | "s") then
|
||||
{
|
||||
TSkip((0.05 + breite / 250.0) * xsize / 5)
|
||||
WAttrib("linewidth=1")
|
||||
TFPoly((0.05 + breite / 250.0) * xsize,3)
|
||||
}
|
||||
if zeiger == ("h" | "m" | "s") then
|
||||
{ Fg("green yellow")
|
||||
TFPoly(0.04 * xsize, 3)
|
||||
Fg("black")
|
||||
}
|
||||
WAttrib("linewidth=" || breite)
|
||||
|
||||
if zeiger == "r" then
|
||||
{ TSkip(0.025 * xsize)
|
||||
TCircle(0.05 * xsize)
|
||||
}
|
||||
if breite > 7 then
|
||||
{ Fg("green yellow")
|
||||
TX(xsize/2); TY(ysize/2); THeading(winkel -90)
|
||||
TSkip(laenge / 2)
|
||||
TFRect(laenge / 2, breite -5)
|
||||
Fg("black")
|
||||
}
|
||||
end
|
||||
|
||||
procedure draw_zeiger(h, m, s)
|
||||
wh := 30 * ((h % 12) + m / 60.0 + s / 3600.0) #/ 180 * &pi
|
||||
wm := 6 * (m + s / 60.0) #/ 180.0 * &pi
|
||||
ws := 6 * s #/ 180.0 * &pi
|
||||
|
||||
draw("h", xsize/2 * 0.45,20 * xsize / 800, wh) # Stundenzeiger
|
||||
draw("r", xsize/2 * 0.15,20 * xsize / 800, wh - 180)
|
||||
|
||||
draw("m", xsize/2 * 0.60,12 * xsize / 800, wm) # Minutenzeiger
|
||||
draw("r", xsize/2 * 0.20,12 * xsize / 800, wm - 180)
|
||||
|
||||
draw("s", xsize/2 * 0.70, 4 * xsize / 800, ws) # Sekundenzeiger
|
||||
draw("r", xsize/2 * 0.25, 8 * xsize / 800, ws - 180)
|
||||
end
|
||||
Loading…
Add table
Add a link
Reference in a new issue