Let us recap the steps we took in the previous post to produce a centred square:

  1. We choose the left coordinate of our square, e.g. left = 20.
  2. We know that the left bottom vertex of a centred square is on the main diagonal and hence bottom = left.
  3. By definition, the left and right sides are equally distant from the centre. We know that centre = 50 due to the dimensions of the canvas.
  4. We next compute the distance from the left to the centre, which is distance = centreleft.
  5. We add this distance to the left to obtain the right coordinate, i.e. we do right = centre + distance.
  6. We know the top right vertex of the square is also on the main diagonal and therefore top = right.
  7. We can now draw the square’s four sides with the following commands, in any order:
    • line left bottom right bottom
    • line right bottom right top
    • line right top left top
    • line left top left bottom

What we have just written down is an algorithm, a stepwise procedure to obtain our intended result. The most important thing is of course to put the steps in the right order, because each step can only use the information obtained in previous steps. For example, we can’t exchange the order of steps 4 and 5, because we need first the distance to then compute where the right side will be. However, even if the order of steps is arbitrary, putting them in a sensible order will make it easier to understand how the algorithm works. For example, although the four sides can be drawn in any order because we have all coordinates at hand, drawing the sides in clockwise or anti-clockwise order, i.e. each line starts where the previous ends, makes the steps easier to follow.

To translate the above algorithm into a DBN program, we need to know one more DBN command: set N E computes the value of expression E and assigns it to the name N so that whenever the name appears, the assigned value is used instead. Whenever a new value is assigned to the same name, the previously assigned value is lost. For example, executing first set left 20 and then set left 40 means that any subsequent use of the name left refers to the value 40, not 20. In programming jargon, left is a variable, a placeholder for a value, a box for storing a value. Whenever a new value is stored into a variable, the previously stored value is discarded.

In DBN, expressions that use mathematical operators like addition and subtraction have to be written within parentheses. For example, the computation in step 5 above is written in DBN as set right (centre + distance). We can now present the whole program, which obviously produces the same picture as in the previous post.

// the tunnel
// version 2, 9 Nov 2008, Michel Wermelinger
// line command uses computed coordinates

line 0 0 100 100 // left bottom corner to right top corner
line 0 100 100 0 // left top corner to right bottom corner

// near square
set left 20
set bottom left
set centre 50
set distance (centre - left)
set right (centre + distance)
set top right

line left bottom right bottom
line right bottom right top
line right top left top
line left top left bottom

// far square
set left 40
set bottom left
set centre 50
set distance (centre - left)
set right (centre + distance)
set top right

line left bottom right bottom
line right bottom right top
line right top left top
line left top left bottom

Although the program has become much longer, it is much clearer. It shows what is constant (the centre of the canvas), what we choose (the left coordinate, which changes from one square to the other), and what are the relationships between the various coordinates. The program reflects our reasoning, the algorithm we devised. This was achieved by abstracting the concrete values of line endpoints into general concepts like ‘left’ and ‘bottom’. We also used intermediate concepts like ‘centre’ and ‘distance’ to obtain our final results. All these concepts are captured by variables, which we have named after the concepts they represent. (As far as DBN is concerned, we could have named the variables with any string of letters, e.g. blop, but that would obfuscate the concepts to the program’s reader.)

Abstracting concrete values into general concepts about centred squares has enabled us to write down an algorithm that executes always the same steps after selecting the value for the left side. Wouldn’t it be nice to write those steps only once, instead of repeating them for each square we want, as we did in the program above? We will see how to do that in the next lesson.

Leave a Reply

You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Spam protection by WP Captcha-Free