■ 측면 및 종횡비로 정의된 사각형을 찾는 방법을 보여준다.
▶ 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 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 |
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> /// 포인트 1 /// </summary> private PointF point1; /// <summary> /// 포인트 2 /// </summary> private PointF point2; /// <summary> /// 포인트 리스트 /// </summary> private List<PointF[]> pointList = new List<PointF[]>(); private Brush[] brushArray = { new SolidBrush(Color.FromArgb(128, Color.Red )), new SolidBrush(Color.FromArgb(128, Color.Red )), new SolidBrush(Color.FromArgb(128, Color.Blue)), new SolidBrush(Color.FromArgb(128, Color.Blue)) }; /// <summary> /// 펜 배열 /// </summary> private Pen[] penArray = { Pens.Red, Pens.Red, Pens.Blue, Pens.Blue }; /// <summary> /// 그리기 여부 /// </summary> private bool isDrawing = false; #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - MainForm() /// <summary> /// 생성자 /// </summary> public MainForm() { InitializeComponent(); this.aspectRatioTextBox.TextChanged += aspectRatioTextBox_TextChanged; this.pictureBox.Paint += pictureBox_Paint; this.pictureBox.MouseDown += pictureBox_MouseDown; this.pictureBox.MouseMove += pictureBox_MouseMove; this.pictureBox.MouseUp += pictureBox_MouseUp; } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Private //////////////////////////////////////////////////////////////////////////////// Event #region 종횡비 텍스트 박스 텍스트 변경시 처리하기 - aspectRatioTextBox_TextChanged(sender, e) /// <summary> /// 종횡비 텍스트 박스 텍스트 변경시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void aspectRatioTextBox_TextChanged(object sender, EventArgs e) { this.point1 = new PointF(); this.point2 = new PointF(); this.pointList = new List<PointF[]>(); this.pictureBox.Refresh(); } #endregion #region 픽처 박스 마우스 DOWN 처리하기 - pictureBox_MouseDown(sender, e) /// <summary> /// 픽처 박스 마우스 DOWN 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void pictureBox_MouseDown(object sender, MouseEventArgs e) { this.point1 = e.Location; this.point2 = e.Location; this.pointList = new List<PointF[]>(); this.pictureBox.Refresh(); this.isDrawing = true; } #endregion #region 픽처 박스 마우스 이동시 처리하기 - pictureBox_MouseMove(sender, e) /// <summary> /// 픽처 박스 마우스 이동시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void pictureBox_MouseMove(object sender, MouseEventArgs e) { if(!this.isDrawing) { return; } this.point2 = e.Location; this.pictureBox.Refresh(); } #endregion #region 픽처 박스 마우스 UP 처리하기 - pictureBox_MouseUp(sender, e) /// <summary> /// 픽처 박스 마우스 UP 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void pictureBox_MouseUp(object sender, MouseEventArgs e) { if(!this.isDrawing) { return; } this.isDrawing = false; float aspectRatio = GetAspectRatio(this.aspectRatioTextBox.Text); this.pointList = FindRectangle(aspectRatio, this.point1, this.point2); this.pictureBox.Refresh(); } #endregion #region 픽처 박스 페인트시 처리하기 - pictureBox_Paint(sender, e) /// <summary> /// 픽처 박스 페인트시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void pictureBox_Paint(object sender, PaintEventArgs e) { e.Graphics.Clear(this.pictureBox.BackColor); e.Graphics.SmoothingMode = SmoothingMode.AntiAlias; for(int i = 0; i < this.pointList.Count; i++) { e.Graphics.FillPolygon(this.brushArray[i], this.pointList[i]); e.Graphics.DrawPolygon(this.penArray[i], this.pointList[i]); } if(this.point1 != this.point2) { e.Graphics.DrawLine(Pens.Black, this.point1, this.point2); } } #endregion //////////////////////////////////////////////////////////////////////////////// Function #region 종횡비 구하기 - GetAspectRatio(text) /// <summary> /// 종횡비 구하기 /// </summary> /// <param name="text">텍스트</param> /// <returns>종횡비</returns> private float GetAspectRatio(string text) { try { if(text.Contains(":")) { float width = float.Parse(text.Split(':')[0]); float height = float.Parse(text.Split(':')[1]); return width / height; } else { return float.Parse(text); } } catch { return 1; } } #endregion #region 사각형 찾기 - FindRectangle(aspectRatio, point1, point2) /// <summary> /// 사각형 찾기 /// </summary> /// <param name="aspectRatio">종횡비</param> /// <param name="point1">포인트 1</param> /// <param name="point2">포인트 2</param> /// <returns>사각형</returns> private List<PointF[]> FindRectangle(float aspectRatio, PointF point1, PointF point2) { float vectorX = point2.X - point1.X; float vectorY = point2.Y - point1.Y; if(Math.Sqrt(vectorX * vectorX + vectorY * vectorY) < 0.1) { return null; } PointF point3; PointF point4; List<PointF[]> pointList = new List<PointF[]>(); float perpendicularVectorX; float perpendicularVectorY; perpendicularVectorX = vectorY * aspectRatio; perpendicularVectorY = -vectorX * aspectRatio; point3 = new PointF(point2.X + perpendicularVectorX, point2.Y + perpendicularVectorY); point4 = new PointF(point1.X + perpendicularVectorX, point1.Y + perpendicularVectorY); pointList.Add(new PointF[] { point1, point2, point3, point4 }); point3 = new PointF(point2.X - perpendicularVectorX, point2.Y - perpendicularVectorY); point4 = new PointF(point1.X - perpendicularVectorX, point1.Y - perpendicularVectorY); pointList.Add(new PointF[] { point1, point2, point3, point4 }); perpendicularVectorX = vectorY / aspectRatio; perpendicularVectorY = -vectorX / aspectRatio; point3 = new PointF(point2.X + perpendicularVectorX, point2.Y + perpendicularVectorY); point4 = new PointF(point1.X + perpendicularVectorX, point1.Y + perpendicularVectorY); pointList.Add(new PointF[] { point1, point2, point3, point4 }); point3 = new PointF(point2.X - perpendicularVectorX, point2.Y - perpendicularVectorY); point4 = new PointF(point1.X - perpendicularVectorX, point1.Y - perpendicularVectorY); pointList.Add(new PointF[] { point1, point2, point3, point4 }); return pointList; } #endregion } } |