728x90
반응형
728x170
▶ MainForm.cs
using System;
using System.Drawing;
using System.Windows.Forms;
namespace TestProject
{
/// <summary>
/// 메인 폼
/// </summary>
public partial class MainForm : Form
{
//////////////////////////////////////////////////////////////////////////////////////////////////// Field
////////////////////////////////////////////////////////////////////////////////////////// Private
#region Field
/// <summary>
/// 파란색 펜
/// </summary>
private Pen bluePen;
/// <summary>
/// 빨간색 펜
/// </summary>
private Pen redPen;
#endregion
//////////////////////////////////////////////////////////////////////////////////////////////////// Constructor
////////////////////////////////////////////////////////////////////////////////////////// Public
#region 생성자 - MainForm()
/// <summary>
/// 생성자
/// </summary>
public MainForm()
{
InitializeComponent();
this.bluePen = new Pen(Brushes.Blue);
this.redPen = new Pen(Brushes.Red );
SetStyle(ControlStyles.ResizeRedraw | ControlStyles.UserPaint | ControlStyles.AllPaintingInWmPaint, true);
UpdateStyles();
}
#endregion
//////////////////////////////////////////////////////////////////////////////////////////////////// Method
////////////////////////////////////////////////////////////////////////////////////////// Protected
#region 배경 페인트시 처리하기 - OnPaintBackground(e)
/// <summary>
/// 배경 페인트시 처리하기
/// </summary>
/// <param name="e">이벤트 인자</param>
protected override void OnPaintBackground(PaintEventArgs e)
{
e.Graphics.Clear(Color.White);
}
#endregion
#region 페인트시 처리하기 - OnPaint(e)
/// <summary>
/// 페인트시 처리하기
/// </summary>
/// <param name="e">이벤트 인자</param>
protected override void OnPaint(PaintEventArgs e)
{
DrawPentagram(e.Graphics);
}
#endregion
////////////////////////////////////////////////////////////////////////////////////////// Private
#region 라디안 구하기 - GetRadian(degree)
/// <summary>
/// 라디안 구하기
/// </summary>
/// <param name="degree">도</param>
/// <returns>라디안</returns>
private double GetRadian(double degree)
{
return Math.PI * degree / 180.0;
}
#endregion
#region 원 그리기 - DrawCircle(graphics, pen, centerX, centerY, radius)
/// <summary>
/// 원 그리기
/// </summary>
/// <param name="graphics">그래픽스</param>
/// <param name="pen">펜</param>
/// <param name="centerX">중심 X 좌표</param>
/// <param name="centerY">중심 Y 좌표</param>
/// <param name="radius">반경</param>
private void DrawCircle(Graphics graphics, Pen pen, int centerX, int centerY, int radius)
{
Rectangle rectangle = new Rectangle(centerX - radius, centerY - radius, radius * 2, radius * 2);
graphics.DrawEllipse(pen, rectangle);
}
#endregion
#region 오각형 그리기 - DrawPentagram(graphics, pointArray)
/// <summary>
/// 오각형 그리기
/// </summary>
/// <param name="graphics">그래픽스</param>
/// <param name="pointArray">포인트 배열</param>
private void DrawPentagram(Graphics graphics, Point[] pointArray)
{
Point startPoint = pointArray[0];
int offset = 2;
for(int i = 0; i < pointArray.Length; i++)
{
Point endPoint = pointArray[offset % 5];
graphics.DrawLine(Pens.Red, startPoint, endPoint);
startPoint = endPoint;
offset += 2;
}
}
#endregion
#region 오각형 그리기 - DrawPentagram(graphics)
/// <summary>
/// 오각형 그리기
/// </summary>
/// <param name="graphics">그래픽스</param>
private void DrawPentagram(Graphics graphics)
{
Point[] pointArray = new Point[5];
int centerX = Width / 2;
int centerY = Height / 2;
int radius = 200;
DrawCircle(graphics, this.bluePen, centerX, centerY, radius);
DrawCircle(graphics, this.redPen, centerX, centerY - radius, 10);
double yPosition = Math.Cos(GetRadian(72)) * radius;
int lastDegree = 90 - 72;
double xPosition = Math.Cos(GetRadian(lastDegree)) * radius;
DrawCircle(graphics, this.redPen, centerX - (int)xPosition, centerY - (int)yPosition, 10);
pointArray[0] = new Point(centerX, centerY - radius);
pointArray[1] = new Point(centerX - (int)xPosition, centerY - (int)yPosition);
graphics.DrawLine(redPen, pointArray[0], pointArray[1]);
xPosition = Math.Cos(GetRadian(72 - lastDegree)) * radius;
lastDegree = 90 - (72 - lastDegree);
yPosition = Math.Cos(GetRadian(lastDegree)) * radius;
DrawCircle(graphics, redPen, centerX - (int)xPosition, centerY + (int)yPosition, 10);
pointArray[2] = new Point(centerX - (int)xPosition, centerY + (int)yPosition);
graphics.DrawLine(redPen, pointArray[1], pointArray[2]);
yPosition = Math.Cos(GetRadian(72 - lastDegree)) * radius;
xPosition = Math.Sin(GetRadian(lastDegree)) * radius;
DrawCircle(graphics, redPen, centerX + (int)xPosition, centerY + (int)yPosition, 10);
pointArray[3] = new Point(centerX + (int)xPosition, centerY + (int)yPosition);
graphics.DrawLine(redPen, pointArray[2], pointArray[3]);
lastDegree = 72 - (90 - lastDegree);
xPosition = Math.Cos(GetRadian(lastDegree)) * radius;
yPosition = Math.Sin(GetRadian(lastDegree)) * radius;
DrawCircle(graphics, redPen, centerX + (int)xPosition, centerY - (int)yPosition, 10);
pointArray[4] = new Point(centerX + (int)xPosition, centerY - (int)yPosition);
graphics.DrawLine(redPen, pointArray[3], pointArray[4]);
graphics.DrawLine(redPen, pointArray[4], pointArray[0]);
DrawPentagram(graphics, pointArray);
}
#endregion
}
}
728x90
반응형
그리드형(광고전용)
'C# > WinForm' 카테고리의 다른 글
[C#/WINFORM] Process 클래스 : GetProcessesByName 정적 메소드를 사용해 프로그램 중복 실행 여부 구하기 (0) | 2019.08.16 |
---|---|
[C#/WINFORM] TextBox 클래스 : ScrollToCaret 메소드를 사용해 마지막 라인으로 스크롤하기 (0) | 2019.08.16 |
[C#/WINFORM] 투명 배경 스플래시 이미지 사용하기 (0) | 2019.08.15 |
[C#/WINFORM] InstalledFontCollection 클래스 : 영문 폰트명 리스트 구하기 (0) | 2019.08.02 |
[C#/WINFORM] 타원과 원 내부 마우스 위치 여부 구하기 (0) | 2019.07.14 |
[C#/WINFORM] SynchronizationContext 클래스 : 크로스 스레드(Cross Thread) 처리하기 (0) | 2019.07.13 |
[C#/WINFORM] 실시간 스트리밍 프로토콜(RTSP)을 사용해 동영상 재생하기 (0) | 2019.06.14 |
[C#/WINFORM] 웹 카메라 사용하기 (0) | 2019.06.12 |
[C#/WINFORM] A* (AStar) 길 찾기 알고리즘 사용하기 (0) | 2019.06.09 |
[C#/WINFORM] Screen 클래스 : AllScreens 정적 속성을 사용해 다른 모니터에서 폼 표시하기 (0) | 2019.05.25 |
댓글을 달아 주세요