■ 타원을 임의 선으로 채우는 방법을 보여준다.
▶ 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 |
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 const int MARGIN = 10; /// <summary> /// 중심 X /// </summary> private int centerX; /// <summary> /// 중심 Y /// </summary> private int centerY; /// <summary> /// 너비 /// </summary> private int width; /// <summary> /// 높이 /// </summary> private int height; /// <summary> /// 포인트 리스트 /// </summary> private List<PointF> pointList = null; #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - MainForm() /// <summary> /// 생성자 /// </summary> public MainForm() { InitializeComponent(); ResizeRedraw = true; #region 이벤트를 설정한다. Load += Form_Load; Resize += Form_Resize; Paint += Form_Paint; #endregion } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Private //////////////////////////////////////////////////////////////////////////////// Event #region 폼 로드시 처리하기 - Form_Load(sender, e) /// <summary> /// 폼 로드시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void Form_Load(object sender, EventArgs e) { SetDrawingObject(); } #endregion #region 폼 크기 조정시 처리하기 - Form_Resize(sender, e) /// <summary> /// 폼 크기 조정시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void Form_Resize(object sender, EventArgs e) { SetDrawingObject(); } #endregion #region 폼 페인트시 처리하기 - Form_Paint(sender, e) /// <summary> /// 폼 페인트시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void Form_Paint(object sender, PaintEventArgs e) { if((this.width <= 0) || (this.height <= 0)) { return; } e.Graphics.SmoothingMode = SmoothingMode.AntiAlias; e.Graphics.FillEllipse(Brushes.LightBlue, MARGIN, MARGIN, this.width, this.height); e.Graphics.DrawEllipse(Pens.Blue, MARGIN, MARGIN, this.width, this.height); e.Graphics.DrawLines(Pens.Blue, this.pointList.ToArray()); } #endregion //////////////////////////////////////////////////////////////////////////////// Function #region 그리기 객체 설정하기 - SetDrawingObject() /// <summary> /// 그리기 객체 설정하기 /// </summary> private void SetDrawingObject() { this.width = ClientSize.Width - 2 * MARGIN; this.height = ClientSize.Height - 2 * MARGIN; this.centerX = ClientSize.Width / 2; this.centerY = ClientSize.Height / 2; Random random = new Random(); double circumference = 2 * Math.PI * Math.Sqrt((this.width * this.width + this.height * this.height) / 2); int pointCount = (int)(circumference / 40); this.pointList = new List<PointF>(); for(int i = 0; i < pointCount; i++) { double theta1 = 2 * Math.PI * random.NextDouble(); float x1 = (float)(this.centerX + Math.Cos(theta1) * this.width / 2); float y1 = (float)(this.centerY + Math.Sin(theta1) * this.height / 2); this.pointList.Add(new PointF(x1, y1)); double theta2 = 2 * Math.PI * random.NextDouble(); float x2 = (float)(this.centerX + Math.Cos(theta2) * this.width / 2); float y2 = (float)(this.centerY + Math.Sin(theta2) * this.height / 2); this.pointList.Add(new PointF(x2, y2)); } } #endregion } } |