Lecture 3: CIRCLE-GENERATING ALGORITHM

advertisement
Lecture 3:
CIRCLE-GENERATING
ALGORITHM
Assistant Teacher: Suad Abdulelah Alasadi
7/10/2013
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).
Algorithm One:
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:
Disadvantage of this algorithm
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.
Algorithm Two:
Another way to eliminate the unequal spacing is to
calculate points along the circular boundary using
polar coordinates (r) and (Ɵ). Expressing the circle
equation in parametric polar form yields the pair of
equations:
x=xc+r*cos Ɵ
y=yc+r*sin Ɵ
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Ɵ.
4-2 Pset(x,y,color).
4-3 Adjust the value of theta:
theta=theta+dtheta.
5-End.
Algorithm Three:
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.
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).
}
The end
Thank you for listening
Download