103 lines
3.9 KiB
C#
Raw Normal View History

2023-01-03 00:17:18 +03:00
using System;
using System.Threading;
using System.Threading.Tasks;
namespace FractalTreeGtk.Draw
{
internal class Fractal
{
double Length = 50;
2023-01-03 00:17:18 +03:00
double deg = 0.3;
int levels;
int level = 0;
double deltaLength = 2;
2023-01-03 00:17:18 +03:00
public Branch[][] Fractallines;
public Fractal(int levels)
{
2023-01-03 00:17:18 +03:00
this.levels = levels;
Fractallines = new Branch[levels + 1][];
CreateBranch();
}
private void CreateBranch()
{
2023-01-03 00:17:18 +03:00
Fractallines[level] = new Branch[Convert.ToInt32(Math.Pow(2, level))];
for (int i = 0; i < Fractallines[level].Length; i++)
{
2023-01-03 00:17:18 +03:00
Fractallines[level][i] = new Branch(0, 0, 0, -Length, true);
}
level++;
NextLevel();
}
private void NextLevel()
{
Length -= deltaLength;
2023-01-03 00:17:18 +03:00
Fractallines[level] = new Branch[Convert.ToInt32(Math.Pow(2, level))];
int x = 0;
for (int i = 0; i < Fractallines[level - 1].Length; i++)
{
2023-01-03 00:17:18 +03:00
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++;
}
level++;
2023-01-03 00:17:18 +03:00
if (level <= levels)
{
NextLevel();
}
}
//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;
// }
2023-01-03 00:17:18 +03:00
// 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
// );
2023-01-03 00:17:18 +03:00
// 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++;
//}
}
}