diff --git a/FractalTreeGtk/CairoWindow.cs b/FractalTreeGtk/CairoWindow.cs index 7fada7b..b039ac5 100644 --- a/FractalTreeGtk/CairoWindow.cs +++ b/FractalTreeGtk/CairoWindow.cs @@ -7,13 +7,29 @@ namespace FractalTreeGtk { internal class CairoWindow : Window { + static int fractallevels = 2; DrawingArea drawing = new DrawingArea(); - Fractal fractal = new Fractal(); + Fractal fractal = new Fractal(fractallevels); public CairoWindow(string title) : base(title) { Fullscreen(); drawing.Drawn += Drawing_Drawn; Add(drawing); ShowAll(); + KeyPressEvent += CairoWindow_KeyPressEvent; + } + + private void CairoWindow_KeyPressEvent(object o, KeyPressEventArgs args) + { + if(args.Event.Key == Gdk.Key.equal) + { + fractal = new Fractal(++fractallevels); + QueueDraw(); + } + if (args.Event.Key == Gdk.Key.minus && fractallevels > 1) + { + fractal = new Fractal(--fractallevels); + QueueDraw(); + } } private void Drawing_Drawn(object o, DrawnArgs args) @@ -21,11 +37,12 @@ namespace FractalTreeGtk args.Cr.Translate(AllocatedWidth / 2, AllocatedHeight); Set.Context = args.Cr; Set.Background(new Cairo.Color(0, 0, 0)); - foreach(CairoObjective.DrawObjects.Line[] lines in fractal.Fractallines) + foreach(CairoObjective.DrawObjects.Line[] lines in (CairoObjective.DrawObjects.Line[][])fractal.Fractallines) { foreach(CairoObjective.DrawObjects.Line line in lines) Line.Make(line); } + Text.Make($"Levels: {fractallevels}", -(AllocatedWidth / 2) + 50, -20, new Cairo.Color(1,1,1)); } } } diff --git a/FractalTreeGtk/Draw/Branch.cs b/FractalTreeGtk/Draw/Branch.cs new file mode 100644 index 0000000..8c18621 --- /dev/null +++ b/FractalTreeGtk/Draw/Branch.cs @@ -0,0 +1,13 @@ +using System.Numerics; + +namespace FractalTreeGtk.Draw +{ + internal class Branch : CairoObjective.DrawObjects.Line + { + public bool left; + public Branch(double x1, double y1, double x2, double y2, bool left) : base(x1, y1, x2, y2) + { + this.left = left; + } + } +} diff --git a/FractalTreeGtk/Draw/Fractal.cs b/FractalTreeGtk/Draw/Fractal.cs index ac4a2d8..7eeff9d 100644 --- a/FractalTreeGtk/Draw/Fractal.cs +++ b/FractalTreeGtk/Draw/Fractal.cs @@ -1,27 +1,29 @@ -using CairoObjective.DrawObjects; -using System; +using System; +using System.Threading; +using System.Threading.Tasks; + namespace FractalTreeGtk.Draw { internal class Fractal { double Length = 50; - double deg = 0.2; - int levels = 14; + double deg = 0.3; + int levels; int level = 0; double deltaLength = 2; - double deltaDeg = 0; - public Line[][] Fractallines; - public Fractal() + public Branch[][] Fractallines; + public Fractal(int levels) { - Fractallines = new Line[levels + 1][]; - CreateBranch(); + this.levels = levels; + Fractallines = new Branch[levels + 1][]; + CreateBranch(); } private void CreateBranch() { - Fractallines[level] = new Line[Convert.ToInt32(Math.Pow(2, level))]; + Fractallines[level] = new Branch[Convert.ToInt32(Math.Pow(2, level))]; for (int i = 0; i < Fractallines[level].Length; i++) { - Fractallines[level][i] = new Line(0, 0, 0, -Length); + Fractallines[level][i] = new Branch(0, 0, 0, -Length, true); } level++; NextLevel(); @@ -29,24 +31,37 @@ namespace FractalTreeGtk.Draw private void NextLevel() { Length -= deltaLength; - //deg += Math.Sin(deg) + deltadeg; - Fractallines[level] = new Line[Convert.ToInt32(Math.Pow(2, level))]; + + Fractallines[level] = new Branch[Convert.ToInt32(Math.Pow(2, level))]; int x = 0; for (int i = 0; i < Fractallines[level - 1].Length; i++) { - Fractallines[level][x] = new Line( - Fractallines[level - 1][i].X2, Fractallines[level - 1][i].Y2, - -(Math.Sin(deg) * Length) + Fractallines[level - 1][i].X2, -(Math.Cos(deg) * Length) + Fractallines[level - 1][i].Y2 - ); - Fractallines[level][++x] = new Line( - Fractallines[level - 1][i].X2, Fractallines[level - 1][i].Y2, - -(Math.Sin(-deg) * Length) + Fractallines[level - 1][i].X2, -(Math.Cos(-deg) * Length) + Fractallines[level - 1][i].Y2 - ); + if (Fractallines[level - 1][i].left) + { + Fractallines[level][x] = new Branch( + Fractallines[level - 1][i].X2, Fractallines[level - 1][i].Y2, + -(Math.Sin(deg + deg * (level-1)) * Length) + Fractallines[level - 1][i].X2, -(Math.Cos(deg + deg * (level - 1)) * Length) + Fractallines[level - 1][i].Y2 + , true); + Fractallines[level][++x] = new Branch( + Fractallines[level - 1][i].X2, Fractallines[level - 1][i].Y2, + -(Math.Sin(-deg + deg * (level-1)) * Length) + Fractallines[level - 1][i].X2, -(Math.Cos(-deg + deg * (level - 1)) * Length) + Fractallines[level - 1][i].Y2 + , false); + } + else + { + Fractallines[level][x] = new Branch( + Fractallines[level - 1][i].X2, Fractallines[level - 1][i].Y2, + -(Math.Sin(deg - deg * (level - 1)) * Length) + Fractallines[level - 1][i].X2, -(Math.Cos(deg - deg * (level - 1)) * Length) + Fractallines[level - 1][i].Y2 + , true); + Fractallines[level][++x] = new Branch( + Fractallines[level - 1][i].X2, Fractallines[level - 1][i].Y2, + -(Math.Sin(-deg - deg * (level - 1)) * Length) + Fractallines[level - 1][i].X2, -(Math.Cos(-deg - deg * (level - 1)) * Length) + Fractallines[level - 1][i].Y2 + , false); + } x++; } - deltaDeg += deg + 0.1; level++; - if(level <= levels) + if (level <= levels) { NextLevel(); } @@ -70,11 +85,11 @@ namespace FractalTreeGtk.Draw // { // z = 1; // } - // Lines[iter + i] = new Line( + // 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 Line( + // 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 // );