From 4e18d216e63de3bffa825e77328c8c1daccd4959 Mon Sep 17 00:00:00 2001
From: Saveliy Savenak <1986developer@gmail.com>
Date: Fri, 30 Aug 2024 16:48:26 +0300
Subject: [PATCH] Previous commit
---
FractalTree/CairoWindow.cs | 85 -----------------
FractalTree/DrawObjects/Grid.cs | 49 ----------
FractalTree/FractalTree.csproj | 19 ----
FractalTree/Program.cs | 15 ---
FractalTreeGtk/CairoWindow.cs | 74 +++++++++------
FractalTreeGtk/Draw/Branch.cs | 14 ++-
FractalTreeGtk/Draw/Fractal.cs | 157 ++++++++++++++------------------
7 files changed, 117 insertions(+), 296 deletions(-)
delete mode 100644 FractalTree/CairoWindow.cs
delete mode 100644 FractalTree/DrawObjects/Grid.cs
delete mode 100644 FractalTree/FractalTree.csproj
delete mode 100644 FractalTree/Program.cs
diff --git a/FractalTree/CairoWindow.cs b/FractalTree/CairoWindow.cs
deleted file mode 100644
index 52f36ff..0000000
--- a/FractalTree/CairoWindow.cs
+++ /dev/null
@@ -1,85 +0,0 @@
-using Gtk;
-using CairoObjective;
-using System.Linq;
-using System.Diagnostics;
-
-namespace FractalTree
-{
- class CairoWindow : Window
- {
- DrawingArea drawing = new DrawingArea();
- DrawObjects.Grid grid;
- int Scaler = 50;
- int offsetX = 0;
- int offsetY = 0;
- public CairoWindow(string title) : base(title)
- {
- Fullscreen();
- DeleteEvent += delegate { Application.Quit(); };
- drawing.Drawn += OnDrawn;
- KeyPressEvent += CairoWindow_KeyPressEvent;
- Add(drawing);
- ShowAll();
- Line.DefaultSize = 2;
- }
-
- private void CairoWindow_KeyPressEvent(object o, KeyPressEventArgs args)
- {
- if(args.Event.Key == Gdk.Key.equal)
- {
- Trace.WriteLine("+");
- Scaler++;
- QueueDraw();
- }
- if (args.Event.Key == Gdk.Key.minus && Scaler > 1)
- {
- Trace.WriteLine("-");
- Scaler--;
- QueueDraw();
- }
- if (args.Event.Key == Gdk.Key.a)
- {
- Trace.WriteLine("←");
- offsetX-=10;
- QueueDraw();
- }
- if (args.Event.Key == Gdk.Key.d)
- {
- Trace.WriteLine("→");
- offsetX+=10;
- QueueDraw();
- }
- if (args.Event.Key == Gdk.Key.w)
- {
- Trace.WriteLine("↑");
- offsetY += 10;
- QueueDraw();
- }
- if (args.Event.Key == Gdk.Key.s)
- {
- Trace.WriteLine("↓");
- offsetY -= 10;
- QueueDraw();
- }
- }
-
- private void OnDrawn(object sender, DrawnArgs args)
- {
- MainDrawer();
- }
- private void MainDrawer()
- {
- grid = new(AllocatedWidth, AllocatedHeight, Scaler, offsetX, offsetY);
- Set.Context = Gdk.CairoHelper.Create(drawing.GdkWindow);
- Set.Background(new Cairo.Color(0, 0, 0));
- foreach(CairoObjective.DrawObjects.Line gridline in grid.LinesHorizontal)
- {
- Line.Make(gridline, new Cairo.Color(1,1,1));
- }
- foreach (CairoObjective.DrawObjects.Line gridline in grid.LinesVertical)
- {
- Line.Make(gridline, new Cairo.Color(1, 1, 1));
- }
- }
- }
-}
diff --git a/FractalTree/DrawObjects/Grid.cs b/FractalTree/DrawObjects/Grid.cs
deleted file mode 100644
index 1495ba0..0000000
--- a/FractalTree/DrawObjects/Grid.cs
+++ /dev/null
@@ -1,49 +0,0 @@
-using CairoObjective.DrawObjects;
-using System;
-using System.Diagnostics;
-
-namespace FractalTree.DrawObjects
-{
- internal class Grid
- {
- public Line[] LinesHorizontal;
- public Line[] LinesVertical;
- int Scaler;
- int Width;
- int Height;
- int offsetX;
- int offsetY;
- ///
- /// Make Grid Array of lines
- ///
- /// Width of Window
- /// Height of Window
- /// Pixels Per Lines
- public Grid(int Width, int Height, int Scaler, int offsetX, int offsetY)
- {
- this.Width = Width;
- this.Height = Height;
- this.Scaler = Scaler;
- this.offsetX = offsetX;
- this.offsetY = offsetY;
- LinesHorizontal = new Line[this.Width / Scaler + 1];
- LinesVertical = new Line[this.Height / Scaler + 1];
- MakeHorizontal();
- MakeVertical();
- }
- private void MakeHorizontal()
- {
- for(int i = 0; i < Width / Scaler + 1; i++)
- {
- LinesHorizontal[i] = new Line(i * Scaler + offsetX, 0, i * Scaler, Height);
- }
- }
- private void MakeVertical()
- {
- for (int i = 0; i < Height / Scaler + 1; i++)
- {
- LinesVertical[i] = new Line(0, i * Scaler, Width, i * Scaler);
- }
- }
- }
-}
diff --git a/FractalTree/FractalTree.csproj b/FractalTree/FractalTree.csproj
deleted file mode 100644
index 0849e29..0000000
--- a/FractalTree/FractalTree.csproj
+++ /dev/null
@@ -1,19 +0,0 @@
-
-
-
- WinExe
- net6.0
- disable
- enable
-
-
-
-
-
-
-
-
-
-
-
-
diff --git a/FractalTree/Program.cs b/FractalTree/Program.cs
deleted file mode 100644
index 307a9a3..0000000
--- a/FractalTree/Program.cs
+++ /dev/null
@@ -1,15 +0,0 @@
-using Gtk;
-
-namespace FractalTree
-{
- internal class Program
- {
- static void Main()
- {
- Application.Init();
- //Create the Window
- new CairoWindow("Window");
- Application.Run();
- }
- }
-}
\ No newline at end of file
diff --git a/FractalTreeGtk/CairoWindow.cs b/FractalTreeGtk/CairoWindow.cs
index 6dce440..50478a3 100644
--- a/FractalTreeGtk/CairoWindow.cs
+++ b/FractalTreeGtk/CairoWindow.cs
@@ -2,69 +2,85 @@
using CairoObjective;
using FractalTreeGtk.Draw;
using System;
-using System.Threading;
-using System.Threading.Tasks;
using System.Diagnostics;
+using Key = Gdk.Key;
namespace FractalTreeGtk
{
internal class CairoWindow : Window
{
- static int fractallevels = 2;
- DrawingArea drawing = new DrawingArea();
- Fractal fractal;
- public CairoWindow(string title) : base(title) {
+ private int FractalLevels { get; set; } = 2;
+ readonly DrawingArea _drawing = new DrawingArea();
+ Fractal _fractal = null!;
+
+ public CairoWindow(string title) : base(title)
+ {
Fullscreen();
- drawing.Drawn += Drawing_Drawn;
- Add(drawing);
+ _drawing.Drawn += Render;
+ Add(_drawing);
ShowAll();
KeyPressEvent += CairoWindow_KeyPressEvent;
- CreateNewFractal();
+ GenerateFractal();
}
- public void CreateNewFractal()
+
+ private void GenerateFractal()
{
- fractal = new Fractal(fractallevels, this);
+ Trace.WriteLine("Generating fractal");
+ _fractal = new Fractal(FractalLevels, this);
}
+
private void CairoWindow_KeyPressEvent(object o, KeyPressEventArgs args)
{
- if(args.Event.Key == Gdk.Key.equal)
+ switch (args.Event.Key)
{
- ++fractallevels;
- CreateNewFractal();
- }
- if (args.Event.Key == Gdk.Key.minus && fractallevels > 1)
- {
- --fractallevels;
- CreateNewFractal();
+ case Key.equal: //Increase fractal levels
+ case Key.plus:
+ FractalLevels++;
+ GenerateFractal();
+ break;
+ case Key.minus: //Decrease fractal levels
+ if (FractalLevels > 1)
+ {
+ FractalLevels--;
+ GenerateFractal();
+ }
+
+ break;
+ case Key.r: //Regenerate fractal
+ GenerateFractal();
+ break;
}
}
- private void Drawing_Drawn(object o, DrawnArgs args)
+ private void Render(object o, DrawnArgs args)
{
long branches = 0;
Text.CenterText = false;
- args.Cr.Translate(AllocatedWidth / 2, AllocatedHeight);
+ args.Cr.Translate(AllocatedWidth / 2f, AllocatedHeight);
Set.Context = args.Cr;
Set.Background(new Cairo.Color(0, 0, 0));
- foreach (CairoObjective.DrawObjects.Line[] lines in (CairoObjective.DrawObjects.Line[][])fractal.Fractallines)
+ foreach (CairoObjective.DrawObjects.Line[] lines in
+ _fractal.FractalLines as CairoObjective.DrawObjects.Line[][])
{
try
{
- foreach (CairoObjective.DrawObjects.Line line in lines) {
+ foreach (CairoObjective.DrawObjects.Line line in lines)
+ {
branches++;
Line.Make(line);
}
}
- catch (NullReferenceException)
+ catch (NullReferenceException e)
{
- Trace.WriteLine("Hello Null");
+ Trace.WriteLine(e);
break;
}
}
+
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));
+ Text.Make($"Levels: {FractalLevels}", -(AllocatedWidth / 2) + 1, -4, 21, new Cairo.Color(0, 0, 0));
+ Text.Make($"Total Branches: {branches}", -(AllocatedWidth / 2), -30, 21, new Cairo.Color(0.1, 1, 0.1));
+ Text.Make($"Levels: {FractalLevels}", -(AllocatedWidth / 2), -5, 21, new Cairo.Color(0.1, 1, 0.1));
}
}
-}
+}
\ No newline at end of file
diff --git a/FractalTreeGtk/Draw/Branch.cs b/FractalTreeGtk/Draw/Branch.cs
index 03aa9e6..34f657c 100644
--- a/FractalTreeGtk/Draw/Branch.cs
+++ b/FractalTreeGtk/Draw/Branch.cs
@@ -1,15 +1,13 @@
-using System.Numerics;
-
-namespace FractalTreeGtk.Draw
+namespace FractalTreeGtk.Draw
{
internal class Branch : CairoObjective.DrawObjects.Line
{
- public bool left;
- public double degree;
- public Branch(double x1, double y1, double x2, double y2, bool left, double degree) : base(x1, y1, x2, y2)
+ public readonly bool IsLeft;
+ public readonly double Degree;
+ public Branch(double x1, double y1, double x2, double y2, bool isLeft, double degree) : base(x1, y1, x2, y2)
{
- this.left = left;
- this.degree = degree;
+ IsLeft = isLeft;
+ Degree = degree;
}
}
}
diff --git a/FractalTreeGtk/Draw/Fractal.cs b/FractalTreeGtk/Draw/Fractal.cs
index 87cde8a..a0f003f 100644
--- a/FractalTreeGtk/Draw/Fractal.cs
+++ b/FractalTreeGtk/Draw/Fractal.cs
@@ -1,125 +1,100 @@
using System;
-using System.ComponentModel.DataAnnotations;
-using System.Threading;
-using System.Threading.Tasks;
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, CairoWindow window)
+ private readonly CairoWindow _window;
+ private double Length { get; set; } = new Random().Next(70, 90);
+ private int MaxLevels { get; set; }
+ private int CurrentLevel { get; set; } = 0;
+ public bool IsDrawing { private set; get; }
+ private double Degree => new Random().Next(30, 50) / 100f;
+ private double DeltaLength => new Random().Next(0, 50) / 100f;
+
+ public Branch[][] FractalLines { get; }
+
+ public Fractal(int maxLevels, CairoWindow window)
{
- this.window = window;
- this.levels = levels;
- Fractallines = new Branch[levels + 1][];
+ _window = window;
+ MaxLevels = maxLevels;
+ FractalLines = new Branch[maxLevels + 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[CurrentLevel] = new Branch[Convert.ToInt32(Math.Pow(2, CurrentLevel))];
+ for (int i = 0; i < FractalLines[CurrentLevel].Length; i++)
+ {
+ FractalLines[CurrentLevel][i] = new Branch(0, 0, 0, -Length, true, 0);
+ }
+
+ CurrentLevel++;
+ NextLevel();
}
+
private void NextLevel()
{
- window.QueueDraw();
- Length -= deltaLength;
- window.QueueDraw();
- Fractallines[level] = new Branch[Convert.ToInt32(Math.Pow(2, level))];
+ _window.QueueDraw();
+ var len = DeltaLength;
+ if (Length < DeltaLength)
+ Length = DeltaLength;
+ else
+ Length -= DeltaLength;
+ _window.QueueDraw();
+ FractalLines[CurrentLevel] = new Branch[Convert.ToInt32(Math.Pow(2, CurrentLevel))];
int x = 0;
- for (int i = 0; i < Fractallines[level - 1].Length; i++)
+ for (int i = 0; i < FractalLines[CurrentLevel - 1].Length; i++)
{
- double Degree = Fractallines[level - 1][i].degree;
- if (Fractallines[level - 1][i].left)
+ double degree = FractalLines[CurrentLevel - 1][i].Degree;
+ if (FractalLines[CurrentLevel - 1][i].IsLeft)
{
- double leftdegree = MainDegree + Degree;
- double rightdegree = -MainDegree + Degree;
- Fractallines[level][x] = new Branch(
- Fractallines[level - 1][i].X2,
- Fractallines[level - 1][i].Y2,
- -(Math.Sin(leftdegree) * Length) + Fractallines[level - 1][i].X2,
- -(Math.Cos(leftdegree) * Length) + Fractallines[level - 1][i].Y2,
+ double leftdegree = Degree + degree;
+ double rightdegree = -Degree + degree;
+ FractalLines[CurrentLevel][x] = new Branch(
+ FractalLines[CurrentLevel - 1][i].X2,
+ FractalLines[CurrentLevel - 1][i].Y2,
+ -(Math.Sin(leftdegree) * Length) + FractalLines[CurrentLevel - 1][i].X2,
+ -(Math.Cos(leftdegree) * Length) + FractalLines[CurrentLevel - 1][i].Y2,
true,
leftdegree);
- Fractallines[level][++x] = new Branch(
- Fractallines[level - 1][i].X2,
- Fractallines[level - 1][i].Y2,
- -(Math.Sin(rightdegree) * Length) + Fractallines[level - 1][i].X2,
- -(Math.Cos(rightdegree) * Length) + Fractallines[level - 1][i].Y2,
+ FractalLines[CurrentLevel][++x] = new Branch(
+ FractalLines[CurrentLevel - 1][i].X2,
+ FractalLines[CurrentLevel - 1][i].Y2,
+ -(Math.Sin(rightdegree) * Length) + FractalLines[CurrentLevel - 1][i].X2,
+ -(Math.Cos(rightdegree) * Length) + FractalLines[CurrentLevel - 1][i].Y2,
false,
rightdegree);
}
else
{
- double leftdegree = -(MainDegree - Degree);
- double rightdegree = -(-MainDegree - Degree);
- Fractallines[level][x] = new Branch(
- Fractallines[level - 1][i].X2,
- Fractallines[level - 1][i].Y2,
- -(Math.Sin(leftdegree) * Length) + Fractallines[level - 1][i].X2,
- -(Math.Cos(leftdegree) * Length) + Fractallines[level - 1][i].Y2,
+ double leftdegree = -(Degree - degree);
+ double rightdegree = -(-Degree - degree);
+ FractalLines[CurrentLevel][x] = new Branch(
+ FractalLines[CurrentLevel - 1][i].X2,
+ FractalLines[CurrentLevel - 1][i].Y2,
+ -(Math.Sin(leftdegree) * Length) + FractalLines[CurrentLevel - 1][i].X2,
+ -(Math.Cos(leftdegree) * Length) + FractalLines[CurrentLevel - 1][i].Y2,
true,
leftdegree);
- Fractallines[level][++x] = new Branch(
- Fractallines[level - 1][i].X2, Fractallines[level - 1][i].Y2,
-
- -(Math.Sin(rightdegree) * Length) + Fractallines[level - 1][i].X2,
- -(Math.Cos(rightdegree) * Length) + Fractallines[level - 1][i].Y2,
- false,
- rightdegree);
+ FractalLines[CurrentLevel][++x] = new Branch(
+ FractalLines[CurrentLevel - 1][i].X2, FractalLines[CurrentLevel - 1][i].Y2,
+ -(Math.Sin(rightdegree) * Length) + FractalLines[CurrentLevel - 1][i].X2,
+ -(Math.Cos(rightdegree) * Length) + FractalLines[CurrentLevel - 1][i].Y2,
+ false,
+ rightdegree);
}
+
x++;
}
- level++;
- if (level <= levels)
+
+ CurrentLevel++;
+ if (CurrentLevel <= MaxLevels)
{
NextLevel();
}
- else Drawing = false;
+ else IsDrawing = false;
}
- //private int SumOfBranches()
- //{
- // double sum = 0;
- // for (int i = 0; i <= levels; i++)
- // {
- // sum += Math.Pow(2, i);
- // }
- // return (int)sum;
- //}
-
- //private void Branch()
- //{
- // for(int i = 0; i < Convert.ToInt32(Math.Pow(2, level)); i+=2)
- // {
- // int z = i;
- // if(i == 0)
- // {
- // z = 1;
- // }
- // Lines[iter + i] = new Branch(
- // Lines[iter - z].X2, Lines[iter - z].Y2,
- // -(Math.Sin(deg) * Length) + Lines[iter - z].X2, -(Math.Cos(deg) * Length) + Lines[iter - z].Y2
- // );
- // Lines[iter + i + 1] = new Branch(
- // Lines[iter - z].X2, Lines[iter - z].Y2,
- // -(Math.Sin(-deg) * Length) + Lines[iter - z].X2, -(Math.Cos(-deg) * Length) + Lines[iter - z].Y2
- // );
- // deg += 0.05;
- // }
- // iter += Convert.ToInt32(Math.Pow(2, level));
- // level++;
- //}
}
-}
+}
\ No newline at end of file