Dibujar un círculo sin aritmética de punto flotante

Dado el radio de un círculo, dibuje el círculo sin usar la aritmética de coma flotante.
El siguiente programa utiliza un concepto simple. Sea el radio del círculo r. Considere un cuadrado de tamaño (2r+1)*(2r+1) alrededor del círculo a dibujar. Ahora camina por todos los puntos dentro del cuadrado. Para cada punto (x, y), si (x, y) se encuentra dentro del círculo (o x^2+ y^2 < r^2), entonces imprímalo, de lo contrario imprima espacio.
 

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 function
int  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 Code
drawCircle(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>

Producción : 
 

Escriba comentarios si encuentra algo incorrecto o si desea compartir más información sobre el tema tratado anteriormente.
 

Publicación traducida automáticamente

Artículo escrito por GeeksforGeeks-1 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *