■ 장미 곡선(Rose Curve)을 그리는 방법을 보여준다.
▶ 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 |
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 { //////////////////////////////////////////////////////////////////////////////////////////////////// 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) { float a = float.Parse(this.aTextBox.Text); int n = int.Parse(this.nTextBox.Text); int d = int.Parse(this.dTextBox.Text); Bitmap bitmap = new Bitmap ( this.canvasPictureBox.ClientSize.Width, this.canvasPictureBox.ClientSize.Height ); using(Graphics graphics = Graphics.FromImage(bitmap)) { graphics.Clear(Color.White); graphics.SmoothingMode = SmoothingMode.AntiAlias; RectangleF rectangle = new RectangleF ( -a - 0.1f, -a - 0.1f, 2 * a + 0.2f, 2 * a + 0.2f ); PointF[] pointArray = { new PointF(0, bitmap.Height), new PointF(bitmap.Width, bitmap.Height), new PointF(0, 0), }; graphics.Transform = new Matrix(rectangle, pointArray); DrawCurve(graphics, a, n, d); DrawAxes(graphics, rectangle.Right); this.canvasPictureBox.Image = bitmap; } } #endregion //////////////////////////////////////////////////////////////////////////////// Function #region 곡선 그리기 - DrawCurve(graphics, a, n, d) /// <summary> /// 곡선 그리기 /// </summary> /// <param name="graphics">그래픽스</param> /// <param name="a">A</param> /// <param name="n">N</param> /// <param name="d">D</param> private void DrawCurve(Graphics graphics, float a, int n, int d) { const int pointCount = 1000; double period = Math.PI * d; if((n % 2 == 0) || (d % 2 == 0)) { period *= 2; } double dtheta = period / pointCount; List<PointF> pointList = new List<PointF>(); double k = (double)n / d; for(int i = 0; i < pointCount; i++) { double theta = i * dtheta; double r = a * Math.Cos(k * theta); float x = (float)(r * Math.Cos(theta)); float y = (float)(r * Math.Sin(theta)); pointList.Add(new PointF(x, y)); } graphics.FillPolygon(Brushes.LightBlue, pointList.ToArray()); using(Pen pen = new Pen(Color.Red, 0)) { graphics.DrawLines(pen, pointList.ToArray()); } } #endregion #region 축 그리기 - DrawAxes(graphics, worldMaximumX) /// <summary> /// 축 그리기 /// </summary> /// <param name="graphics">그래픽스</param> /// <param name="worldMaximumX">월드 최대 X</param> private void DrawAxes(Graphics graphics, float worldMaximumX) { int maximumX = (int)worldMaximumX; using(Pen pen = new Pen(Color.Black, 0)) { graphics.DrawLine(pen, -worldMaximumX, 0 , worldMaximumX, 0 ); graphics.DrawLine(pen, 0 , -worldMaximumX, 0 , worldMaximumX); float tic = 0.1f; for(int x = -maximumX; x <= maximumX; x++) { graphics.DrawLine(pen, x , -tic, x , tic); graphics.DrawLine(pen, -tic, x , tic, x ); } } } #endregion } } |