Advertisement
peachyontop

Fractal Tree

May 28th, 2025
745
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.23 KB | None | 0 0
  1. using System;
  2. using System.Drawing;
  3. using System.Windows.Forms;
  4.  
  5. public class FractalTreeForm : Form
  6. {
  7.     private Bitmap canvas;
  8.  
  9.     public FractalTreeForm()
  10.     {
  11.         this.Width = 800;
  12.         this.Height = 600;
  13.         canvas = new Bitmap(this.Width, this.Height);
  14.         this.Paint += new PaintEventHandler(DrawTree);
  15.     }
  16.  
  17.     private void DrawTree(object sender, PaintEventArgs e)
  18.     {
  19.         using (Graphics g = Graphics.FromImage(canvas))
  20.         {
  21.             g.Clear(Color.White);
  22.             DrawBranch(g, this.Width / 2, this.Height - 50, -90, 100, 10);
  23.         }
  24.         e.Graphics.DrawImage(canvas, 0, 0);
  25.     }
  26.  
  27.     private void DrawBranch(Graphics g, float x, float y, float angle, float length, int depth)
  28.     {
  29.         if (depth == 0) return;
  30.  
  31.         float x2 = x + (float)(length * Math.Cos(angle * Math.PI / 180));
  32.         float y2 = y + (float)(length * Math.Sin(angle * Math.PI / 180));
  33.  
  34.         g.DrawLine(Pens.Black, x, y, x2, y2);
  35.  
  36.         DrawBranch(g, x2, y2, angle - 30, length * 0.7f, depth - 1);
  37.         DrawBranch(g, x2, y2, angle + 30, length * 0.7f, depth - 1);
  38.     }
  39.  
  40.     [STAThread]
  41.     static void Main()
  42.     {
  43.         Application.Run(new FractalTreeForm());
  44.     }
  45. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement