Raster bars: Difference between revisions

m (added whitespace.)
Line 263:
</lang>
 
=={{header|Nim}}==
{{trans|Julia}}
{{libheader|gintro}}
<lang Nim>import gintro/[gobject, glib, gdk, gtk, gio, cairo]
 
 
const Palette = [[166.0, 124.0, 0.0],
[191.0, 155.0, 48.0],
[255.0, 191.0, 0.0],
[255.0, 207.0, 64.0],
[255.0, 220.0, 115.0]]
 
type
 
# Description of the simulation.
Simulation = ref object
area: DrawingArea
horizontal: bool
paletteIndex: Natural
count: int
 
Color = array[3, float]
 
 
#---------------------------------------------------------------------------------------------------
 
proc newSimulation(area: DrawingArea): Simulation {.noInit.} =
## Allocate and initialize the simulation object.
Simulation(area: area, horizontal: true, paletteIndex: 0, count: 0)
 
#---------------------------------------------------------------------------------------------------
 
proc bar(ctx: cairo.Context; x1, y1, x2, y2: int; c: Color) =
## Draw a bar.
ctx.setSource(c)
ctx.rectangle(x1.toFloat, y1.toFloat, (x2 - x1 + 1).toFloat, (y2 - y1 + 1).toFloat)
ctx.fill()
 
#---------------------------------------------------------------------------------------------------
 
proc draw(sim: Simulation; ctx: cairo.Context) =
## Draw the bars.
 
let width = sim.area.window.width()
let height = sim.area.window.height()
 
if sim.horizontal:
for i in countup(0, height - 4, 3):
ctx.bar(0, i, width - 1, i + 3, Palette[sim.paletteIndex])
sim.paletteIndex = (sim.paletteIndex + 1) mod 5
 
else:
for i in countup(0, width - 4, 3):
ctx.bar(i, 0, i + 3, height - 1, Palette[sim.paletteIndex])
sim.paletteIndex = (sim.paletteIndex + 1) mod 5
 
#---------------------------------------------------------------------------------------------------
 
proc update(sim: Simulation): gboolean =
## Update the simulation state.
 
sim.draw(sim.area.window.cairoCreate())
sim.area.showAll()
sim.paletteIndex = (sim.paletteIndex + 2) mod 5
inc sim.count
if sim.count mod 30 == 29:
sim.horizontal = not sim.horizontal
result = gboolean(1)
 
#---------------------------------------------------------------------------------------------------
 
proc activate(app: Application) =
## Activate the application.
 
let window = app.newApplicationWindow()
window.setSizeRequest(500, 500)
window.setTitle("Raster Bar Demo")
 
let area = newDrawingArea()
window.add(area)
 
let sim = newSimulation(area)
timeoutAdd(50, update, sim)
 
window.showAll()
 
#———————————————————————————————————————————————————————————————————————————————————————————————————
 
let app = newApplication(Application, "Rosetta.rasterbars")
discard app.connect("activate", activate)
discard app.run()</lang>
 
=={{header|Phix}}==
Anonymous user