1 step to working

This commit is contained in:
Pavel-Savely Savianok 2023-01-03 00:17:18 +03:00
parent b16a3b1adc
commit 1d2645531d
3 changed files with 72 additions and 27 deletions

View File

@ -7,13 +7,29 @@ namespace FractalTreeGtk
{ {
internal class CairoWindow : Window internal class CairoWindow : Window
{ {
static int fractallevels = 2;
DrawingArea drawing = new DrawingArea(); DrawingArea drawing = new DrawingArea();
Fractal fractal = new Fractal(); Fractal fractal = new Fractal(fractallevels);
public CairoWindow(string title) : base(title) { public CairoWindow(string title) : base(title) {
Fullscreen(); Fullscreen();
drawing.Drawn += Drawing_Drawn; drawing.Drawn += Drawing_Drawn;
Add(drawing); Add(drawing);
ShowAll(); 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) private void Drawing_Drawn(object o, DrawnArgs args)
@ -21,11 +37,12 @@ namespace FractalTreeGtk
args.Cr.Translate(AllocatedWidth / 2, AllocatedHeight); args.Cr.Translate(AllocatedWidth / 2, AllocatedHeight);
Set.Context = args.Cr; Set.Context = args.Cr;
Set.Background(new Cairo.Color(0, 0, 0)); 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) foreach(CairoObjective.DrawObjects.Line line in lines)
Line.Make(line); Line.Make(line);
} }
Text.Make($"Levels: {fractallevels}", -(AllocatedWidth / 2) + 50, -20, new Cairo.Color(1,1,1));
} }
} }
} }

View File

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

View File

@ -1,27 +1,29 @@
using CairoObjective.DrawObjects; using System;
using System; using System.Threading;
using System.Threading.Tasks;
namespace FractalTreeGtk.Draw namespace FractalTreeGtk.Draw
{ {
internal class Fractal internal class Fractal
{ {
double Length = 50; double Length = 50;
double deg = 0.2; double deg = 0.3;
int levels = 14; int levels;
int level = 0; int level = 0;
double deltaLength = 2; double deltaLength = 2;
double deltaDeg = 0; public Branch[][] Fractallines;
public Line[][] Fractallines; public Fractal(int levels)
public Fractal()
{ {
Fractallines = new Line[levels + 1][]; this.levels = levels;
CreateBranch(); Fractallines = new Branch[levels + 1][];
CreateBranch();
} }
private void 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++) 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++; level++;
NextLevel(); NextLevel();
@ -29,24 +31,37 @@ namespace FractalTreeGtk.Draw
private void NextLevel() private void NextLevel()
{ {
Length -= deltaLength; 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; int x = 0;
for (int i = 0; i < Fractallines[level - 1].Length; i++) for (int i = 0; i < Fractallines[level - 1].Length; i++)
{ {
Fractallines[level][x] = new Line( if (Fractallines[level - 1][i].left)
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 Branch(
); Fractallines[level - 1][i].X2, Fractallines[level - 1][i].Y2,
Fractallines[level][++x] = new Line( -(Math.Sin(deg + deg * (level-1)) * Length) + Fractallines[level - 1][i].X2, -(Math.Cos(deg + deg * (level - 1)) * Length) + Fractallines[level - 1][i].Y2
Fractallines[level - 1][i].X2, Fractallines[level - 1][i].Y2, , true);
-(Math.Sin(-deg) * Length) + Fractallines[level - 1][i].X2, -(Math.Cos(-deg) * Length) + Fractallines[level - 1][i].Y2 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++; x++;
} }
deltaDeg += deg + 0.1;
level++; level++;
if(level <= levels) if (level <= levels)
{ {
NextLevel(); NextLevel();
} }
@ -70,11 +85,11 @@ namespace FractalTreeGtk.Draw
// { // {
// z = 1; // z = 1;
// } // }
// Lines[iter + i] = new Line( // Lines[iter + i] = new Branch(
// Lines[iter - z].X2, Lines[iter - z].Y2, // Lines[iter - z].X2, Lines[iter - z].Y2,
// -(Math.Sin(deg) * Length) + Lines[iter - z].X2, -(Math.Cos(deg) * Length) + 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, // Lines[iter - z].X2, Lines[iter - z].Y2,
// -(Math.Sin(-deg) * Length) + Lines[iter - z].X2, -(Math.Cos(-deg) * Length) + Lines[iter - z].Y2 // -(Math.Sin(-deg) * Length) + Lines[iter - z].X2, -(Math.Cos(-deg) * Length) + Lines[iter - z].Y2
// ); // );