Advertisement
elena1234

How to draw circle on the console

Apr 4th, 2021
247
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.16 KB | None | 0 0
  1. using System;
  2.  
  3. namespace Shapes
  4. {
  5.     public class Circle : IDrawable
  6.     {
  7.         private int radius;
  8.  
  9.         public Circle(int radius)
  10.         {
  11.             this.Radius = radius;
  12.         }
  13.  
  14.         public int Radius
  15.         {
  16.             get => radius;
  17.             set
  18.             {
  19.                 if (value > 0)
  20.                 {
  21.                     radius = value;
  22.                 }
  23.             }
  24.         }
  25.  
  26.         public void Draw()
  27.         {
  28.             double consoleRatio = Convert.ToDouble(4.0 / 2.0);
  29.             double a = consoleRatio * radius;
  30.             double b = radius;
  31.  
  32.             for (double y = -radius; y <= radius; y++)
  33.             {
  34.                 for (double x = -a; x <= a; x++)
  35.                 {
  36.                     double d = (x / a) * (x / a) + (y / b) * (y / b);
  37.                     if (d > 1 && d < 1.2)
  38.                     {
  39.                         Console.Write('*');
  40.                     }
  41.  
  42.                     else
  43.                     {
  44.                         Console.Write(" ");
  45.                     }
  46.                 }
  47.  
  48.                 Console.WriteLine();
  49.             }
  50.         }
  51.     }
  52. }
  53.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement