Circle Drawing Algorithm
   HOME

TheInfoList



OR:

In
computer graphics Computer graphics deals with generating images and art with the aid of computers. Computer graphics is a core technology in digital photography, film, video games, digital art, cell phone and computer displays, and many specialized applications. ...
, the midpoint circle algorithm is an algorithm used to determine the points needed for rasterizing a
circle A circle is a shape consisting of all point (geometry), points in a plane (mathematics), plane that are at a given distance from a given point, the Centre (geometry), centre. The distance between any point of the circle and the centre is cal ...
. It is a generalization of
Bresenham's line algorithm Bresenham's line algorithm is a line drawing algorithm that determines the points of an ''n''-dimensional raster that should be selected in order to form a close approximation to a straight line between two points. It is commonly used to draw li ...
. The algorithm can be further generalized to
conic section A conic section, conic or a quadratic curve is a curve obtained from a cone's surface intersecting a plane. The three types of conic section are the hyperbola, the parabola, and the ellipse; the circle is a special case of the ellipse, tho ...
s.


Summary

This algorithm draws all eight octants simultaneously, starting from each cardinal direction (0°, 90°, 180°, 270°) and extends both ways to reach the nearest multiple of 45° (45°, 135°, 225°, 315°). It can determine where to stop because, when = , it has reached 45°. The reason for using these angles is shown in the above picture: As increases, it neither skips nor repeats any value until reaching 45°. So during the ''while'' loop, increments by 1 with each iteration, and decrements by 1 on occasion, never exceeding 1 in one iteration. This changes at 45° because that is the point where the tangent is =. Whereas before and after. The second part of the problem, the determinant, is far trickier. This determines when to decrement . It usually comes after drawing the pixels in each iteration, because it never goes below the radius on the first pixel. Because in a continuous function, the function for a sphere is the function for a circle with the radius dependent on (or whatever the third variable is), it stands to reason that the algorithm for a discrete (
voxel In computing, a voxel is a representation of a value on a three-dimensional regular grid, akin to the two-dimensional pixel. Voxels are frequently used in the Data visualization, visualization and analysis of medical imaging, medical and scient ...
) sphere would also rely on the midpoint circle algorithm. But when looking at a sphere, the integer radius of some adjacent circles is the same, but it is not expected to have the same exact circle adjacent to itself in the same hemisphere. Instead, a circle of the same radius needs a different determinant, to allow the curve to come in slightly closer to the center or extend out farther. concentric circles In geometry, two or more objects are said to be ''concentric'' when they share the same center. Any pair of (possibly unalike) objects with well-defined centers can be concentric, including circles, spheres, regular polygons, regular polyhe ...
drawn with the midpoint circle algorithm." heights="191px"> File:Concentric circles drawn with Bresenham's circle algorithm (all black) 01.png, On left, all circles are drawn black. File:Concentric circles drawn with Bresenham's circle algorithm.png, On right, red, black and blue are used together to demonstrate the concentricity of the circles.


Algorithm

The objective of the algorithm is to approximate a
circle A circle is a shape consisting of all point (geometry), points in a plane (mathematics), plane that are at a given distance from a given point, the Centre (geometry), centre. The distance between any point of the circle and the centre is cal ...
, more formally put, to approximate the curve x^2 + y^2 = r^2 using pixels; in
layman's terms Plain English (also referred to as layman's terms) is a mode of writing or speaking the English language intended to be easy to understand regardless of one's familiarity with a given topic. It usually avoids the use of rare words and uncommon euph ...
every pixel should be approximately the same distance from the center, as is the definition of a circle. At each step, the path is extended by choosing the adjacent pixel which satisfies x^2 + y^2 \leq r^2 but maximizes x^2 + y^2 . Since the candidate pixels are adjacent, the arithmetic to calculate the latter expression is simplified, requiring only bit shifts and additions. But a simplification can be done in order to understand the bitshift. Keep in mind that a left bitshift of a
binary number A binary number is a number expressed in the Radix, base-2 numeral system or binary numeral system, a method for representing numbers that uses only two symbols for the natural numbers: typically "0" (zero) and "1" (one). A ''binary number'' may ...
is the same as multiplying with 2. Ergo, a left bitshift of the radius only produces the
diameter In geometry, a diameter of a circle is any straight line segment that passes through the centre of the circle and whose endpoints lie on the circle. It can also be defined as the longest Chord (geometry), chord of the circle. Both definitions a ...
which is defined as radius times two. This algorithm starts with the
circle A circle is a shape consisting of all point (geometry), points in a plane (mathematics), plane that are at a given distance from a given point, the Centre (geometry), centre. The distance between any point of the circle and the centre is cal ...
equation. For simplicity, assume the center of the circle is at (0,0). To start with, consider the first octant only, and draw a curve which starts at point (r,0) and proceeds counterclockwise, reaching the angle of 45°. The ''fast'' direction here (the
basis vector In mathematics, a set of elements of a vector space is called a basis (: bases) if every element of can be written in a unique way as a finite linear combination of elements of . The coefficients of this linear combination are referred to as ...
with the greater increase in value) is the y direction (see
Differentiation of trigonometric functions The differentiation of trigonometric functions is the mathematical process of finding the derivative of a trigonometric function, or its rate of change with respect to a variable. For example, the derivative of the sine function is written (''a' ...
). The algorithm always takes a step in the positive y direction (upwards), and occasionally takes a step in the ''slow'' direction (the negative x direction). From the circle equation is obtained the transformed equation x^2 + y^2 - r^2 = 0, where r^2 is computed only once during initialization. Let the points on the circle be a sequence of coordinates of the vector to the point (in the usual basis). Points are numbered according to the order in which drawn, with n=1 assigned to the first point (r,0). For each point, the following holds: :\begin x_n^2 + y_n^2 = r^2 \end This can be rearranged thus: :\begin x_n^2 = r^2 - y_n^2 \end And likewise for the next point: :\begin x_^2 = r^2 - y_^2 \end Since for the first octant the next point will always be at least 1 pixel higher than the last (but also at most 1 pixel higher to maintain continuity), it is true that: :\begin y_^2 &= (y_n + 1)^2 \\ &= y_n^2 + 2y_n + 1 \end :\begin x_^2 = r^2 - y_n^2 - 2y_n - 1 \end So, rework the next-point-equation into a recursive one by substituting x_n^2 = r^2 - y_n^2: :\begin x_^2 = x_n^2 - 2y_n - 1 \end Because of the continuity of a circle and because the maxima along both axes are the same, clearly it will not be skipping points as it advances in the sequence. Usually it stays on the same coordinate, and sometimes advances by one to the left. The resulting coordinate is then translated by adding midpoint coordinates. These frequent integer additions do not limit the
performance A performance is an act or process of staging or presenting a play, concert, or other form of entertainment. It is also defined as the action or process of carrying out or accomplishing an action, task, or function. Performance has evolved glo ...
much, as those square (root) computations can be spared in the inner loop in turn. Again, the zero in the transformed circle equation is replaced by the error term. The initialization of the error term is derived from an offset of ½ pixel at the start. Until the intersection with the perpendicular line, this leads to an accumulated value of r in the error term, so that this value is used for initialization. The frequent computations of
square In geometry, a square is a regular polygon, regular quadrilateral. It has four straight sides of equal length and four equal angles. Squares are special cases of rectangles, which have four equal angles, and of rhombuses, which have four equal si ...
s in the circle equation,
trigonometric Trigonometry () is a branch of mathematics concerned with relationships between angles and side lengths of triangles. In particular, the trigonometric functions relate the angles of a right triangle with ratios of its side lengths. The field ...
expressions and
square root In mathematics, a square root of a number is a number such that y^2 = x; in other words, a number whose ''square'' (the result of multiplying the number by itself, or y \cdot y) is . For example, 4 and −4 are square roots of 16 because 4 ...
s can again be avoided by dissolving everything into single steps and using
recursive Recursion occurs when the definition of a concept or process depends on a simpler or previous version of itself. Recursion is used in a variety of disciplines ranging from linguistics to logic. The most common application of recursion is in m ...
computation of the quadratic terms from the preceding iterations.


Variant with integer-based arithmetic

Just as with
Bresenham's line algorithm Bresenham's line algorithm is a line drawing algorithm that determines the points of an ''n''-dimensional raster that should be selected in order to form a close approximation to a straight line between two points. It is commonly used to draw li ...
, this algorithm can be optimized for integer-based math. Because of symmetry, if an algorithm can be found that only computes the pixels for one octant, the pixels can be reflected to get the whole circle. We start by defining the radius error as the difference between the exact representation of the circle and the center point of each pixel (or any other arbitrary mathematical point on the pixel, so long as it is consistent across all pixels). For any pixel with a center at (x_i, y_i), the radius error is defined as: :RE(x_i,y_i) = \left\vert x_i^2 + y_i^2 - r^2 \right\vert For clarity, this formula for a circle is derived at the origin, but the algorithm can be modified for any location. It is useful to start with the point (r,0) on the positive X-axis. Because the radius will be a whole number of pixels, clearly the radius error will be zero: :RE(x_i,y_i) = \left\vert x_i^2 + 0^2 - r^2 \right\vert = 0 Because it starts in the first counter-clockwise positive octant, it will step in the direction with the greatest ''travel'', the Y direction, so it is clear that y_ = y_i + 1. Also, because it concerns this octant only, the values have only 2 options: to stay the same as the prior iteration, or decrease by 1. A decision variable can be created that determines if the following is true: :RE(x_i-1, y_i+1) < RE(x_i,y_i+1) If this inequality holds, then plot (x_i-1, y_i+1); if not, then plot (x_i,y_i+1). So, how to determine if this inequality holds? Start with a definition of radius error: : \begin RE(x_i-1, y_i+1) & < RE(x_i,y_i+1) \\ \left\vert (x_i-1)^2 + (y_i+1)^2 - r^2 \right\vert & < \left\vert x_i^2 + (y_i+1)^2 - r^2 \right\vert \\ \left\vert (x_i^2 - 2 x_i + 1) + (y_i^2 + 2 y_i + 1) - r^2 \right\vert & < \left\vert x_i^2 + (y_i^2 + 2 y_i + 1) - r^2 \right\vert \\ \end The absolute value function does not help, so square both sides, since a square is always positive: : \begin \left (x_i^2 - 2 x_i + 1) + (y_i^2 + 2 y_i + 1) - r^2 \right 2 & < \left x_i^2 + (y_i^2 + 2 y_i + 1) - r^2 \right 2 \\ \left (x_i^2 + y_i^2 - r^2 + 2 y_i + 1) + (1 - 2 x_i) \right 2 & < \left x_i^2 + y_i^2 - r^2 + 2 y_i + 1 \right 2 \\ \left ( x_i^2 + y_i^2 - r^2 + 2 y_i + 1 \right )^2 + 2 (1 - 2 x_i) (x_i^2 + y_i^2 - r^2 + 2 y_i + 1) + (1 - 2 x_i)^2 & < \left x_i^2 + y_i^2 - r^2 + 2 y_i + 1 \right 2 \\ 2 (1 - 2 x_i) (x_i^2 + y_i^2 - r^2 + 2 y_i + 1) + (1 - 2 x_i)^2 & < 0 \\ \end Since > 0, the term (1 - 2 x_i) < 0, so dividing gets: : \begin 2 \left (x_i^2 + y_i^2 - r^2) + (2 y_i + 1) \right + (1 - 2 x_i) & > 0 \\ 2 \left RE(x_i,y_i) + Y_\text \right + X_\text & > 0 \\ \end Thus, the decision criterion changes from using floating-point operations to simple integer addition, subtraction, and bit shifting (for the multiply by 2 operations). If , then decrement the value. If , then keep the same value. Again, by reflecting these points in all the octants, a full circle results. We may reduce computation by only calculating the delta between the values of this decision formula from its value at the previous step. We start by assigning as which is the initial value of the formula at , then as above at each step if we update it as (and decrement ), otherwise thence increment as usual.


Jesko's Method

The algorithm has already been explained to a large extent, but there are further optimizations. The new presented method gets along with only 5 arithmetic operations per step (for 8 pixels) and is thus best suitable for low-performance systems. In the "if" operation, only the sign is checked (positive? Yes or No) and there is a variable assignment, which is also not considered an arithmetic operation. The initialization in the first line (shifting by 4 bits to the right) is only due to beauty and not really necessary. So we get countable operations within main-loop: # The comparison x >= y (is counted as a subtraction: x - y >= 0) # y=y+1 ++# t1 + y # t1 - x #: The comparison t2 >= 0 is not counted as no real arithmetic takes place. In
two's complement Two's complement is the most common method of representing signed (positive, negative, and zero) integers on computers, and more generally, fixed point binary values. Two's complement uses the binary digit with the ''greatest'' value as the ''s ...
representation of the vars only the
sign bit In computer science, the sign bit is a bit in a signed number representation that indicates the sign of a number. Although only signed numeric data types have a sign bit, it is invariably located in the most significant bit position, so the term ...
has to be checked. # x=x-1 -- Operations: 5 ''t1'' = r / 16 ''x'' = r ''y'' = 0 Repeat Until ''x'' < ''y'' Pixel (x, y) and all symmetric pixels are colored (8 times) ''y'' = ''y'' + 1 ''t1'' = ''t1'' + ''y'' ''t2'' = ''t1'' - ''x'' If ''t2'' >= 0 ''t1'' = ''t2'' ''x'' = ''x'' - 1


Drawing incomplete octants

The implementations above always draw only complete octants or circles. To draw only a certain arc from an angle \alpha to an angle \beta, the algorithm needs first to calculate the x and y coordinates of these end points, where it is necessary to resort to trigonometric or square root computations (see
Methods of computing square roots Square root algorithms compute the non-negative square root \sqrt of a positive real number S. Since all square roots of natural numbers, other than of perfect squares, are irrational, square roots can usually only be computed to some finite pre ...
). Then the Bresenham algorithm is run over the complete octant or circle and sets the pixels only if they fall into the wanted interval. After finishing this arc, the algorithm can be ended prematurely. If the angles are given as
slope In mathematics, the slope or gradient of a Line (mathematics), line is a number that describes the direction (geometry), direction of the line on a plane (geometry), plane. Often denoted by the letter ''m'', slope is calculated as the ratio of t ...
s, then no trigonometry or square roots are necessary: simply check that y/x is between the desired slopes.


Generalizations

It is also possible to use the same concept to rasterize a
parabola In mathematics, a parabola is a plane curve which is Reflection symmetry, mirror-symmetrical and is approximately U-shaped. It fits several superficially different Mathematics, mathematical descriptions, which can all be proved to define exactl ...
,
ellipse In mathematics, an ellipse is a plane curve surrounding two focus (geometry), focal points, such that for all points on the curve, the sum of the two distances to the focal points is a constant. It generalizes a circle, which is the special ty ...
, or any other two-dimensional
curve In mathematics, a curve (also called a curved line in older texts) is an object similar to a line, but that does not have to be straight. Intuitively, a curve may be thought of as the trace left by a moving point. This is the definition that ...
.


References

{{Reflist


External links


Drawing circles
- An article on drawing circles, that derives from a simple scheme to an efficient one
Midpoint Circle Algorithm in several programming languages
Geometric algorithms Digital geometry de:Rasterung von Kreisen#Midpoint-Algorithmus