Graphical display

The elements of the graphical display are: the reference point and the
graphical context, the colors, the drawings, the filling pattern of
closed forms, the texts and the bitmaps.

Reference point and graphical context

The Graphics library manages a unique main window. The
coordinates of the reference point of the window range from point (0,0)
at the bottom left to the upper right corner of the window. The main
functions on this window are:

open_graph, of type string -> unit, which opens a window;

close_graph, of type unit -> unit, which closes it;

clear_graph, of type unit -> unit, which clears it.

The dimensions of the graphical window are given by the functions
size_x and size_y.

The string argument of the function open_graph depends on the
window system of the machine on which the program is executed and is
therefore not platform independent. The empty string, however, opens a
window with default settings. It is possible to specify the size of the
window: under X-Windows, " 200x300" yields a window which is
200 pixels wide and 300 pixels high. Beware, the space at the beginning
of the string " 200x300" is required!

The graphical context contains a certain number of readable and/or
modifiable parameters:

the current point:

current_point : unit -> int * int

moveto : int -> int -> unit

the current color:

set_color : color -> unit

the width of lines:

set_line_width : int -> unit

the current character font:

set_font : string -> unit

the size of characters:

set_text_size : int -> unit

Colors

Colors are represented by three bytes: each stands for the intensity
value of a main color in the RGB-model (red, green, blue), ranging from
a minimum of 0 to a maximum of 255. The function rgb (of
type int -> int -> int -> color) allows the generation of a new
color from these three components. If the three components are identical,
the resulting color is a gray which is more or less intense depending on
the intensity value. Black corresponds to the minimum intensity of each
component (000) and white is the maximum (255255255).
Certain colors are predefined: black, white,
red, green, blue, yellow,
cyan and magenta.

The variables foreground and background correspond
to the color of the fore- and the background respectively. Clearing the
screen is equivalent to filling the screen with the background color.

A color (a value of type color) is in fact an integer which
can be manipulated to, for example, decompose the color into its three
components (from_rgb) or to apply a function to it that inverts
it (inv_color).

The function point_color, of type int -> int -> color,
returns the color of a point when given its coordinates.

Drawing and filling

A drawing function draws a line on the screen. The line is of the current
width and color. A filling function fills a closed form with the current
color. The various line- and filling functions are presented in figure
5.1.

drawing

filling

type

plot

int -> int -> unit

lineto

int -> int -> unit

fill_rect

int -> int -> int -> int -> unit

fill_poly

( int * int) array -> unit

draw_arc

fill_arc

int -> int -> int -> int -> int -> unit

draw_ellipse

fill_ellipse

int -> int -> int -> int -> unit

draw_circle

fill_circle

int -> int -> int -> unit

Figure 5.1: Drawing- and filling functions.

Beware, the function lineto changes the position of the current
point to make drawing of vertices more convenient.

Drawing polygons

To give an example, we add drawing primitives which are not predefined. A
polygon is described by a table of its vertices.

Please note that these functions take the same arguments as the predefined
ones for filling forms. Like the other functions for drawing forms,
they do not change the current point.

Illustrations in the painter's model

This example generates an illustration of a token ring network (figure
5.2).
Each machine is represented by a small circle. We place the set
of machines on a big circle and draw a line between the connected
machines. The current position of the token in the network is indicated
by a small black disk.

The function net_points generates the coordinates of the
machines in the network. The resulting data is stored in a table.

The following function call corresponds to the left drawing in figure
5.2.

# draw_net(140,20)60.010103;;- : unit = ()

# save_screen"IMAGES/tokenring.caa";;- : unit = ()

Figure 5.2: Tokenring network.

We note that the order of drawing objects is important. We first plot the
connections then the nodes. The drawing of network nodes erases some part
of the connecting lines. Therefore, there is no need to calculate the
point of intersection between the connection segments and the circles
of the vertices. The right illustration of figure 5.2
inverts the order in which the objects are displayed. We see that the
segments appear inside of the circles representing the nodes.

Text

The functions for displaying texts are rather simple. The two
functions draw_char (of type char -> unit) and
draw_string (of type string -> unit) display a
character and a character string respectively at the current point.
After displaying, the latter is modified. These functions do not change
the current font and its current size.

Note

The displaying of strings may differ depending on the graphical interface.

The function text_size takes a string as input and returns a
pair of integers that correspond to the dimensions of this string when
it is displayed in the current font and size.

Displaying strings vertically

This example describes the function draw_string_v, which
displays a character string vertically at the current point. It is
used in figure 5.3. Each letter is displayed separately
by changing the vertical coordinate.

If we wish to realize vertical displaying of text, it is necessary
to account for the fact that the current point is modified by the
function draw_string_v. To do this, we define the function
draw_text_v, which accepts the spacing between columns and
a list of words as parameters.

If we need further text transformations like, for example, rotation,
we will have to take the bitmap of each letter and perform the
rotation on this set of pixels.

Bitmaps

A bitmap may be represented by either a color matrix (color
array array) or a value of abstract type 1image,
which is declared in library Graphics. The names and types of the
functions for manipulating bitmaps are given in figure 5.4.

function

type

make_image

color array array -> image

dump_image

image -> color array array

draw_image

image -> int -> int -> unit

get_image

int -> int -> int -> int -> image

blit_image

image -> int -> int -> unit

create_image

int -> int -> image

Figure 5.4: Functions for manipulating bitmaps.

The functions make_image and dump_image are
conversion functions between types image and color array
array. The function draw_image displays a bitmap starting at
the coordinates of its bottom left corner.

The other way round, one can capture a rectangular part of the screen to
create an image using the function get_image and by indicating
the bottom left corner and the upper right one of the area to be captured.
The function blit_image modifies its first parameter (of
type image) and captures the region of the screen where the
lower left corner is given by the point passed as parameter. The size
of the captured region is the one of the image argument. The function
create_image allows initializing images by specifying their
size to use them with blit_image.

The predefined color transp can be used to create transparent
points in an image. This makes it possible to display an image within a
rectangular area only; the transparent points do not modify the initial
screen.

Polarization of Jussieu

This example inverts the color of points of a bitmap. To do this, we use
the function for color inversion presented on page ??,
applying it to each pixel of a bitmap.

Given the bitmap jussieu, which is displayed in the left half
of figure 5.5, we use the function inv_image
and obtain a new ``solarized'' bitmap, which is displayed in the right
half of the same figure.

Example: drawing of boxes with relief patterns

In this example we will define a few utility functions for drawing boxes
that carry relief patterns. A box is a generic object that is useful in
many cases. It is inscribed in a rectangle which is characterized by a
point of origin, a height and a width.

To give an impression of a box with a relief pattern, it is sufficient
to surround it with two trapezoids in a light color and two others in
a somewhat darker shade.

Inverting the colors, one can give the impression that the boxes are on
top or at the bottom.

Implementation

We add the border width, the display mode (top, bottom, flat) and the
colors of its edges and of its bottom.
This information is collected in a record.