From 4c25bc17afc72c13bfe8ff3375ffc8a46e7027ab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=A1=D0=B0=D0=B2=D0=B5=D0=BB=D1=96=D0=B9=20=D0=A1=D0=B0?= =?UTF-8?q?=D0=B2=D1=91=D0=BD=D0=B0=D0=BA?= <1986developer@gmail.com> Date: Mon, 30 Jan 2023 00:24:30 +0300 Subject: [PATCH] Final --- FractalTreeGtk/CairoWindow.cs | 41 ++++++++++++++++++++++++---------- FractalTreeGtk/Draw/Fractal.cs | 28 ++++++++++++++--------- 2 files changed, 46 insertions(+), 23 deletions(-) diff --git a/FractalTreeGtk/CairoWindow.cs b/FractalTreeGtk/CairoWindow.cs index 12c321e..6dce440 100644 --- a/FractalTreeGtk/CairoWindow.cs +++ b/FractalTreeGtk/CairoWindow.cs @@ -1,7 +1,10 @@ using Gtk; using CairoObjective; -using GLib; using FractalTreeGtk.Draw; +using System; +using System.Threading; +using System.Threading.Tasks; +using System.Diagnostics; namespace FractalTreeGtk { @@ -9,26 +12,30 @@ namespace FractalTreeGtk { static int fractallevels = 2; DrawingArea drawing = new DrawingArea(); - Fractal fractal = new Fractal(fractallevels); + Fractal fractal; public CairoWindow(string title) : base(title) { Fullscreen(); drawing.Drawn += Drawing_Drawn; Add(drawing); ShowAll(); KeyPressEvent += CairoWindow_KeyPressEvent; + CreateNewFractal(); + } + public void CreateNewFractal() + { + fractal = new Fractal(fractallevels, this); } - private void CairoWindow_KeyPressEvent(object o, KeyPressEventArgs args) { if(args.Event.Key == Gdk.Key.equal) { - fractal = new Fractal(++fractallevels); - QueueDraw(); + ++fractallevels; + CreateNewFractal(); } if (args.Event.Key == Gdk.Key.minus && fractallevels > 1) { - fractal = new Fractal(--fractallevels); - QueueDraw(); + --fractallevels; + CreateNewFractal(); } } @@ -41,13 +48,23 @@ namespace FractalTreeGtk Set.Background(new Cairo.Color(0, 0, 0)); foreach (CairoObjective.DrawObjects.Line[] lines in (CairoObjective.DrawObjects.Line[][])fractal.Fractallines) { - foreach (CairoObjective.DrawObjects.Line line in lines) { - branches++; - Line.Make(line); + try + { + foreach (CairoObjective.DrawObjects.Line line in lines) { + branches++; + Line.Make(line); + } + } + catch (NullReferenceException) + { + Trace.WriteLine("Hello Null"); + break; } } - Text.Make($"Total Branches: {branches}", -(AllocatedWidth / 2), -30, new Cairo.Color(1, 1, 1)); - Text.Make($"Levels: {fractallevels}", -(AllocatedWidth / 2), -5, new Cairo.Color(1,1,1)); + Text.Make($"Total Branches: {branches}", -(AllocatedWidth / 2) + 1, -29, 21, new Cairo.Color(0, 0, 0)); + Text.Make($"Levels: {fractallevels}", -(AllocatedWidth / 2) + 1, -4, 21, new Cairo.Color(0, 0, 0)); + Text.Make($"Total Branches: {branches}", -(AllocatedWidth / 2), -30, new Cairo.Color(0.1, 1, 0.1)); + Text.Make($"Levels: {fractallevels}", -(AllocatedWidth / 2), -5, new Cairo.Color(0.1,1,0.1)); } } } diff --git a/FractalTreeGtk/Draw/Fractal.cs b/FractalTreeGtk/Draw/Fractal.cs index feff3bf..87cde8a 100644 --- a/FractalTreeGtk/Draw/Fractal.cs +++ b/FractalTreeGtk/Draw/Fractal.cs @@ -7,31 +7,36 @@ namespace FractalTreeGtk.Draw { internal class Fractal { + public bool Drawing = true; + CairoWindow window; double Length = 100; double MainDegree = 0.4; int levels; int level = 0; double deltaLength = 5; public Branch[][] Fractallines; - public Fractal(int levels) + public Fractal(int levels, CairoWindow window) { - this.levels = levels; - Fractallines = new Branch[levels + 1][]; - CreateBranch(); + this.window = window; + this.levels = levels; + Fractallines = new Branch[levels + 1][]; + CreateBranch(); } private void CreateBranch() { - Fractallines[level] = new Branch[Convert.ToInt32(Math.Pow(2, level))]; - for (int i = 0; i < Fractallines[level].Length; i++) - { - Fractallines[level][i] = new Branch(0, 0, 0, -Length, true, 0); - } - level++; - NextLevel(); + Fractallines[level] = new Branch[Convert.ToInt32(Math.Pow(2, level))]; + for (int i = 0; i < Fractallines[level].Length; i++) + { + Fractallines[level][i] = new Branch(0, 0, 0, -Length, true, 0); + } + level++; + NextLevel(); } private void NextLevel() { + window.QueueDraw(); Length -= deltaLength; + window.QueueDraw(); Fractallines[level] = new Branch[Convert.ToInt32(Math.Pow(2, level))]; int x = 0; for (int i = 0; i < Fractallines[level - 1].Length; i++) @@ -82,6 +87,7 @@ namespace FractalTreeGtk.Draw { NextLevel(); } + else Drawing = false; } //private int SumOfBranches() //{