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.
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), (64,0)
(100,15) (64,31) and (0,31).
Flag of Bahrain
test text
Qatar
Consider the neighbouring state Qatar.
9 pillars, each pillar is 17 pixels below the previous.
The five coordinates of the bottom pillar are (0,0) (84,0) (125,9) (84,17), (0,17)
Use rgb(0.5,0,0) for the less jaunty red.
Flag of Qatar
The need for a loop becomes even more obvious for this flag.
test text
India
The flag of India is 306 by 204. The orange, white and green stripes
are 306 by 68. The wheel has radius 30 and the linewidth is 1.2.
The flag of India includes a wheel with 24 spokes, the spokes are
360/24 degrees 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
.
def drawFlag(g):
g.fill(path.rect(0,0,306,68), [color.cmyk.LimeGreen])
g.fill(path.rect(0,136,306,68), [color.cmyk.BurntOrange])
g.stroke(path.circle(153,102,30), [color.rgb.blue, style.linewidth(1.2)])
trancenter = trafo.translate(153,102)
g.stroke(path.line(0,0,0,30), [color.rgb.blue, style.linewidth(1.2), trafo.rotate(0*360/24), trancenter])
g.stroke(path.line(0,0,0,30), [color.rgb.blue, style.linewidth(1.2), trafo.rotate(1*360/24), trancenter])
g.stroke(path.line(0,0,0,30), [color.rgb.blue, style.linewidth(1.2), trafo.rotate(2*360/24), trancenter])
[Font ]
[Default ]
[Show ]
[Resize ]
[History ]
[Profile ]
test text
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,140).
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 trafo.rotate(i*30) 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
.
def drawFlag(g):
g.fill(path.rect(0,0,300,200), [color.rgb.white])
g.fill(path.path(path.moveto(0,200),
path.lineto(240,200),
path.lineto(0,40),
path.closepath()), [color.rgb.green])
g.fill(path.path(path.moveto(60,0),
path.lineto(300,160),
path.lineto(300,0),
path.closepath()), [color.rgb.red])
g.stroke(path.line(0,0,300,200), [color.rgb.blue, style.linewidth(42)])
burstc = trafo.translate(60,140);
tri = path.path(path.moveto(19,5),
path.lineto(29,0),
path.lineto(19,-5),
path.closepath())
g.fill(tri, [color.rgb.black, burstc])
g.fill(tri, [color.rgb.black, trafo.rotate(30), burstc])
[Font ]
[Default ]
[Show ]
[Resize ]
[History ]
[Profile ]
test text
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
The sin and cos functions in Java are called Math.sin and Math.cos. They
expect radians as input. 360o is
2*Math.PI in radians.
.
import math
def drawFlag(g):
star = path.path(path.moveto(0,100),
path.lineto(22,31),
path.lineto(95,31),
path.lineto(36,-12),
path.lineto(59,-81),
path.lineto(0,-38),
path.lineto(-59,-81),
path.lineto(-36,-12),
path.lineto(-95,31),
path.lineto(-22,31),
path.closepath())
g.fill(path.rect(0,0,500,350), [color.rgb.blue])
for i in range(9):
a = math.pi*2*i/9
c = trafo.translate(125*math.cos(a),125*math.sin(a))
g.fill(star, [color.rgb(1,1,0), c])
[Font ]
[Default ]
[Show ]
[Resize ]
[History ]
[Profile ]
test text