This commit is contained in:
Pavel-Savely Savianok 2023-01-30 00:24:30 +03:00
parent 17db4dc996
commit 4c25bc17af
2 changed files with 46 additions and 23 deletions

View File

@ -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));
}
}
}

View File

@ -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()
//{