120 lines
4.5 KiB
C#
Raw Normal View History

2023-01-03 00:17:18 +03:00
using System;
2023-01-03 18:32:30 +03:00
using System.ComponentModel.DataAnnotations;
2023-01-03 00:17:18 +03:00
using System.Threading;
using System.Threading.Tasks;
namespace FractalTreeGtk.Draw
{
internal class Fractal
{
2023-01-03 18:32:30 +03:00
double Length = 100;
double MainDegree = 0.4;
2023-01-03 00:17:18 +03:00
int levels;
int level = 0;
2023-01-03 18:32:30 +03:00
double deltaLength = 5;
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 18:32:30 +03:00
Fractallines[level][i] = new Branch(0, 0, 0, -Length, true, 0);
}
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 18:32:30 +03:00
double Degree = Fractallines[level - 1][i].degree;
2023-01-03 00:17:18 +03:00
if (Fractallines[level - 1][i].left)
{
2023-01-03 18:32:30 +03:00
double leftdegree = MainDegree + Degree;
double rightdegree = -MainDegree + Degree;
2023-01-03 00:17:18 +03:00
Fractallines[level][x] = new Branch(
2023-01-03 18:32:30 +03:00
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);
2023-01-03 00:17:18 +03:00
Fractallines[level][++x] = new Branch(
2023-01-03 18:32:30 +03:00
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);
2023-01-03 00:17:18 +03:00
}
else
{
2023-01-03 18:32:30 +03:00
double leftdegree = -(MainDegree - Degree);
double rightdegree = -(-MainDegree - Degree);
2023-01-03 00:17:18 +03:00
Fractallines[level][x] = new Branch(
2023-01-03 18:32:30 +03:00
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);
2023-01-03 00:17:18 +03:00
Fractallines[level][++x] = new Branch(
Fractallines[level - 1][i].X2, Fractallines[level - 1][i].Y2,
2023-01-03 18:32:30 +03:00
-(Math.Sin(rightdegree) * Length) + Fractallines[level - 1][i].X2,
-(Math.Cos(rightdegree) * Length) + Fractallines[level - 1][i].Y2,
false,
rightdegree);
2023-01-03 00:17:18 +03:00
}
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++;
//}
}
}