Python vs Modula-2

(8) Let's Traverse over Desktop Icons

To Workbench Python (EN)    

<Task>

Desk top icons are those you see on the PC screen. If they are scattered on the screen, just click the "Sort by" menu and they will be arranged in the form of rectangle or one with a line of icons attached on the right side. The arrangements in the right is a rectangle of H=6 by W=7 with a line of A=4.

Suppose that you move a mouse so that the mouse poiner hovers over the icons through the corners but not through the sides. At the sides of the rectangle where no icon exists beyond the corner, the mouse pointer is reflected. At the corners, whether they are protruding or receding, the movement is terminated. The figure at right shows an example of the trajectory. In a sense the mouse pointer were moved as if it were carried by light.

Now the task is to make a computer program that will predict the trajectory for any parameter set of (H, W, A). In practice the parameter values may be at most 30. The trajectory length may be finite or infinite depending on the start position.

<Traversing over desktop icons >

Each icon is referred to as ID, which ranges from 0 (upper-left corner) to Length-1 (lower-right corner).

Structure of the Program
<Python>

Run in the VS Code/Anaconda environment. The (main) program is named DeskTopIcons.py. The following modules are also made.

  1. IconArrangements.py: determines the location of each icon and those of neighboring four icons, given ID. The four means ƒÎ/4 rad, 3ƒÎ/4 rad, 5ƒÎ/4 rad and 7ƒÎ/4 rad with respect to the horizontal axis. Method Trajectory of Class traverseIcon produces the trajectory for a given set of the position and the direction.

  2. IconSequence.py: Infinite sequences are separated to initial finite sequence and the repetition of finite sequences that follow.

  3. IconGraphics.py: Graphics representation of the movement based on pygame.py and pygwidgets.py

The code development was relatively alleviated because Modula-2 code was completed first, but two-dimensional variables of Modula-2 were found difficult to inherit.

<XDS Modula-2>

Run in the XDS environment with TopSpeed extension and Hgraph graphics package.

  1. ScreenIcons.mod: Main module having a menu structure in the text window.
  2. SubScreenIcons.def, .mod: Defines the following type
      cellType = RECORD
             x,y: INTEGER;                       (* position of icon     *)		
        neighbor: ARRAY [0..3] OF INTEGER;       (* surrounding ID       *)
            dpi4: ARRAY [0..3] OF BOOLEAN;       (* movable to neighbor  *)
           dircn: INTEGER;                       (* direction to neighbor*)
                 END;
    
    Each icon is given its own cellType property. The following procedure that yields a trajectory is included.
     PROCEDURE followIcons(VAR pathID: ARRAY OF INTEGER; (* sequence in terms od ID*)
                            VAR sizeN: INTEGER;          (* the number of icons    *) 
                           VAR isFull: BOOLEAN;          (* not of finite length?  *)
                                initN: INTEGER;          (* initial ID             *)
                            initdircn: INTEGER);         (* initial direction      *)
    
  3. screenIconSequence.def, .mod: Analyzes trajectories.
  4. Hgr*.def, mod: Plotter-type graphics modules
Decision-Making Steps
<Reflection and Termination>

When traversing in a straight line, next icon is always found with the value of dircn (all True) kept fixed. However, on the boundaries the property does not hold true, and the next appropriate value of dircn has to be sought for using

PROCEDURE checkNeighbor(VAR next: INTEGER; prevN: INTEGER;  prevdircn: INTEGER):
									BOOLEAN;
This procedure effectively takes into account the table of dpi4 shown at right.

As a matter of fact another set of the dpi4 values for 0 rad, ƒÎ/2 rad, ƒÎ rad and 3ƒÎ/2 rad are necessary for the automatic determination of reflecton direction.

<Values of 'dircn' at various positions>

<Sequences of Infinite Lengths>

If the number of icons tends to exceed the predetermined memory capacity, the sequence should be understood as continuing indefinitely. Such a sequence is illustrated at right. It consisits of a leading sequence of length Lini (=5 in the figure)and a repetition of the finite sequence of length Lrep (=6), which may be called period.

<An infinite sequence composed of two types of finite sequences>

Infinite Sequence for H=12, W=6 and A=3
< Python > < XDS Modula-2 >

11-12-2023, S. Hayashi