Skip to content

Instantly share code, notes, and snippets.

@KrabCode
Created March 8, 2017 15:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save KrabCode/3958f19fb3c15fe1a600ddcfde9bc9fd to your computer and use it in GitHub Desktop.
Save KrabCode/3958f19fb3c15fe1a600ddcfde9bc9fd to your computer and use it in GitHub Desktop.
Tree generator core
BRANCH DEFINITION
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Fractal
{
class Branch
{
public Point Origin { get; set; }
public Point End { get; set; }
public Pen Pen { get; set; }
public List<Branch> Children { get; set; }
public double Angle { get; set; }
private double _childDeviation { get; set; }
private int _childCount { get; set; }
private double _length { get; set; }
public Branch(Point origin, Point end, Pen pen, double childDeviation, int childCount)
{
Origin = origin;
End = end;
Pen = pen;
Angle = AngleMath.GetAngleInDegrees(Origin, End);
Children = new List<Branch>();
_childDeviation = childDeviation;
_length = AngleMath.GetDistance(Origin, End);
_childCount = childCount;
}
public void Populate()
{
double minAngle = Angle - _childDeviation;
double childAngleStep = _childDeviation * 2 / _childCount;
while (Children.Count <= _childCount)
{
double childEndAngle = minAngle + (childAngleStep * Children.Count);
Point childEndPoint = AngleMath.GetPointOnEdgeOfCircle(End.X, End.Y, _length, childEndAngle);
Branch child = new Branch(End, childEndPoint, Pen, _childDeviation, _childCount);
Children.Add(child);
}
}
}
}
LOGIC
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Fractal
{
public class Logic
{
public delegate EventHandler RedrawEvent(object sender, RedrawEventArgs e);
public event RedrawEvent RedrawImage;
private Bitmap _offscreen;
private bool busy;
public void Start(int width, int height, double childDeviation, int detail, int childCount, int penOpacity, int size)
{
if(!busy)
{
int levelOfDetail = 0;
int maxLevelOfDetail = detail;
busy = true;
_offscreen = new Bitmap(width, height);
Graphics g = Graphics.FromImage(_offscreen);
List<Branch> branchesToPopulate = new List<Branch>();
Pen blackPen = new Pen(new SolidBrush(Color.FromArgb(penOpacity,Color.Black)));
SolidBrush backgroundBrush = new SolidBrush(Color.White);
Branch root = new Branch(new Point(width / 2, height / 2 + size), new Point(width / 2, height / 2 - size), blackPen, childDeviation, childCount);
root.Populate();
g.FillRectangle(backgroundBrush, 0, 0, width, height);
g.DrawLine(blackPen, root.Origin, root.End);
foreach (Branch child in root.Children)
{
branchesToPopulate.Add(child);
}
while (levelOfDetail < maxLevelOfDetail)
{
int branchesToPopulateCount = branchesToPopulate.Count;
for (int i = 0; i < branchesToPopulateCount; i++)
{
if (branchesToPopulate[i].Children.Count == 0)
{
branchesToPopulate[i].Populate();
foreach (Branch babyBranch in branchesToPopulate[i].Children)
{
branchesToPopulate.Add(babyBranch);
}
}
}
levelOfDetail++;
}
foreach (Branch b in branchesToPopulate)
{
g.DrawLine(blackPen, b.Origin, b.End);
}
RedrawImage(this, new RedrawEventArgs(_offscreen));
levelOfDetail = 0;
busy = false;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment