MARCIN.com
Marcin Jamro, PhD, DSc
How to draw fractal?
C# | .NET | Algorithms
Let's paint some beautiful fractals using recursion! Thus, you can generate interesting patterns, such as the one shown below:
The main code is as follows:
Listlines = []; AddLine(14, 0, 0, 500, (float)Math.PI * 1.5f); DrawAndSave();
The AddLine
method adds a new line that forms a fractal:
void AddLine(int level, float x, float y, float length, float angle) { if (level < 0) { return; } float endX = x + (float)(length * Math.Cos(angle)); float endY = y + (float)(length * Math.Sin(angle)); lines.Add(new(x, y, endX, endY)); AddLine(level - 1, endX, endY, length * 0.8f, angle + (float)Math.PI * 0.3f); AddLine(level - 1, endX, endY, length * 0.6f, angle + (float)Math.PI * 1.7f); }
Drawing the collection of lines, as well as saving the image, is performed in the Save
method:
void DrawAndSave() { const int maxSize = 1000; float xMin = lines.Min(l => Math.Min(l.X1, l.X2)); float xMax = lines.Max(l => Math.Max(l.X1, l.X2)); float yMin = lines.Min(l => Math.Min(l.Y1, l.Y2)); float yMax = lines.Max(l => Math.Max(l.Y1, l.Y2)); float size = Math.Max(xMax - xMin, yMax - yMin); float factor = maxSize / size; int width = (int)((xMax - xMin) * factor); int height = (int)((yMax - yMin) * factor); using Bitmap bitmap = new(width, height); using Graphics graphics = Graphics.FromImage(bitmap); graphics.Clear(Color.White); graphics.SmoothingMode = SmoothingMode.AntiAlias; using Pen pen = new(Color.Black, 1); foreach (Line line in lines) { pen.Width = line.GetDistance() / 20; float sx = (line.X1 - xMin) * factor; float sy = (line.Y1 - yMin) * factor; float ex = (line.X2 - xMin) * factor; float ey = (line.Y2 - yMin) * factor; graphics.DrawLine(pen, sx, sy, ex, ey); } bitmap.Save($"{DateTime.Now:HH-mm-ss}.png"); }
The code above uses the Line
record, which is defined as follows:
record Line(float X1, float Y1, float X2, float Y2) { public float GetDistance() => (float)Math.Sqrt(Math.Pow(X1 - X2, 2) + Math.Pow(Y1 - Y2, 2)); }
Do not forget to install System.Drawing.Common
package from NuGet, as well as to add namespaces for System.Drawing
and System.Drawing.Drawing2D
.
This entry can contain content, including parts of code, from one of my books, namely from the Second Edition of C# Data Structures and Algorithms, published by Packt Publishing in 2024. If you want to get a bit more detailed description of generating fractals, as well as about many other algorithms, I encourage you to take a look at the mentioned book. You can buy it here.
The content, including any part of code, is presented without warranty, either express or implied. The author cannot be held liable for any damages caused or alleged to have been caused directly or indirectly by any content shown on this website.
Hello, I am Marcin
Reliable entrepreneur with 10+ years of companies operation, such as CEO at a few IT companies. I was an author of a few software & hardware products, still open to new ideas & cooperation.
Helpful expert with 10+ years of experience, together with PhD and DSc in Computer Science. I was an author of books and publications, as well as an expert in international projects.
Experienced developer with 10+ years of development and 100+ completed projects. I worked on various complex international projects, e.g. at Microsoft in USA and as CTO at a few companies. I have MCP, MCTS and MCPD certificates.
You can read more about me in my short bio. I am waiting for contact at [email protected], as well as at my Facebook and LinkedIn profiles.
Books
I am an author of a few books and numerous publications, also in high-quality international scientific journals.
If you want to learn data structures and algorithms in the context of C#, let's take a look at my newest book. It is the second edition of C# Data Structures and Algorithms. You can buy it here.
Projects
Do you like traveling? If so, discover an amazing world with local guides, right here and right now at: https://camaica.com
Travels
I love travels, learning new cultures and regions, meeting outstanding people, as well as taking pictures of beautiful places. Take a look at my travel diary.