■ 쌍곡나사선(Hyperbolic Spiral)을 그리는 방법을 보여준다.
▶ 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 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 |
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 Pen[] penArray = { Pens.Red, Pens.Green, Pens.Purple, Pens.Blue, Pens.Magenta }; #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - MainForm() /// <summary> /// 생성자 /// </summary> public MainForm() { InitializeComponent(); #region 이벤트를 설정한다. this.drawButton.Click += drawButton_Click; this.canvasPictureBox.Resize += canvasPictureBox_Resize; this.canvasPictureBox.Paint += canvasPictureBox_Paint; #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) { this.canvasPictureBox.Refresh(); } #endregion #region 캔버스 픽처 박스 크기 조정시 처리하기 - canvasPictureBox_Resize(sender, e) /// <summary> /// 캔버스 픽처 박스 크기 조정시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void canvasPictureBox_Resize(object sender, EventArgs e) { this.canvasPictureBox.Refresh(); } #endregion #region 캔버스 픽처 박스 페인트시 처리하기 - canvasPictureBox_Paint(sender, e) /// <summary> /// 캔버스 픽처 박스 페인트시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void canvasPictureBox_Paint(object sender, PaintEventArgs e) { e.Graphics.Clear(canvasPictureBox.BackColor); e.Graphics.SmoothingMode = SmoothingMode.AntiAlias; try { float a = float.Parse(this.aTextBox.Text); int spiralCount = int.Parse(this.spiralCountTextBox.Text); float angleStep = (float)(2 * Math.PI / spiralCount); float start_angle = 0; PointF centerPoint = new PointF ( this.canvasPictureBox.ClientSize.Width / 2, this.canvasPictureBox.ClientSize.Height / 2 ); e.Graphics.DrawLine ( Pens.Black, centerPoint.X, 0, centerPoint.X, this.canvasPictureBox.ClientSize.Height ); e.Graphics.DrawLine ( Pens.Black, 0, centerPoint.Y, this.canvasPictureBox.ClientSize.Width, centerPoint.Y ); Rectangle rectangle = new Rectangle(25, 50, 150, 150); float maximumR = GetDistance(centerPoint, rectangle); for(int i = 0; i < spiralCount; i++) { List<PointF> pointList = GetSpiralPointList(0.1f, centerPoint, a, start_angle, maximumR); e.Graphics.DrawLines(penArray[i % penArray.Length], pointList.ToArray()); start_angle += angleStep; } e.Graphics.DrawRectangle(Pens.Black, rectangle); } catch(Exception exception) { MessageBox.Show(exception.Message); } } #endregion //////////////////////////////////////////////////////////////////////////////// Function #region 거리 구하기 - GetDistance(point1, point2) /// <summary> /// 거리 구하기 /// </summary> /// <param name="point1">포인트 1</param> /// <param name="point2">포인트 2</param> /// <returns>거리</returns> private float GetDistance(PointF point1, PointF point2) { float deltaX = point1.X - point2.X; float deltaY = point1.Y - point2.Y; return (float)Math.Sqrt(deltaX * deltaX + deltaY * deltaY); } #endregion #region 거리 구하기 - GetDistance(point, rectangle) /// <summary> /// 거리 구하기 /// </summary> /// <param name="point">포인트</param> /// <param name="rectangle">사각형</param> /// <returns>거리</returns> private float GetDistance(PointF point, Rectangle rectangle) { float maximumDistance = GetDistance(point, new PointF(rectangle.Left, rectangle.Top)); float testDistance = GetDistance(point, new PointF(rectangle.Left, rectangle.Bottom)); if(maximumDistance < testDistance) { maximumDistance = testDistance; } testDistance = GetDistance(point, new PointF(rectangle.Right, rectangle.Top)); if(maximumDistance < testDistance) { maximumDistance = testDistance; } testDistance = GetDistance(point, new PointF(rectangle.Right, rectangle.Bottom)); if(maximumDistance < testDistance) { maximumDistance = testDistance; } return maximumDistance; } #endregion #region 극 좌표계를 데카르트 좌표계로 변환하기 - ConvertPolarCoordinatesToCartesianCoordinates(r, theta, x, y) /// <summary> /// 극 좌표계를 데카르트 좌표계로 변환하기 /// </summary> /// <param name="r">R</param> /// <param name="theta">세타</param> /// <param name="x">X</param> /// <param name="y">Y</param> private void ConvertPolarCoordinatesToCartesianCoordinates(float r, float theta, out float x, out float y) { x = (float)(r * Math.Cos(theta)); y = (float)(r * Math.Sin(theta)); } #endregion #region 나선 포인트 리스트 구하기 - GetSpiralPointList(deltaTheta, centerPoint, a, angleOffset, maximumR) /// <summary> /// 나선 포인트 리스트 구하기 /// </summary> /// <param name="deltaTheta">델타 세타</param> /// <param name="centerPoint">중심 포인트</param> /// <param name="a">A</param> /// <param name="angleOffset">각도 오프셋</param> /// <param name="maximumR">최대 R</param> /// <returns>나선 포인트 리스트</returns> private List<PointF> GetSpiralPointList(float deltaTheta, PointF centerPoint, float a, float angleOffset, float maximumR) { float maximumTheta = a / 2f; List<PointF> pointList = new List<PointF>(); for(float theta = maximumTheta; ; theta -= deltaTheta) { float r = a / theta; float x; float y; ConvertPolarCoordinatesToCartesianCoordinates(r, theta + angleOffset, out x, out y); x += centerPoint.X; y += centerPoint.Y; pointList.Add(new PointF((float)x, (float)y)); if(r > maximumR) { break; } } return pointList; } #endregion } } |