How to resolve the algorithm Cistercian numerals step by step in the Julia programming language
How to resolve the algorithm Cistercian numerals step by step in the Julia programming language
Table of Contents
Problem Statement
Cistercian numerals were used across Europe by Cistercian monks during the Late Medieval Period as an alternative to Roman numerals. They were used to represent base 10 integers from 0 to 9999. All Cistercian numerals begin with a vertical line segment, which by itself represents the number 0. Then, glyphs representing the digits 1 through 9 are optionally added to the four quadrants surrounding the vertical line segment. These glyphs are drawn with vertical and horizontal symmetry about the initial line segment. Each quadrant corresponds to a digit place in the number: Please consult the following image for examples of Cistercian numerals showing each glyph: [1] Due to the inability to upload images to Rosetta Code as of this task's creation, showing output here on this page is not required. However, it is welcomed — especially for text output.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Cistercian numerals step by step in the Julia programming language
The provided Julia code creates a graphical user interface (GUI) with a canvas and displays a moon cipher, which is a representation of numbers as digits and lines. Here's a detailed explanation of the code:
-
Package Import:
using Gtk, Cairo
This code imports the Gtk and Cairo packages, which are used for creating GUI elements and drawing on a canvas, respectively.
-
Canvas and Window Creation:
const can = GtkCanvas(800, 100) const win = GtkWindow(can, "Canvas")
A canvas (
can
) with a width of 800 pixels and height of 100 pixels is created usingGtkCanvas
. This canvas is then placed inside a window (win
) with the title "Canvas" usingGtkWindow
. -
Numerals:
const numbers = [0, 1, 20, 300, 4000, 5555, 6789, 8123]
An array
numbers
is defined, containing various numbers that will be displayed in the moon cipher representation. -
Custom Drawing Function:
function drawcnum(ctx, xypairs) move_to(ctx, xypairs[1][1], xypairs[1][2]) for p in xypairs[2:end] line_to(ctx, p[1], p[2]) end stroke(ctx) end
This function,
drawcnum
, takes a Cairo context (ctx
) and an array of pairsxypairs
as arguments. It moves the context's cursor to the first pair of coordinates inxypairs
. Then, it connects all the subsequent pairs of coordinates with lines usingline_to
and finally draws the path usingstroke
. This function is used to draw individual numerals. -
Drawing Function:
@guarded draw(can) do widget ctx = getgc(can) hlen, wlen, len = height(can), width(can), length(numbers) halfwspan, thirdcolspan, voffset = wlen ÷ (len * 2), wlen ÷ (len * 3), hlen ÷ 8 set_source_rgb(ctx, 0, 0, 2550) # ... (code continued below) end
The
draw
function is defined with the@guarded
macro, which is used to handle exceptions within the function.Inside the function:
ctx
is set to the Cairo context of the canvas (can
).hlen
,wlen
, andlen
are calculated to get the height, width, and length of the numbers array, respectively.halfwspan
,thirdcolspan
, andvoffset
are calculated to determine the spacing and offsets for drawing.set_source_rgb(ctx, 0, 0, 2550)
sets the color for drawing to dark blue.
-
Moon Cipher Drawing:
- The code iterates through the
numbers
array:- For each number, it calculates the vertical position of the digit (x position) by dividing the canvas width by the number of elements times two.
- It calculates the x and y offsets for drawing the digit based on its value (1, 10, 100, or 1000), using a ternary conditional expression.
- Then, it draws the lines for the digit using the
drawcnum
function based on the number's value. - Finally, it draws the text representation of the number below the digit.
- The code iterates through the
-
Moon Cipher Display:
function mooncipher() draw(can) # ... (code continued below) end
The
mooncipher
function calls thedraw
function to draw the moon cipher on the canvas.- It also creates a
Condition
object (cond
) and defines a functionendit
that notifies the condition when the window is destroyed. - The
endit
function is connected to the:destroy
signal of the window (win
). - The canvas is shown using
show(can)
, and the condition is waited on usingwait(cond)
. This means the program will wait until the window is closed before proceeding.
- It also creates a
-
Main Execution:
mooncipher()
The
mooncipher
function is called to display the moon cipher representation of the given numbers on the canvas. When the user closes the window, the program exits.
In summary, this code creates a GUI window with a canvas and draws a moon cipher representation of the numbers in the numbers
array. The moon cipher is a visual representation of numbers using digits and lines, and the program displays it using Cairo drawing commands. The program waits for the user to close the window before exiting.
Source code in the julia programming language
using Gtk, Cairo
const can = GtkCanvas(800, 100)
const win = GtkWindow(can, "Canvas")
const numbers = [0, 1, 20, 300, 4000, 5555, 6789, 8123]
function drawcnum(ctx, xypairs)
move_to(ctx, xypairs[1][1], xypairs[1][2])
for p in xypairs[2:end]
line_to(ctx, p[1], p[2])
end
stroke(ctx)
end
@guarded draw(can) do widget
ctx = getgc(can)
hlen, wlen, len = height(can), width(can), length(numbers)
halfwspan, thirdcolspan, voffset = wlen ÷ (len * 2), wlen ÷ (len * 3), hlen ÷ 8
set_source_rgb(ctx, 0, 0, 2550)
for (i, n) in enumerate(numbers)
# paint vertical as width 2 rectangle
x = halfwspan * (2 * i - 1)
rectangle(ctx, x, voffset, 2, hlen - 2 * voffset)
stroke(ctx)
# determine quadrant and draw numeral lines there
dig = [(10^(i - 1), m) for (i, m) in enumerate(digits(n))]
for (d, m) in dig
y, dx, dy = (d == 1) ? (voffset, thirdcolspan, thirdcolspan) :
(d == 10) ? (voffset, -thirdcolspan, thirdcolspan) :
(d == 100) ? (hlen - voffset, thirdcolspan, -thirdcolspan) :
(hlen - voffset, -thirdcolspan, -thirdcolspan)
m == 1 && drawcnum(ctx, [[x, y], [x + dx, y]])
m == 2 && drawcnum(ctx, [[x, y + dy], [x + dx, y + dy]])
m == 3 && drawcnum(ctx, [[x, y], [x + dx, y + dy]])
m == 4 && drawcnum(ctx, [[x, y + dy], [x + dx, y]])
m == 5 && drawcnum(ctx, [[x, y + dy], [x + dx, y], [x, y]])
m == 6 && drawcnum(ctx, [[x + dx, y], [x + dx, y + dy]])
m == 7 && drawcnum(ctx, [[x, y], [x + dx, y], [x + dx, y + dy]])
m == 8 && drawcnum(ctx, [[x, y + dy], [x + dx, y + dy], [x + dx, y]])
m == 9 && drawcnum(ctx, [[x, y], [x + dx, y], [x + dx, y + dy], [x, y + dy]])
end
move_to(ctx, x - halfwspan ÷ 6, hlen - 4)
Cairo.show_text(ctx, string(n))
stroke(ctx)
end
end
function mooncipher()
draw(can)
cond = Condition()
endit(w) = notify(cond)
signal_connect(endit, win, :destroy)
show(can)
wait(cond)
end
mooncipher()
You may also check:How to resolve the algorithm Voronoi diagram step by step in the Python programming language
You may also check:How to resolve the algorithm Array length step by step in the Zoea Visual programming language
You may also check:How to resolve the algorithm File modification time step by step in the M2000 Interpreter programming language
You may also check:How to resolve the algorithm Zero to the zero power step by step in the Ada programming language
You may also check:How to resolve the algorithm Floyd-Warshall algorithm step by step in the Mathematica / Wolfram Language programming language