■ 코흐 눈송이 프랙탈(Von Koch Snow Flake Fractal)을 그리는 방법을 보여준다.
▶ MainForm.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 |
using System; using System.Collections.Generic; using System.Drawing; using System.Drawing.Drawing2D; using System.Windows.Forms; namespace TestProject { /// <summary> /// 메인 폼 /// </summary> public partial class MainForm : Form { //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Private #region Field /// <summary> /// 개시자 포인트 리스트 /// </summary> private List<PointF> initiatorPointList; /// <summary> /// 스케일 팩터 /// </summary> private float scaleFactor; /// <summary> /// 생성자 델타 세타 /// </summary> private List<float> generatorDeltaTheta; #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - MainForm() /// <summary> /// 생성자 /// </summary> public MainForm() { InitializeComponent(); #region 이벤트를 설정한다. this.drawButton.Click += drawButton_Click; #endregion } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Private //////////////////////////////////////////////////////////////////////////////// Event #region 그리기 버튼 클릭시 처리하기 - drawButton_Click(sender, e) /// <summary> /// 그리기 버튼 클릭시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void drawButton_Click(object sender, EventArgs e) { Cursor = Cursors.WaitCursor; Application.DoEvents(); this.initiatorPointList = new List<PointF>(); float height = Math.Min ( this.canvasPictureBox.Width, this.canvasPictureBox.Height ) - 100; float x1 = (this.canvasPictureBox.Width - height) / 2; float x2 = x1 + height; float y1 = (this.canvasPictureBox.Height - height) / 2; float y2 = y1 + height; this.initiatorPointList.Add(new PointF(x1, y1)); this.initiatorPointList.Add(new PointF(x2, y1)); this.initiatorPointList.Add(new PointF(x2, y2)); this.initiatorPointList.Add(new PointF(x1, y2)); this.initiatorPointList.Add(new PointF(x1, y1)); this.scaleFactor = (float)(1.0 / Math.Sqrt(5.0)); this.generatorDeltaTheta = new List<float>(); this.generatorDeltaTheta.Add((float)-Math.Atan(1.0 / 2.0)); float piOver2 = (float)(Math.PI / 2); this.generatorDeltaTheta.Add(piOver2); this.generatorDeltaTheta.Add(-piOver2); int depth = int.Parse(this.depthTextBox.Text); Bitmap bitmap = new Bitmap ( this.canvasPictureBox.Width, this.canvasPictureBox.Height ); this.canvasPictureBox.Image = bitmap; using(Graphics graphics = Graphics.FromImage(bitmap)) { graphics.SmoothingMode = SmoothingMode.AntiAlias; DrawSnowflakeFractal(graphics, depth); } Cursor = Cursors.Default; } #endregion //////////////////////////////////////////////////////////////////////////////// Function #region 눈송이 에지 그리기 - DrawSnowFlakeEdge(graphics, depth, point1, theta, distance) /// <summary> /// 눈송이 에지 그리기 /// </summary> /// <param name="graphics">그래픽스</param> /// <param name="depth">깊이</param> /// <param name="point1">포인트 1</param> /// <param name="theta">세타</param> /// <param name="distance">거리</param> private void DrawSnowFlakeEdge(Graphics graphics, int depth, ref PointF point1, float theta, float distance) { if(depth == 0) { PointF point2 = new PointF ( (float)(point1.X + distance * Math.Cos(theta)), (float)(point1.Y + distance * Math.Sin(theta)) ); graphics.DrawLine(Pens.Blue, point1, point2); point1 = point2; return; } distance *= this.scaleFactor; for(int i = 0; i < this.generatorDeltaTheta.Count; i++) { theta += this.generatorDeltaTheta[i]; DrawSnowFlakeEdge(graphics, depth - 1, ref point1, theta, distance); } } #endregion #region 눈송이 프랙탈 그리기 - DrawSnowflakeFractal(graphics, depth) /// <summary> /// 눈송이 프랙탈 그리기 /// </summary> /// <param name="graphics">그래픽스</param> /// <param name="depth">깊이</param> private void DrawSnowflakeFractal(Graphics graphics, int depth) { graphics.Clear(this.canvasPictureBox.BackColor); for(int i = 1; i < this.initiatorPointList.Count; i++) { PointF p1 = this.initiatorPointList[i - 1]; PointF p2 = this.initiatorPointList[i]; float dx = p2.X - p1.X; float dy = p2.Y - p1.Y; float length = (float)Math.Sqrt(dx * dx + dy * dy); float theta = (float)Math.Atan2(dy, dx); DrawSnowFlakeEdge(graphics, depth, ref p1, theta, length); } } #endregion } } |