Perl:Flags with Repeats Tutorial
From Progzoo
We can repeat a statement or block of statements with a for loop. The simplest for loops take the following form:
The control variable i takes every value 0,1,2..9 in turn.
We can use the value of i inside the loop if required.
for (my $i=0;$i<10;$i++){
<i>statement</i>
}
Contents |
Bahrain
The flag of Bahrain has a five irregular pentagons on a red background.
Each pentagon can be represented by five points such as (0,0), (128,0) (200,31) (128,62) and (0,62).
Qatar
Neighbouring state Qatar has 9 pillars, each pillar is 34 units below the previous. Use RGB(128,0,0) for the less jaunty red.
The need for a loop becomes even more obvious for this flag.
India
The flag of India is 306 by 204. The orange, white and green stripes are 306 by 68. The wheel has radius 30.
The flag of India includes a wheel with 24 spokes, the spokes are 2*pi/24 radians apart.
The code given draws only three of the spokes - we could copy and paste - but it would be much better to use a loop.
We have not really done India justice here - the actual flag is much more elegant, the spokes taper and the rim includes decoration. Nevertheless - we apologize to the sub-continent and move on.
Namibia
Each the rightmost triangle has coordinates (19,5) (29,0) (19,-5) relative to the centre of the star burst - which is at (60,60).
Notice that only two of the 12 triangles are drawn by the example code. Also note that the colors are mixed up.




