using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using System.Data; using System.Threading; namespace PS6_Q3 { // // Problem Set 6, Question 3 // Written by Paul Hudak // October 27, 2006 // public class Form1 : System.Windows.Forms.Form { // // Program to generate a fractal snowflake // private System.ComponentModel.Container components = null; // create a pallette (an array) of colors private Brush[] colors = { Brushes.Magenta, Brushes.Blue, Brushes.Green, Brushes.Yellow, Brushes.Red }; public Form1() { // Required for Windows Form Designer support InitializeComponent(); // set window size and label Size = new Size(1020,1100); Text = "Fractal Snowflake"; // looks better with black background BackColor = Color.Black; } protected override void OnPaint(PaintEventArgs e) { // get handle on graphics window Graphics g = e.Graphics; int m = 2*81; // a power of 3 for triangle size int x = 500; // x and y coordinates of int y = 500; // center of snowflake DrawTri(g, x, y, m, 0, false); // draw first triangle w/flat top Flake (g, x, y, m, 0, true); // begin recursion to complete job } public void DrawTri(Graphics g, int x, int y, int m, int c, bool o) { int d = (3*m)/2; if (o) { // horizontal side at bottom Point[] vertices = {new Point(x,y-3*m), new Point(x-3*m,y+d), new Point(x+3*m,y+d)}; g.FillPolygon(colors[c],vertices); } else { // horizontal side at top Point[] vertices = {new Point(x,y+3*m), new Point(x-3*m,y-d), new Point(x+3*m,y-d)}; g.FillPolygon(colors[c],vertices); } // Thread.Sleep(20); /* The above vertices are based on the following "base case": (3,0) (0,1.5) (6,1.5) (3,3) (0,4.5) (6,4.5) (3,6) */ } // draw snowflake at center (x,y), size m, and orientation o public void Flake(Graphics g, int x, int y, int m, int c, bool o) { DrawTri(g, x, y, m, c, o); // draw second triangle int c1 = (c+1)%5; // compute next color index int m1 = m/3; // compute next size if (m1>0) // if too small, we're done { Flake(g, x-2*m, y-m, m1, c1, true); // NW flat bottom Flake(g, x+2*m, y-m, m1, c1, true); // NE flat bottom Flake(g, x, y+2*m, m1, c1, true); // S flat bottom Flake(g, x-2*m, y+m, m1, c1, false); // SW flat top Flake(g, x+2*m, y+m, m1, c1, false); // SE flat top Flake(g, x, y-2*m, m1, c1, false); // N flat top }; /* The above coordinates are based on the following "base case", where each point is the center of a cluster: (3,1) (1,2) (5,2) (3,3) (1,4) (5,4) (3,5) */ } // Clean up any resources being used. protected override void Dispose( bool disposing ) { if( disposing ) { if (components != null) { components.Dispose(); } } base.Dispose( disposing ); } #region Windows Form Designer generated code /// /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// private void InitializeComponent() { this.components = new System.ComponentModel.Container(); this.Size = new System.Drawing.Size(300,300); this.Text = "Form1"; } #endregion // The main entry point for the application. [STAThread] static void Main() { Application.Run(new Form1()); } } }