11.3. Graphics and Bit Operations Problems

The following problems test your knowledge of basic graphics and bit operations.

11.3.1. Eighth of a Circle


Write a function that draws the upper eighth of a circle centered at (0, 0) with a given radius, where the upper eighth is defined as the portion starting at 12 and going to 1:30 on a clock face. Use the following prototype:

void drawEighthOfCircle( int radius );

The coordinate system and an example of what you are to draw are shown in Figure 11-1. You will use a function with the following prototype to draw pixels:

void setPixel( int xCoord, int yCoord );
Figure 11.1. Figure 11-1

This problem is not as contrived as it seems. If you were trying to implement a full-circle drawing routine, you would want to do as little calculation as possible to maintain optimum performance. Given the pixels for one-eighth of a circle, you can easily determine the pixels for the remainder of the circle from symmetry.

If a point (x, y) is on a circle, so are the points (–x, y), (x, –y), (–x, –y), (y, x), (–y, x), (y, –x), and (–y, –x).

This problem is an example of a scan conversion, converting a geometric drawing to a pixel-based raster image. You will need an equation for a circle before you can calculate anything. The common mathematical function that produces a circle is as follows:

x2 + y2 = r2

The definition is nice because it contains ...

Get Programming Interviews Exposed: Secrets to Landing Your Next Job, Second Edition now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.