• Aucun résultat trouvé

Drawing Circles

Dans le document Computer Graphics and Geometric Modeling (Page 70-73)

2.9 More Drawing Algorithms

2.9.2 Drawing Circles

Probably the most straightforward approach to generating points on a circle is to use a polar coordinate parameterization. If, for simplicity, we restrict the discussion to circles of radius r centered at the origin, then this map is given by the formula

The only problem with this is that the sine and cosine functions are relatively com-plicated to evaluate. We want a speedier algorithm. One can use rational functions.

For example, there is the following rational function parameterization of the right half of the unit circle

These rational polynomials can be evaluated rather efficiently using a method of forward differences, but the problem now is that equally spaced t’s do not give rise to equally spaced points on the circle.

The DDA approach that led to a good algorithm in the case of lines is also appli-cable to circles. By differentiating the equation

for a circle of radius r about the origin implicitly with respect to x, one sees that

is the differential equation for that circle. This means that a circle-generating DDA can be written in the form

A natural choice for eis 2-n, where 2n-1£r<2n. Unfortunately, if one were to plot the points that are generated by these equations, one would find that they spiral outward.

From a mathematical standpoint one should not have been surprised because the determinant of the matrix

for this linear transformation is 1 + e2. An ad hoc way to correct this determinant problem is to make a slight change and note that the matrix

1

has determinant equal to 1. The points that are generated by the corresponding trans-formation produce a much better result. The equations for this transtrans-formation are

which reduces to

Notice how the equation for the (n + 1)st value for y now also involves the (n +1)st value of x. The coordinates of the new point have to be computed sequentially, first the x value, then the y value. Before we could compute them in parallel. Furthermore, what we have here is a simple example of an error-correcting term. All good methods for numerical solutions to differential equations have this, something that was alluded to in Section 2.5.1.

Returning to our problem of generating points on a circle, our new system of equa-tions produces points that no longer spiral out. However, having determinant equal to 1 is only a necessary requirement for a transformation to preserve distance. It is not a suf-ficient one. In fact, the points generated by our new transformation form a slight ellipse.

To get a better circle-generating algorithm we start over from scratch with a new approach. Assume that the radius r is an integer and define the “error” function E by

This function measures how close the point (x,y) is to lying on the circle of radius r.

As we generate points on the circle we obviously want to minimize this error. Let us restrict ourselves to the octant of the circle in the first quadrant, which starts at (0,r) and ends at (r/÷2,r/÷2). Note that in this octant as we move from one point to the next the x-coordinate will always increase by 1 and the y-coordinate will either stay the same or decrease by 1. Any other choice would not minimize E. The two cases are illustrated in Figure 2.19. We shall call the two possible moves an R-move or a D-move, respectively.

As we move from point to point, we choose that new point which minimizes E.

However, we can save computation time by computing the new E incrementally. To see this, suppose that we are at (x,y). Then the current E, Ecur, is given by

E=r2-x2-y2.

ED ER |ED| - |ER|

+ + ED-ER=2y-1, always positive

+ - ED+ER

- + this case never happens

- - -ED+ER= -(2y-1), always negative

After an R-move the new E, call it ER, is

After a D-move the new E, call it ED, is

One algorithm for drawing a circle is to choose that move which makes our new error, either ERor ED, have the opposite sign of our current one. The idea is that if we find ourselves outside the circle we should move as quickly as possible back into the circle and vice versa. This leads to Algorithm 2.9.2.1, the Bresenham circle-drawing algorithm.

The only problem with Algorithm 2.9.2.1 is that we were using a heuristic that does not always minimize the real error E (Exercise 2.9.2.1). To get a better algorithm, we have to make that our goal. Choosing the move that minimizes the error can be done by testing the sign of |ED| - |ER|. To gain efficiency we want to avoid having to compute absolute values of numbers. Consider the possible outcomes shown in following table:

E r x y

Algorithm 2.9.2.1. The Bresenham circle-drawing algorithm (one octant).

This table shows that the sign of |ED| - |ER|always agrees with the sign of the auxili-ary variable

Furthermore, G can also be computed incrementally. Let GRand GDdenote the values of G after an R-move or D-move, respectively. If Gcuris the current G value, then

(2.4) and

(2.5) It is easy to derive formulas (2.4) and (2.5). We prove formula (2.4) for GRin case we move right. Recall that

On an R-move,

The other cases are proved in a similar fashion.

Finally, going one step further, the increments to GRand GD themselves can be computed incrementally, producing an improved Algorithm 2.9.2.2. It can be shown that the algorithm produces a best-fit curve for the circle when either the radius r or its square is an integer, but that may not be the case if one tries the same approach when r2is not an integer.

See [Blin87] for a more complete overview of circle drawing algorithms. For a version of the midpoint line-drawing algorithm that works for circles see [VanN85].

Dans le document Computer Graphics and Geometric Modeling (Page 70-73)