3.3 STRUCTURED PROGRAMMING

 

 

INTRODUCTION

 

The concept of variables is very important to programming. Therefore, you need to give a special effort in order to understand it as thoroughly as possible. You already know that a variable is a memory slot that is addressed with a name and that holds a value. You also know that parameters can be understood as “volatile” memory slots which, when their function is called, receive a value that the function can then access during its execution.

PROGRAMMING CONCEPTS: Constants, procedural programming, reusability

 

 

A MOSAIC OF 10X10 STONES

 

You have the task of creating a beautiful colored mosaic out of square stones. You get different colored mosaic stones with the side length 10, and you should put them together exactly side by side on a canvas with the size 400x400. You feel a bit lazy, so you are going to leave the task up to the computer. You tell it to place the stones with random colors line by line. Use delay(1) to create a short pause after each stone is laid so that you can watch the computer making the mosaic.

 
from gpanel import *
makeGPanel(0, 400, 0, 400)

for y in range(0, 400, 10):
    for x in range(0, 400, 10):
        setColor(getRandomX11Color())
        move(x + 5, y + 5)
        fillRectangle(10, 10)
        delay(1)        
Highlight program code (Ctrl+C copy, Ctrl+V paste)

 

 

MEMO

 

Whenever a grid needs to be run through, two nested for loops are best suited. Think about why precisely the stones are placed line by line from bottom to top. You need a shift at move() because the stones are anchored/established in the middle.

The method getRandomX11Color() gives back (as a word) one of the colors from the X11 color palette, which you can then pass on to setColor(). You can first call the function getRandomX11Color(), and then the function setColor().

 

 

MAGIC NUMBERS

 

Two weeks later, you receive a delivery of stones that are five times larger with a side length of 50. The computer should lay them on the same canvas again. What do you need to do to adjust the program? You check out the previous code, but of course you no longer understand what each particular line means.

You think: anywhere there is a 10, the number should be changed to 50. So you do this, but soon realize you were wrong. Unfortunately the mosaic no longer covers the entire canvas.
 

Now you have to go back through the code line by line in order to find the error. If this is necessary to adapt the program to a new situation (reuse), then your program was correct but just poorly written. You should get into the habit of having a good programming style, so that you can easily adjust programs to (fit) new situations.

How can you proceed? Instead of writing the stone size as a fixed number in the code, define a variable size, that can be used in place of the fixed number wherever the stone size plays a role. In order to structure the program even better, you should also write a separate function drawStone() for the placement of the stone.

from gpanel import *

def drawStone(x, y):
    setColor(getRandomX11Color())
    move(x + size/2, y + size/2)
    fillRectangle(size, size)

makeGPanel(0, 400, 0, 400)

size = 50

for x in range(0, 400, size):
    for y in range(0, 400, size):
        drawStone(x, y)      
Highlight program code (Ctrl+C copy, Ctrl+V paste)

 

 

MEMO

 

The use of fixed numerical values throughout the program results in poorly reusable programs. You should instead define variables and use them in place of numbers. To indicate that these should never change, sometimes you write these variables in capital letters and call them constants.

A variable that is defined in the main block can also be read in each function. Therefore, we also call it a global variable.

For longer self-contained actions, the code should be put into separate functions. This has several advantages: the first is that you can recognize the function name and what it should do, the second is that you can call them several times without having to rewrite the code, and the third is that the program becomes clear and comprehensible. This type of programming is called structured programming (or procedural programming) and it is an important trait of a good programming style.

 

 

EXERCISES

 
1.

As you can find out using the Pythagorean theorem, the command:

fillTriangle(x - math.sqrt(3)/2 * r, y - r/2,
                 x + math.sqrt(3)/2 * r, y - r/2,
                 x, y + r);

draws an equilateral triangle with the center point at (x, y) and the radius r. Verify this in a GPanel with the coordinate system -1..1 for both axes, which draws a triangle with its center at the origin.


2.


Use the code from exercise 1 and define a function star(x, y, r), which draws a star using two equilateral triangles with the center point (x, y) and the size r. Try it out and draw a few stars.


3.

Enhance the function star() with a parameter that sets the color of the star. Draw a star mosaic on a grey background with 50x50 stars. Keep the size of the stars consistent.
Also, make sure that none of the stars are drawn with the background color.