📜  画一个没有浮点数的圆

📅  最后修改于: 2021-05-24 15:57:30             🧑  作者: Mango

给定圆的半径,请在不使用浮点算法的情况下绘制圆。

以下程序使用一个简单的概念。令圆的半径为r。考虑要绘制的圆周围大小为(2r + 1)*(2r + 1)的正方形。现在,走过广场内的每个点。对于每个点(x,y),如果(x,y)位于圆内(或x ^ 2 + y ^ 2

C++
// C++ code to demonstrate to draw
// circle without floating 
// point arithmetic
#include 
  
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 code to demonstrate to draw
// circle without floating 
// point arithmetic
  
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 arg[])
{
    drawCircle(8);
}
}
  
// This code is contributed
// by Anant Agarwal.


Python3
# Python3 code to demonstrate to draw
# 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 = " ")
                  
            # If outside the circle, print space
            else:
                print(" ", end = " ")
        print()
  
# Driver Code
if __name__ == "__main__":
    drawCircle(8)
  
# This code is contributed 
# by vibhu4agarwal


C#
// C# code to demonstrate to draw 
// 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 ajit. 
}


PHP


输出: