1.12 More exercises
2.1.2 The perimeter of a polygon – for loops
In engineering we often have to calculate certain properties of polygons, or of figures that can be approximated by polygons. Such a property is the perimeter. The knowledge of the perimeter can be necessary, for example, for calculating the length of a weld that connects a polygonal element to another element. Other examples are the calculation of the length of a fence or the calculation of the area of the developed lateral surface of a prismatic body.
Returning to the triangle shown in Figure 2.3, we can calculate its perimeter by simply adding the distances between the three vertices, that is the lengths of the three sides
norm(P2 - P1) + norm(P3 - P2) + norm(P3 - P1) ans =
9.5215
While the above procedure may be acceptable for one case, with few points, it isn’t convenient for repeated use, and certainly not for larger numbers of points. To better use the possibilities of a programming language like MAT- LAB, let us develop a function that performs the operation for any number of points. The solution is simple, but we are going to show how to proceed in a systematic manner that can be used later for more complex problems.
We begin by defining the input and the output. The input, that is the argument of the function, will be the description of the polygonal line. As shown above, we have chosen for this description a 2-by-n array, where n is the number of points, in this case the number of vertices. If the object is a polygon, that is a closed polygonal line, the line must be closed by repeating in the array, as the last column, the column vector of the first point (vertex). The output should be the sum of the distances between the points represented in the input array. This is one number.
Next, we choose a strategy for calculating the sum of distances. We shall use for this, as in the preceding subsection, the MATLAB function norm. If there are n points, then the function has to call n−1 times the function norm. For a polygon with m vertices the function must call the function norm m times. In other words, the function we are interested in is based on repetitive calls of norm. We say that each call is an iteration. Like other programming languages, MATLAB provides a repetitive construct that performs a required
number of iterations. This construct is called a for loop and its basic form is for k = k0: kn
statements end
For the moment we assume k0 ≤ kn, later we are going to see that this restriction is not necessary. The statements included between the line begin- ning with for and the line end are executed for k = k0, k = k0+ 1, . . . kn, that is kn− k0+ 1 times. The statements can include functions of k.
Below is code that calculates the length of the polygonal line using a for loop. Write this code in a file with the name perimeter and the extension m, and note how the MATLAB editor helps in formatting the text.
function s = perimeter(Polyline)
%PERIMETER Calculates the length of the polygonal line Polyline. % For n points (vertices) Polyline is a 2-by-n array. % The first line represents the x-coordinates of % the n points, the second line, their y-coordinates. % If Polyline is a closed polygon, the last point is % the repetition of the first point.
s = 0; % initializes to zero the perimeter length [ m, n ] = size(Polyline);
for k = 2: n
ds = Polyline(:, k) - Polyline(:, k - 1); s = s + norm(ds);
end
The first line declares the contents of the file as a function with input argument Polyline and output argument s. The next six lines, beginning with ‘%’, are comments that explain the purpose of the function and how to build its input argument. The first word in the comments is the name of the function, identical to the name of the file. After writing the function, try the command help perimeter and see how the function echoes the six comment lines. Comment lines are not executable.
The function perimeter creates a variable called s and initializes its value to zero. At each iteration of the loop the function will increase the value of the variable s by the length of one side of the polygonal line. The MATLAB built-in function size is used to determine the number of iterations to be executed. The output argument n, of size, is the number of vertices of the polygonal line. The number of segments (sides in the case of a polygon) is n− 1, and this is also the number of required iterations. Therefore, the first line of the loop contains the expression k = 2: n. The body of the loop is contained between the line beginning with for and the line end. As a matter
of good style, the body of the loop is indented. In this way it is easier to identify the loop and separate it from the rest of the code. As you may have noted while writing the code, the MATLAB editor produces automatically the indentation.
To understand how the loop works, let us simulate its behavior when the input is the description of the triangle shown in Figure 2.3. The function is called with the input argument [ P1 P2 P3 P1 ]. The function reads the input and finds that its dimensions are m = 2 and n = 4. The execution begins with s = 0 and k = 2. The function calculates
ds = Polyline(:, 2) - Polyline(:, 1);
afterward the function calculates the norm of ds, and adds the result to s. At this moment the value of s equals the length of the side P1P2. The value of k is incremented to 3. The second iteration of the loop calculates
ds = Polyline(:, 3) - Polyline(:, 2);
then, the norm of the new ds and adds it to the variable s making it equal to the length of P1P2 plus the length of P2P3. The value of k is incremented to 4. The third iteration calculates
ds = Polyline(:, 4) - Polyline(:, 3);
then, the norm of the new ds and adds it to the variable s making it equal to the length of P1P2, plus the length of P2P3, plus the length of P3P1. At this moment k = n and the execution of the loop stops. Note that at the end of each iteration, s correctly represents the sum of the distances between the points processed up to this point. In computer jargon s is called a loop invariant.
Now we know that the function works correctly for a polygon with three vertices, that is a triangle. We may see no reason why the function could not work correctly for a polygon with any number of vertices, n. However, one may ask for a more convincing, general proof. Such a proof can be obtained by induction. To carry on such a proof we must first check that the function works correctly for n = 1, that is one vertex. Next, assuming that it works for some m, where 1 < m < n, we must show that it also works for m + 1.
When n = 1 the polygonal line has only one vertex. The iterations must start with k = 2, which is greater than 1. No iteration is executed and s remains as initialized, that is equal to zero. This is also the value of the output and it is correct. The distance between a point and itself is, indeed, null.
We assume now that the function worked correctly until k = m and s is equal to the sum of distances between the first m−1 points. The next iteration increments k to m + 1 and the function adds to s the distance between the points Pmand Pm+1. Thus, the value of s equals the length of the line up to the (m + 1)th point and this is what we had to prove.
We developed the function perimeter starting from the function norm. Next, we decided to use it in a loop. This way of designing software, starting from a building block and ending up with the whole function, or program, is called bottom-up. Later we shall learn also how to design top-down.