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), (128,0)
(200,31) (128,62) and (0,62).
test text
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.
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.
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.
.
static void drawFlag(Image& g){
g.fillColor("orange");
g.draw(DrawableRectangle(0,0,306,68));
g.fillColor("white");
g.draw(DrawableRectangle(0,68,306,136));
g.fillColor("green");
g.draw(DrawableRectangle(0,136,306,204));
g.fillColor("white");
g.strokeColor("blue");
g.strokeWidth(2);
std::list<Magick::Drawable> dl;
dl.push_back(DrawablePushGraphicContext());
dl.push_back(DrawableCircle(150,100,150,70));
dl.push_back(DrawableTranslation(150,100));
dl.push_back(DrawableLine(0,0,30,0));
dl.push_back(DrawableRotation(15));
dl.push_back(DrawableLine(0,0,30,0));
dl.push_back(DrawableRotation(15));
dl.push_back(DrawableLine(0,0,30,0));
dl.push_back(DrawableRotation(15));
dl.push_back(DrawablePopGraphicContext());
g.draw(dl);
}
[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,60).
Notice that only two of the 12 triangles are drawn by the example
code. Also note that the colors are mixed up.
.
static void drawFlag(Image& g){
std::list<Coordinate> top;
top.push_back(Coordinate(0,0));
top.push_back(Coordinate(240,0));
top.push_back(Coordinate(0,160));
std::list<Coordinate> btm;
btm.push_back(Coordinate(60,200));
btm.push_back(Coordinate(300,40));
btm.push_back(Coordinate(300,200));
std::list<Coordinate> ray;
ray.push_back(Coordinate(19,5));
ray.push_back(Coordinate(29,0));
ray.push_back(Coordinate(19,-5));
// Build up a list of Drawable
std::list<Magick::Drawable> dl;
dl.push_back(DrawablePushGraphicContext());
dl.push_back(DrawableFillColor("green"));
dl.push_back(DrawablePolygon(top));
dl.push_back(DrawableFillColor("red"));
dl.push_back(DrawablePolygon(btm));
dl.push_back(DrawableStrokeColor("blue"));
dl.push_back(DrawableStrokeWidth(42));
dl.push_back(DrawableLine(0,200,300,0));
dl.push_back(DrawableStrokeColor("black"));
dl.push_back(DrawableStrokeWidth(0));
dl.push_back(DrawableFillColor("black"));
dl.push_back(DrawableTranslation(60,60));
dl.push_back(DrawablePolygon(ray));
dl.push_back(DrawableRotation(30));
dl.push_back(DrawablePolygon(ray));
dl.push_back(DrawablePopGraphicContext());
g.draw(dl);
}
[Font ]
[Default ]
[Show ]
[Resize ]
[History ]
[Profile ]
test text
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).
.
static void drawFlag(Image& g){
// Define the polygon star
std::list<Coordinate> star;
star.push_back(Coordinate(0,-25));
star.push_back(Coordinate(6,-8));
star.push_back(Coordinate(24,-8));
star.push_back(Coordinate(9,3));
star.push_back(Coordinate(15,20));
star.push_back(Coordinate(0,9));
star.push_back(Coordinate(-15,20));
star.push_back(Coordinate(-9,3));
star.push_back(Coordinate(-24,-8));
star.push_back(Coordinate(-6,-8));
// Build up a list of Drawable
std::list<Magick::Drawable> dl;
dl.push_back(DrawablePushGraphicContext());
dl.push_back(DrawableFillColor("blue"));
dl.push_back(DrawableRectangle(0,0,500,350));
dl.push_back(DrawableFillColor("yellow"));
for (int i=0;i<9;i++){
double th = PI*2*i/9;
dl.push_back(DrawableTranslation(125*cos(th),
125*sin(th)));
dl.push_back(DrawablePolygon(star));
dl.push_back(DrawableTranslation(-125*cos(th),
-125*sin(th)));
}
dl.push_back(DrawablePopGraphicContext());
g.draw(dl);
}
[Font ]
[Default ]
[Show ]
[Resize ]
[History ]
[Profile ]
test text