Flags with Repeats Tutorial

From Progzoo
Jump to navigation Jump to 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.


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), (64,0) (100,15) (64,31) and (0,31).

Flag of Bahrain

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


Consider the neighbouring state Qatar.

  • 9 pillars, each pillar is 17 pixels below the previous.
  • The five coordinates of the top pillar are (0,0) (84,0) (125,9) (84,17), (0,17)
  • 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]


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.

  • The yellow circle has radius 16
  • The triangles are partially covered by a blue circle of radius 20
  • The whole flag is 300 by 200
  • You must set the white part of the flag white
Flag of Namibia

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

European Union

Flag of European Union

We need the trigonometry functions cos and sin to draw flags in a circle.

  • There are 12 stars
  • The flag is 250 by 180
  • The stars centers are 64 from the center
  • The coordinates at 30o from the x-axis (East) are 64*cos(30), 64*sin(30).
  • Each star has radius 12.5

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