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.
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).
<img src="/img/flagbahrain.png" alt="Flag of Bahrain: Five white points on a red background."/>
Neighbouring state Qatar has 9 pillars, each pillar is 34 units below the previous. Use RGB(128,0,0) for the less jaunty red.
<img src="/img/flagqatar.png" alt="Flag of the Qatar."/>
The need for a loop becomes even more obvious for this flag.
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.
<img src="/img/flagnamibia.png" alt="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.
We can use the method g.rotate(Math.PI/6) to repeat the triangles of the star-burst for Namibia.
<img src="/img/flageurope.png" alt="Flag of Europe 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).