Programming and Graphics

(Kiana) #1

206 Introduction to C++ Programming and Graphics


//--- Define a viewport in the top right corner and draw a circle:

viewport(0.0, 1.0, 0.0, 1.0);
move2(-0.9, -0.5);
color(BLUE);
drawstr("circle");

//--- draw a circle of radius 0.4 centered at the point (0.0, 0.0)

circle(0.0, 0.0, 0.4);

//--- Define a viewport in the bottom left corner and draw an ellipse:

viewport(-1.0, 0.0, -1.0, 0.0);
move2(-0.9, -0.5);
color(GREEN);
drawstr("ellipse");

/* To draw an ellipse, we change the aspect ratio so it is no longer
equal to one and call circle. In this case we use ortho2 to make
the square viewport appear rectangular.

The call to pushmatrix saves the current viewing transformation.
After the ortho2 is done, we restore the current viewing
transformation with a call to popmatrix. Otherwise everything
after the call to ortho would come out looking squashed as the
world aspect ratio is no longer 1. */

pushmatrix();
ortho2(-1.0, 1.0, -1.0, 2.0);
circle(0.0, 0.5, 0.4);
popmatrix();

//--- Define a viewport in the bottom right corner and draw an arc:

color(RED);
viewport(0.0, 1.0, -1.0, 0.0);
move2(-0.9, -0.5);
drawstr("arc");

/* Draw an arc centered at (0.0, 0.0), radius of 0.4. The start
angle is 0.0 and the end angle is 90 degrees. */

arc(0.0, 0.0, 0.4, 0.0, 90.0);

//--- Done:

getkey();
Free download pdf