Previous Contents Next

Exercises

Polar coordinates

Coordinates as used in the library Graphics are Cartesian. There a line segment is represented by its starting point (x0,y0) and its end point (x1,y1). It can be useful to use polar coordinates instead. Here a line segment is described by its point of origin (x0,y0), a length (radius) (r) and an angle (a). The relation between Cartesian and Polar coordinates is defined by the following equations:
ì
í
î
x1 = x0 + r * cos(a)
y1 = y0 + r * sin(a)
The following type defines the polar coordinates of a line segment:

# type seg_pol = {x:float; y:float; r:float; a:float};;
type seg_pol = { x: float; y: float; r: float; a: float }


  1. Write the function to_cart that converts polar coordinates to Cartesian ones.

  2. Write the function draw_seg which displays a line segment defined by polar coordinates in the reference point of Graphics.

  3. One of the motivations behind polar coordinates is to be able to easily apply transformations to line segments. A translation only modifies the point of origin, a rotation only affects the angle field and modifying the scale only changes the length field. Generally, one can represent a transformation as a triple of floats: the first represents the translation (we do not consider the case of translating the second point of the line segment here), the second the rotation and the third the scaling factor. Define the function app_trans which takes a line segment in polar coordinates and a triple of transformations and returns the new segment.

  4. One can construct recursive drawings by iterating transformations. Write the function draw_r which takes as arguments a line segment s, a number of iterations n, a list of transformations and displays all the segments resulting from the transformations on s iterated up to n.

  5. Verify that the following program does produce the images in figure 5.10.

    let pi = 3.1415927 ;;
    let s = {x=100.; y= 0.; a= pi /. 2.; r = 100.} ;;
    draw_r s 6 [ (-.pi/.2.),0.6,1.; (pi/.2.), 0.6,1.0] ;;
    Graphics.clear_graph();;
    draw_r s 6 [(-.pi /. 6.), 0.6, 0.766;
    (-.pi /. 4.), 0.55, 0.333;
    (pi /. 3.), 0.4, 0.5 ] ;;


Figure 5.10: Recursive drawings.


Bitmap editor

We will attempt to write a small bitmap editor (similar to the command bitmap in X-window). For this we represent a bitmap by its dimensions (width and height), the pixel size and a two-dimensional table of booleans.

  1. Define a type bitmap_state describing the information necessary for containing the values of the pixels, the size of the bitmap and the colors of displayed and erased points.

  2. Write a function for creating bitmaps (create_bitmap) and for displaying bitmaps (draw_bitmap) .

  3. Write the functions read_bitmap and write_bitmap which respectively read and write in a file passed as parameter following the ASCII format of X-window. If the file does not exist, the function for reading creates a new bitmap using the function create_bitmap. A displayed pixel is represented by the character #, the absence of a pixel by the character -. Each line of characters represents a line of the bitmap. One can test the program using the functions atobm and bmtoa of X-window, which convert between this ASCII format and the format of bitmaps created by the command bitmap. Here is an example.

    
     ###################-------------#######---------######
     ###################---------------###-------------##--
     ###-----###-----###---------------###-------------#---
     ##------###------##----------------###-----------##---
     #-------###-------#-----------------###---------##----
     #-------###-------#-----------------###--------##-----
     --------###--------------------------###-------#------
     --------###-------###############-----###----##-------
     --------###-------###---------###------###--##--------
     --------###-------###----------##-------###-#---------
     --------###-------###-----------#-------#####---------
     --------###-------###-----------#--------###----------
     --------###-------###--------------------####---------
     --------###-------###--------------------####---------
     --------###-------###------#-----------##---###-------
     --------###-------###------#----------##----###-------
     --------###-------##########----------#------###------
     --------###-------##########---------##-------###-----
     --------###-------###------#--------##--------###-----
     --------###-------###------#-------##----------###----
     --------###-------###--------------#------------###---
     ------#######-----###-----------#######--------#######
     ------------------###---------------------------------
     ------------------###-----------#---------------------
     ------------------###-----------#---------------------
     ------------------###----------##---------------------
     ------------------###---------###---------------------
     ------------------###############---------------------
    
  4. We reuse the skeleton for interactive loops on page ?? to construct the graphical interface of the editor. The human-computer interface is very simple. The bitmap is permanently displayed in the graphical window. A mouse click in one of the slots of the bitmap inverts its color. This change is reflected on the screen. Pressing the key 'S' saves the bitmap in a file. The key 'Q' terminates the program.
  5. Write a function go which takes the name of a file as parameter, loads the bitmap, displays it and starts the interactive loop.

Earth worm

The earth worm is a small, longish organism of a certain size which grows over time while eating objects in a world. The earth worm moves constantly in one direction. The only actions allowing a player to control it are changes in direction. The earth worm vanishes if it touches a border of the world or if it passes over a part of its body. It is most often represented by a vector of coordinates with two principal indices: its head and its tail. A move will therefore be computed from the new coordinates of its head, will display it and erase the tail. A growth step only modifies its head without affecting the tail of the earth worm.
  1. Write the Objective CAML type or types for representing an earth worm and the world where it evolves. One can represent an earth worm by a queue of its coordinates.

  2. Write a function for initialization and displaying an earth worm in a world.

  3. Modify the function skel of the skeleton of the program which causes an action at each execution of the interactive loop, parameterized by a function. The treatment of keyboard events must not block.

  4. Write a function run which advances the earth worm in the game. This function raises the exception Victory (if the worm reaches a certain size) and Loss if it hits a full slot or a border of the world.

  5. Write a function for keyboard interaction which modifies the direction of the earth worm.

  6. Write the other utility functions for handling interaction and pass them to the new skeleton of the program.

  7. Write the initiating function which starts the application.

Previous Contents Next