using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Circles
{
class Program
{
///
/// Структура круга
///
struct t_circle {
public int x, y, d;
public t_circle(int x, int y, int d)
{
this.x = x;
this.y = y;
this.d = d;
}
}
static List l_circles = new List();
///
/// Рекурсивно добавляет структуры кругов в массив, пока n>0
///
static void add_circle( int x, int y, int dia, int n)
{
if (n>0)
{
//Console.WriteLine($"{x}, {y}, {dia}, {n}");
t_circle t = new t_circle(x, y, dia);
l_circles.Add(t);
add_circle(x - dia / 2, y, dia / 2, n - 1);
add_circle(x + dia / 2, y, dia / 2, n - 1);
add_circle(x, y - dia / 2, dia / 2, n - 1);
add_circle(x, y + dia / 2, dia / 2, n - 1);
}
}
static void Main(string[] args)
{
Console.Write("Enter depth of circles [1..6] : ");
string val = Console.ReadLine();
try
{
int n = Convert.ToInt32(val);
if (n<1 || n>6)
{
Console.WriteLine("invalid input!");
} else
{
int dia = Convert.ToInt32(20 * Math.Pow(2, n));
int w = dia * 2;
add_circle(w/2, w/2, dia, n);
Bitmap bmp = new Bitmap(w, w);
using (Graphics graph = Graphics.FromImage(bmp))
{
Rectangle ImageSize = new Rectangle(0, 0, w, w);
graph.FillRectangle(Brushes.White, ImageSize);
Pen pen = new Pen(Color.Black, 1);
foreach (var t in l_circles)
graph.DrawEllipse(pen, t.x - t.d / 2, t.y - t.d / 2, t.d, t.d);
}
string filePath = AppDomain.CurrentDomain.BaseDirectory + $"out_{n}.bmp";
bmp.Save(filePath);
Console.WriteLine($"Image saved to \"{filePath}\"");
}
}
catch (Exception e)
{
Console.WriteLine(e.Message);
throw;
}
}
}
}