Algorithm of midpoint drawing circle

Source: Internet
Author: User
Tags square root

In Plane analytic geometry, the equation of the circle can be described as (x–x0) 2 + (y–y0) 2 = R2, where (x0, y0) is the center of the coordinates, R is the radius of the circle, in particular, when (x0, y0) is the coordinate center point, the circle equation can be reduced to x2 + y2 = R2. In computer graphics, as well as straight lines, there are also problems in the display or output of the dot-matrix output device, so a raster scan conversion algorithm is also needed. In order to simplify, we first consider the circle of the center at the origin of the generation, for the central is not the origin of the circle, you can through the translation of coordinates to obtain the corresponding position of the circle.

Before scanning conversion, you need to understand the characteristics of a circle, that is, eight points of the circle. As shown in figure (1):

Figure (1) eight-cent symmetry of a circle

The circle at the origin of the center has four axis of symmetry x = 0, y = 0, x = y and x = y, if a little P (x,y) is known on the arc, you can get seven symmetric points about the four symmetric axes: (x, Y), (-X, X), (-X,-y), (y, ×), (Y,-X), (-y, X), (-y,-X), this property is called eight-point symmetry. So as long as you can draw a one-eighth of the arc, you can use the principle of symmetry to get the whole circle.

There are several easy ways to get round scan transformation, first of all, introduce the rectangular coordinates method. Known circle equation: x2 + y2 = R2, if you take x as the argument, y can be calculated at any time.

When the circle is generated, scan the circle of One-fourth, let the independent variable x from 0 to R in unit step increments, at each step can solve the Y, and then call the point function to draw a circle point. But in doing so, the algorithm is inefficient because of the square root operation and all floating-point operations. And when X is near the R value (the center of the circle at the origin), near the point (r,0) on the circumference, because the slope of the circle tends to infinity, because the floating-point number rounding needs rounding, so that the circumference has a larger gap. Next, we introduce the polar coordinate method, assuming that the angle between a point P (x,y) and the x axis in the rectangular coordinate system is θ, the polar equation of the circle is:

x = rcosθ

y = rsinθ

The circle is generated by the eight-point symmetry of the circle, so that the value range of the independent variable θ (0,45°) can be used to draw the whole circle. This method involves trigonometric functions calculation and multiplication operation, and has a large computational load. Both the Cartesian and polar coordinate methods are not efficient algorithms, so they are only existed as theoretical methods, and they are not used in computer graphics to generate circles. The following is a brief introduction to some of the more useful circle generation algorithms in computer graphics.

1. Circle method of Midpoint drawing

The first is the midpoint of the circle method, considering the center at the origin, radius of the circle of R in the first quadrant of the one-eighth arc, from point (0, R) points (r/, r/) clockwise direction to determine this arc. Assuming that a point pi (xi, Yi) is already the closest point to the actual arc on the arc, the next point of Pi may only be one of the P1 or lower-right P2 on the right-hand side, as shown in Figure (2):

Figure (2) example of the dotted line method

Construct discriminant function:

F (x, y) = x2 + y2–r2

When f (x, Y) = 0, the point is on the circle, when F (x, Y) > 0, the point is outside the circle, when F (x, Y) < 0, the point in the circle is expressed. If M is the midpoint of the P1 and P2, then the coordinates of M are (xi + 1, yi–0.5), when F (xi + 1, yi–0.5) < 0 o'clock, M points in the circle, the P1 point is closer to the actual arc and should be taken as the next point of the circle. Similarly, when F (xi + 1, yi–0.5) > 0 o'clock, P2 is closer to the actual arc, the P2 should be taken as the next point. When F (xi + 1, yi–0.5) = 0 o'clock, P1 and P2 can be used as the next point of the Circle, the algorithm convention takes P2 as the next point.

Now bring the M-point coordinate (xi + 1, yi–0.5) into the discriminant function f (x, y), and get the discriminant D:

D = F (xi + 1, yi–0.5) = (xi + 1) 2 + (yi–0.5) 2–r2

If D < 0, then take P1 as the next point, at which point the discriminant of the next P1 is:

d ' = F (xi + 2, yi–0.5) = (xi + 2) 2 + (yi–0.5) 2–r2

After expansion, the D is brought into the recursive relationship that can be discriminant:

d ' = d + 2xi + 3

If d > 0, then take P2 as the next point, at which point the discriminant of the next P2 is:

d ' = F (xi + 2, yi–1.5) = (xi + 2) 2 + (yi–1.5) 2–r2

After expansion, the D is brought into the recursive relationship that can be discriminant:

d ' = d + 2 (xi-yi) + 5

In particular, at the first point (0, R) of the first quadrant, the initial value of discriminant D can be d0:

D0 = F (1, r–0.5) = (r–0.5) 2–R2 = 1.25-r

According to the above analysis, we can write the algorithm of the midpoint circle method. Considering that the center is not at the origin of the case, the calculated coordinates need to be translated, the following is the general point of the circle method of the source code:

-void mp_circle (int xc, int yc, int r)

27 {

int x, y;

Double D;

30

to x = 0;

y = r;

D = 1.25-r;

Circleplot (XC, YC, x, y);

while (x < y)

36 {

Panax Notoginseng if (d < 0)

38 {

D = d + 2 * x + 3;

40}

Or else

42 {

D = d + 2 * (x-y) + 5;

y--;

45}

+ x + +;

Circleplot (XC, YC, x, y);

48}

49}

The parameter XC and YC are center coordinates, R is the radius, and the Circleplot () function is the auxiliary function that calculates the position of eight points with reference to the eight-point symmetry of the circle.

The specific actions of Circleplot () are:

Draw Circle Method (X, Y, P, Q).

Call Putpixel (X + P, Y + Q).
Call Putpixel (x-p, Y + Q).
Call Putpixel (X + P, y-q).
Call Putpixel (X-p, y-q).
Call Putpixel (X + Q, Y + P).
Call Putpixel (x-q, Y + P).
Call Putpixel (X + Q, y-p).
Call Putpixel (X-q, y-p).

2. Improved-bresenham algorithm of midpoint-drawing circle method

In the circle method of midpoint drawing, the calculation discriminant D uses floating-point operation, which affects the generation efficiency of the circle. If the discriminant specification can be used to integer operation, it can simplify the calculation and improve the efficiency. So there are many improvements to the midpoint circle method, one of which is to change the initial value of D from 1.25–r to 1–r, taking into account that the radius R of the circle is always greater than 2, so this modification does not ring the symbol of the initial value of D, and can avoid floating-point operations. There is another way is to enlarge the calculation of D twice times, while the initial value to 3–2R, so as to avoid floating-point operations, multiply two operations can also be replaced by a rapid shift, using 3–2R as the initial value of the improved algorithm, also known as the Bresenham algorithm:

bresenham_circle void (int xc, int yc, int r)

53 {

int x, y, D;

55

x = 0;

y = r;

D = 3-2 * r;

Circleplot (XC, YC, x, y);

while (x < y)

61 {

if (d < 0)

63 {

D = d + 4 * x + 6;

65}

Or else

67 {

D = d + 4 * (x-y) + 10;

y--;

70}

+ x + +;

Circleplot (XC, YC, x, y);

73}

74}

3, positive and negative to determine the circle method of drawing

In addition to the midpoint of the circle algorithm, there is a circle algorithm is also the use of the current point generated by the circle function to identify the symbol, the use of negative feedback to determine the next point to generate a direct arc, is positive and negative method, the following introduction of the positive and negative method of the algorithm implementation.

Positive and negative method according to the Circle function: F (x, y) = x2 + y2–r2 value, the planar area is divided into circles and outside the circle, as shown in Figure (3):

Figure (3) schematic diagram of positive and negative method

Assuming that the arc is generated from a to B direction, when a certain point pi is determined, the value of the next pi+1 of Pi is judged according to the value of F (xi, Yi), and the principle of determination is:

1. When F (xi, Yi) ≤0: Take xi+1 = Xi+1,yi+1 = Yi. That is to take one step to the right, from within the circle towards the circle. From Pi to pi+1 in the corresponding graph (3-a).

2, when F (xi, Yi) > 0 o'clock: Take xi+1 = Xi,yi+1 = yi-1. That is to go down one step, from the circle outside the circle. From Pi to pi+1 in the corresponding graph (3-b).

Because the direction of the next point is to go to the circle or to the outside of the circle depends on F (xi, Yi) of positive and negative, so called positive and negative method. The recursive formula for the discriminant F (xi, Yi) is also calculated separately in two different cases:

1. When F (xi, Yi

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.