Javatpoint Logo
Javatpoint Logo

Draw a circle without floating point arithmetic in C++

Algorithms that only use integer operations must be used to draw circles without the use of floating-point math. Bresenham's circle drawing algorithm is one of the regularly used algorithms for this purpose. Using solely integer arithmetic, this approach efficiently and effectively creates circles.

A version of Bresenham's line method, which is used to draw lines on a raster display, is Bresenham's circle drawing algorithm. It chooses the spots to be plotted on the circle's circumference based on an effective decision-making procedure that only uses integer calculations. Without using floating-point operations, the method can decide where the pixels should be placed to approximate a circle as closely as possible.

The decision parameter is used to determine the next point to be plotted, and it is the main concept behind Bresenham's circle drawing algorithm. The algorithm effectively draws the circle using only integer-based operations by carefully updating this decision parameter based on the prior choice and the mathematical characteristics of the circle. It is suited for systems or circumstances where floating-point operations may be undesirable or impractical because of this feature.

Even though Bresenham's method offers a quick technique to draw circles without the use of floating-point arithmetic, it is crucial to keep in mind that the circles produced by this approach could not be as accurate or smooth as those produced by algorithms that use floating-point operations. However, Bresenham's technique is a useful tool for many applications, particularly in computer graphics on machines with constrained capabilities, for creating circles and other forms without the requirement for floating-point math.

Code:

Output:

                  . . .                   
            . . . . . . . . .             
        . . . . . . . . . . . . .         
      . . . . . . . . . . . . . . .       
    . . . . . . . . . . . . . . . . .     
    . . . . . . . . . . . . . . . . .     
  . . . . . . . . . . . . . . . . . . .   
  . . . . . . . . . . . . . . . . . . .   
  . . . . . . . . . . . . . . . . . . .   
. . . . . . . . . . . . . . . . . . . . . 
. . . . . . . . . . . . . . . . . . . . . 
. . . . . . . . . . . . . . . . . . . . . 
  . . . . . . . . . . . . . . . . . . .   
  . . . . . . . . . . . . . . . . . . .   
  . . . . . . . . . . . . . . . . . . .   
    . . . . . . . . . . . . . . . . .     
    . . . . . . . . . . . . . . . . .     
      . . . . . . . . . . . . . . .       
        . . . . . . . . . . . . .         
            . . . . . . . . .             
                  . . .    

Explanation:

  1. In this example, when given an integer radius as an input, the Draw_Circle function utilizes that radius to calculate the size of the square grid and draw the circle inside of it.
  2. The square grid's dimensions are specified by the variable N, which is set to 2 * radius + 1. The grid will have N rows and N columns, forming a square with N units on each side.
  3. The rows and columns of the square grid are iterated over using two nested loops. The variables k and l are responsible for these loops.
  4. The nested loops contain calculations for the variables xyz and pqr. The coordinates of the current point concerning the grid's center are shown by these variables.
  5. The if statement determines if the current point, denoted by the variables xyz and pqr, is inside the circle. It is accomplished by comparing the point's squared distance from the center to the circle's squared radius (using the Pythagorean theorem). It prints a period (. ), which symbolizes a filled point if the point is inside the circle. In the absence of that, a space (), which stands for an empty point, is printed.
  6. A newline character (n), which signals the transition to the following row of the grid, is displayed after iterating through each point in a row.
  7. Draw_Circle is called by the main function with a radius of 10, and it returns 0.






Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA