You are on page 1of 5

University of Babylon College of Computer Technology Department of Information Network Day: Wednesday Date: 71-10-2012 Stage: Second.

Topic: Introduction to Computer Graphics Lecture3: Circle Generating Algorithms . General Objective: The students will be able to use circle generating algorithms when they need to build other algorithms. Procedural Objective: 1- The students can use more than one algorithm to draw any circle. 2- The students will be able to alter the given circle generating algorithm to draw other shapes. 3- The students can recognize the way that they could follow to build an efficient circle generating algorithm. Teaching aids: Lecture (presented by MS-power point program on the laptop), discussion between lecturer and students, LCD monitor, whiteboard, pens.

CIRCLE-GENERATING ALGORITHM
3.1 Introduction Since the circle is a frequently used component in pictures and graphs, a procedure for generating either full circles or circular arcs is included in most graphics packages. 3.2 Properties of Circles A circle is defined as the set of points that are all at a given distance (r) from a center position (xc, yc) Fig.(3-1). This distance relationship is expressed by the Pythagorean theorem in Cartesian coordinates as: (x-xc)2+(y-yc)2=r2 (3-1)

Figure (3-1) Circle with center coordinates (xc,yc) and radius r.

We could use this equation to calculate the position of points on a circle circumference by stepping along the x axis in unit steps from xc - r to xc + r and calculating the corresponding y values at each position as:
(3-2)

But this is not the best method for generating a circle. One problem with this approach is that it involves considerable computation at each step. Moreover, the spacing between plotted pixel positions is not uniform, as demonstrated in Fig.(3-2).

Figure(3-2) positive half of a circle plotted with Eq. (3-2) and with (xc,yc) = (0.0).

Another way to eliminate the unequal spacing shown in Fig.(3-2) is to calculate points along the circular boundary using polar coordinates (r) and () Fig.(3-1). Expressing the circle equation in parametric polar form yields the pair of equations: x=xc+r*cos y=yc+r*sin
(3-3)

When a display is generated with these equations using a fixed angular step size, a circle is plotted with equally spaced points along the circumference. The step size chosen for () depends on the application and the display device. Larger angular separations along the circumference can be connected with straight line segments to approximate the circular path. For a more continuous boundary on a raster display, we can set the step size at (l/r). This plots pixel positions that are approximately one unit apart. Algorithm for generating a circle by using polar coordinates: 1-Get the center point (xc,yc) and radius (r). 2-Set the step size (dtheta) to (1/r). 3-Set the starting angle (theta) to zero. 4-While (theta<=6.28) do the following sub steps: 4-1 Calculate the value of x & y: x=xc+r*cos. y=yc+r*sin.
3

4-2 Pset(x,y,color). 4-3 Adjust the value of theta: theta=theta+dtheta. 5-End. Computation can be reduced by considering the symmetry of circles. The shape of the circle is similar in each quadrant. We can generate the circle section in the second quadrant of the xy plane by noting that the two circle sections are symmetric with respect to the y axis. And circle sections in the third and fourth quadrants can be obtained from sections in the first and second quadrants by considering symmetry about the x axis. We can take this one step further and note that there is also symmetry between octants. Circle sections in adjacent octants within one quadrant are symmetric with respect to the 45 line dividing the two octants. These symmetry conditions are illustrated in Fig. (3-3), where a point at position (x, y) on a one-eighth circle sector is mapped into the seven circle points in the other octants of the xy plane. Taking advantage of the circle symmetry in this way we can generate all pixel positions around a circle by calculating only the points within the sector from x = 0 (or x=r) to x = y.

Figure (3-3) symmetry of a circle. Calculation of a circle point (x,y) in one octant yields the circle points shown for the other seven octants.

Algorithm for generating a circle by making use of the symmetry between octants: 1- Get the center point (xc,yc) and radius (r). 2-Set the starting point as: x=r. y=0. 3-Set the step size (dtheta) to (1/r). 4- While (x>=y) do the following sub steps: 4-1 Call CirclePoints procedure* to display (energized) the eight symmetrical points. 4-2 Adjust the value of x & y to get the new point: x=x-y*dtheta. y=y+x*dtheta. 5-End. *Procedure CirclePoints (xc,yc,x,y) { PSet (xc + x, yc + y,color). PSet (xc + x, yc - y,color). PSet (xc + y, yc + x,color). PSet (xc - y, yc + x,color). PSet (xc - x, yc + y,color). PSet (xc - x, yc - y,color). PSet (xc - y, yc - x,color). PSet (xc + y, yc - x,color). }

You might also like