Draw a circle without floating point arithmetic

Given a radius of a circle, draw the circle without using floating point arithmetic.
The following program uses a simple concept. Let the radius of the circle be r. Consider a square of size (2r+1)*(2r+1) around the circle to be drawn. Now walk through every point inside the square. For every point (x,y), if (x, y) lies inside the circle (or x^2+ y^2 < r^2), then print it, otherwise print space.
C++
// C++ program to draw a circle without// floating point arithmetic#include <stdio.h>Â
void drawCircle(int r){Â Â Â Â // Consider a rectangle of size N*NÂ Â Â Â int N = 2*r+1;Â
    int x, y; // Coordinates inside the rectangleÂ
    // Draw a square of size N*N.    for (int i = 0; i < N; i++)    {        for (int j = 0; j < N; j++)        {            // Start from the left most corner point            x = i-r;            y = j-r;Â
            // If this point is inside the circle, print it            if (x*x + y*y <= r*r+1 )                printf(".");            else // If outside the circle, print space                printf(" ");            printf(" ");        }        printf("\n");    }}Â
// Driver Program to test above functionint main(){    drawCircle(8);    return 0;} |
Java
// Java program to draw a circle without// floating point arithmeticÂ
import java.io.*;Â
class GFG {static void drawCircle(int r) { Â Â Â Â // Consider a rectangle of size N*N Â Â Â Â int N = (2*r+1); Â
    int x, y; // Coordinates inside the rectangle Â
    // Draw a square of size N*N.     for (int i = 0; i < N; i++)     {         for (int j = 0; j < N; j++)         {             // Start from the left most corner point             x = i-r;             y = j-r; Â
            // If this point is inside the circle, print it             if (x*x + y*y <= r*r+1 )                     System.out.print(".");             else // If outside the circle, print space                     System.out.print(" ");                 System.out.print(" ");         }             System.out.println();     } } Â
// Driver Program to test above function     public static void main (String[] args) {        drawCircle(8);     }//This code is contributed by ajit. } |
Python3
# Python3 program to draw a circle without # floating point arithmetic def drawCircle(r) :         # Consider a rectangle of size N*N     N = 2*r + 1       # Draw a square of size N*N.     for i in range(N) :            for j in range(N) :                     # Start from the left most corner point             x = i - r             y = j - r               # If this point is inside the circle, print it             if (x * x + y * y <= r * r + 1 ) :                 print(".", end = "")             else :# If outside the circle, print space                 print(" ", end = "")             print(" ", end = "")                     print()             drawCircle(8)Â
# This code is contributed by divyeshrabadiya07. |
C#
// C#Â program to draw a circle without// floating point arithmeticÂ
using System;Â
public class GFG{Â Â Â Â static void drawCircle(int r) { Â Â Â Â // Consider a rectangle of size N*N Â Â Â Â int N = (2*r+1); Â
    int x, y; // Coordinates inside the rectangle Â
    // Draw a square of size N*N.     for (int i = 0; i < N; i++)     {         for (int j = 0; j < N; j++)         {             // Start from the left most corner point             x = i-r;             y = j-r; Â
            // If this point is inside the circle, print it             if (x*x + y*y <= r*r+1 )                     Console.Write(".");             else // If outside the circle, print space                     Console.Write(" ");                 Console.Write(" ");         }             Console.WriteLine();     } } Â
// Driver Program to test above function     static public void Main (){        drawCircle(8);     }//This code is contributed by Sachin. } |
PHP
<?php// PHP program to draw a circle without// floating point arithmeticÂ
function drawCircle($r){    // Consider a rectangle    // of size N*N    $N = 2 * $r + 1;Â
    // Coordinates inside    // the rectangle    $x; $y; Â
    // Draw a square of size N*N.    for ($i = 0; $i < $N; $i++)    {        for ($j = 0; $j < $N; $j++)        {            // Start from the left             // most corner point            $x = $i - $r;            $y = $j - $r;Â
            // If this point is inside            // the circle, print it            if ($x * $x + $y * $y <= $r * $r + 1 )                echo ".";                         // If outside the circle,             // print space            else                echo " ";            echo " ";        }        echo "\n";    }}Â
// Driver CodedrawCircle(8);Â
// This code is contributed by aj_36?> |
Javascript
<script>// javascript program to draw a circle without// floating point arithmeticÂ
    function drawCircle(r) {        // Consider a rectangle of size N*N        var N = (2 * r + 1);Â
        var x, y; // Coordinates inside the rectangleÂ
        // Draw a square of size N*N.        for (i = 0; i < N; i++)        {            for (j = 0; j < N; j++)             {                             // Start from the left most corner point                x = i - r;                y = j - r;Â
                // If this point is inside the circle, print it                if (x * x + y * y <= r * r + 1)                    document.write(". ");                else // If outside the circle, print space                    document.write("  ");                document.write("  ");            }            document.write("<br/>");        }    }Â
    // Driver Program to test above function        drawCircle(8);Â
// This code is contributed by gauravrajput1 </script> |
Output
. . .
. . . . . . . . .
. . . . . . . . . . .
. . . . . . . . . . . . .
. . . . . . . . . . . . . . .
. . . . . . . . . . . . . . .
. . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . .
. . . . . . . . . . . . . . .
. . . . . . . . . . . . . . .
. . . . . . . . . . . . .
. . . . . . . . . . .
. . . . . . . . .
. . .
Time complexity: O(N2)
Auxiliary space: O(1)
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above
Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 zambiatek!
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 zambiatek!



