Perl:Flags with Repeats Tutorial

From Progzoo
Revision as of 22:38, 6 September 2008 by (talk) (Namibia)
Jump to: navigation, search

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++){


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).

Flag of Bahrain
[Font] [Default] [Show] [Resize] [History] [Profile]


Neighbouring state Qatar has 9 pillars, each pillar is 34 units below the previous. Use RGB(128,0,0) for the less jaunty red.

Flag of Qatar

The need for a loop becomes even more obvious for this flag.

[Font] [Default] [Show] [Resize] [History] [Profile]


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.

Flag of India
[Font] [Default] [Show] [Resize] [History] [Profile]


Flag of 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.

[Font] [Default] [Show] [Resize] [History] [Profile]

European Union


We need the trigonometry functions cos and sin to draw flags in a circle. The coordinates of a point on a circle radius 125 at 30° from the x-axis (East) these are 125*cos(30), 125*sin(30).

[Font] [Default] [Show] [Resize] [History] [Profile]