WORKING FRACTAL!!!!!!!!!!!!

This commit is contained in:
Pavel-Savely Savianok 2023-01-03 18:32:30 +03:00
parent 1d2645531d
commit 17db4dc996
4 changed files with 60 additions and 24 deletions

View File

@ -4,7 +4,7 @@
public class Text
{
public static double DefaultFontSize = 20;//Default Font Size
public static bool CenterText = true;
public static void Make(string text, int pointX, int pointY, double size, Cairo.Color color)
{
Set.CheckContext();
@ -12,7 +12,10 @@
context.SetSourceColor(color);
context.SetFontSize(size);
Cairo.TextExtents extents = context.TextExtents(text);
if(CenterText)
context.MoveTo(pointX - extents.Width / 2, pointY + extents.Height / 2);
else
context.MoveTo(pointX, pointY);
context.ShowText(text);
}
public static void Make(string text, int pointX, int pointY, Cairo.Color color)
@ -22,7 +25,10 @@
context.SetSourceColor(color);
context.SetFontSize(DefaultFontSize);
Cairo.TextExtents extents = context.TextExtents(text);
context.MoveTo(pointX - extents.Width / 2, pointY + extents.Height / 2);
if (CenterText)
context.MoveTo(pointX - extents.Width / 2, pointY + extents.Height / 2);
else
context.MoveTo(pointX, pointY);
context.ShowText(text);
}
public static void Make(string text, int pointX, int pointY, double size)
@ -32,7 +38,10 @@
context.SetSourceColor(Set.Color);
context.SetFontSize(size);
Cairo.TextExtents extents = context.TextExtents(text);
context.MoveTo(pointX - extents.Width / 2, pointY + extents.Height / 2);
if (CenterText)
context.MoveTo(pointX - extents.Width / 2, pointY + extents.Height / 2);
else
context.MoveTo(pointX, pointY);
context.ShowText(text);
}
public static void Make(string text, int pointX, int pointY)
@ -42,7 +51,10 @@
context.SetSourceColor(Set.Color);
context.SetFontSize(DefaultFontSize);
Cairo.TextExtents extents = context.TextExtents(text);
context.MoveTo(pointX - extents.Width / 2, pointY + extents.Height / 2);
if (CenterText)
context.MoveTo(pointX - extents.Width / 2, pointY + extents.Height / 2);
else
context.MoveTo(pointX, pointY);
context.ShowText(text);
}
}

View File

@ -34,15 +34,20 @@ namespace FractalTreeGtk
private void Drawing_Drawn(object o, DrawnArgs args)
{
long branches = 0;
Text.CenterText = false;
args.Cr.Translate(AllocatedWidth / 2, 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 (CairoObjective.DrawObjects.Line[][])fractal.Fractallines)
{
foreach(CairoObjective.DrawObjects.Line line in lines)
foreach (CairoObjective.DrawObjects.Line line in lines) {
branches++;
Line.Make(line);
}
}
Text.Make($"Levels: {fractallevels}", -(AllocatedWidth / 2) + 50, -20, new Cairo.Color(1,1,1));
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));
}
}
}

View File

@ -5,9 +5,11 @@ 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)
public double degree;
public Branch(double x1, double y1, double x2, double y2, bool left, double degree) : base(x1, y1, x2, y2)
{
this.left = left;
this.degree = degree;
}
}
}

View File

@ -1,4 +1,5 @@
using System;
using System.ComponentModel.DataAnnotations;
using System.Threading;
using System.Threading.Tasks;
@ -6,11 +7,11 @@ namespace FractalTreeGtk.Draw
{
internal class Fractal
{
double Length = 50;
double deg = 0.3;
double Length = 100;
double MainDegree = 0.4;
int levels;
int level = 0;
double deltaLength = 2;
double deltaLength = 5;
public Branch[][] Fractallines;
public Fractal(int levels)
{
@ -23,7 +24,7 @@ namespace FractalTreeGtk.Draw
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);
Fractallines[level][i] = new Branch(0, 0, 0, -Length, true, 0);
}
level++;
NextLevel();
@ -31,32 +32,48 @@ namespace FractalTreeGtk.Draw
private void NextLevel()
{
Length -= deltaLength;
Fractallines[level] = new Branch[Convert.ToInt32(Math.Pow(2, level))];
int x = 0;
for (int i = 0; i < Fractallines[level - 1].Length; i++)
{
double Degree = Fractallines[level - 1][i].degree;
if (Fractallines[level - 1][i].left)
{
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(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 - 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,
true,
leftdegree);
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);
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);
}
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(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 - 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,
true,
leftdegree);
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);
-(Math.Sin(rightdegree) * Length) + Fractallines[level - 1][i].X2,
-(Math.Cos(rightdegree) * Length) + Fractallines[level - 1][i].Y2,
false,
rightdegree);
}
x++;
}