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>
/// 원 중심 X 좌표
/// </summary>
private int circleCenterX;
/// <summary>
/// 원 중심 Y 좌표
/// </summary>
private int circleCenterY;
/// <summary>
/// 원 반경
/// </summary>
private int circleRadius = 100;
/// <summary>
/// 타원 중심 X 좌표
/// </summary>
private int ellipseCenterX;
/// <summary>
/// 타원 중심 Y 좌표
/// </summary>
private int ellipseCenterY;
/// <summary>
/// 타원 반경 X
/// </summary>
private int ellipseRadiusX = 150;
/// <summary>
/// 타원 반경 Y
/// </summary>
private int ellipseRadiusY = 100;
/// <summary>
/// 타원 C 값
/// </summary>
private int ellipseC;
#endregion
//////////////////////////////////////////////////////////////////////////////////////////////////// Constructor
////////////////////////////////////////////////////////////////////////////////////////// Public
#region 생성자 - MainForm()
/// <summary>
/// 생성자
/// </summary>
public MainForm()
{
InitializeComponent();
SetStyle(ControlStyles.ResizeRedraw | ControlStyles.UserPaint | ControlStyles.AllPaintingInWmPaint, true);
UpdateStyles();
Load += Form_Load;
}
#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)
{
DrawCircle(e.Graphics);
}
#endregion
#region 마우스 이동시 처리하기 - OnMouseMove(e)
/// <summary>
/// 마우스 이동시 처리하기
/// </summary>
/// <param name="e">이벤트 인자</param>
protected override void OnMouseMove(MouseEventArgs e)
{
base.OnMouseMove(e);
bool circleOver = false;
{
int x = Math.Abs(this.circleCenterX - e.X);
int y = Math.Abs(this.circleCenterY - e.Y);
if(Math.Sqrt(x * x + y * y) <= this.circleRadius)
{
circleOver = true;
}
}
bool ellipseOver = false;
{
int x;
int y;
double f1Length;
double f2Length;
if(this.ellipseRadiusX > this.ellipseRadiusY)
{
x = Math.Abs((this.ellipseCenterX - this.ellipseC) - e.X);
y = Math.Abs(this.ellipseCenterY - e.Y);
f1Length = Math.Sqrt(x * x + y * y);
x = Math.Abs((this.ellipseCenterX + this.ellipseC) - e.X);
f2Length = Math.Sqrt(x * x + y * y);
ellipseOver = (f1Length + f2Length) <= (2 * this.ellipseRadiusX);
}
else
{
y = Math.Abs((this.ellipseCenterY - this.ellipseC) - e.Y);
x = Math.Abs(e.X - this.ellipseCenterX);
f1Length = Math.Sqrt(x * x + y * y);
y = Math.Abs((this.ellipseCenterY + this.ellipseC) - e.Y);
f2Length = Math.Sqrt(x * x + y * y);
ellipseOver = (f1Length + f2Length) <= (2 * this.ellipseRadiusY);
}
}
Text = string.Format("타원과 원 내부 마우스 위치 여부 구하기 (원 : {0}, 타원 : {1})", circleOver, ellipseOver);
Invalidate();
}
#endregion
////////////////////////////////////////////////////////////////////////////////////////// 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)
{
this.circleCenterX = 150;
this.circleCenterY = 150;
this.ellipseCenterX = 300;
this.ellipseCenterY = 250;
if(this.ellipseRadiusX > this.ellipseRadiusY)
{
this.ellipseC = (int)Math.Sqrt(this.ellipseRadiusX * this.ellipseRadiusX - this.ellipseRadiusY * this.ellipseRadiusY);
}
else
{
this.ellipseC = (int)Math.Sqrt(this.ellipseRadiusY * this.ellipseRadiusY - this.ellipseRadiusX * this.ellipseRadiusX);
}
}
#endregion
//////////////////////////////////////////////////////////////////////////////// Function
#region 원 그리기 - DrawCircle(graphics)
/// <summary>
/// 원 그리기
/// </summary>
/// <param name="graphics">그래픽스</param>
private void DrawCircle(Graphics graphics)
{
Point mousePoint = PointToClient(Control.MousePosition);
Rectangle circleRectangle = new Rectangle
(
this.circleCenterX - this.circleRadius,
this.circleCenterY - this.circleRadius,
this.circleRadius * 2,
this.circleRadius * 2
);
graphics.DrawEllipse(Pens.Blue, circleRectangle);
graphics.DrawLine(Pens.Violet, mousePoint, new Point(this.circleCenterX, this.circleCenterY));
Rectangle ellipseRectangle = new Rectangle
(
this.ellipseCenterX - this.ellipseRadiusX,
this.ellipseCenterY - this.ellipseRadiusY,
this.ellipseRadiusX * 2,
this.ellipseRadiusY * 2
);
graphics.DrawEllipse(Pens.Red, ellipseRectangle);
Point focalPoint2;
Point focalPoint1;
if(this.ellipseRadiusX > this.ellipseRadiusY)
{
focalPoint1 = new Point(this.ellipseCenterX - this.ellipseC, this.ellipseCenterY);
focalPoint2 = new Point(this.ellipseCenterX + this.ellipseC, this.ellipseCenterY);
}
else
{
focalPoint1 = new Point(this.ellipseCenterX, this.ellipseCenterY - this.ellipseC);
focalPoint2 = new Point(this.ellipseCenterX, this.ellipseCenterY + this.ellipseC);
}
graphics.DrawLine(Pens.Violet, mousePoint, focalPoint1);
graphics.DrawLine(Pens.Violet, mousePoint, focalPoint2);
}
#endregion
}
}
728x90
반응형
그리드형(광고전용)
'C# > WinForm' 카테고리의 다른 글
[C#/WINFORM] UserControl 클래스 : 가상 리스트 박스 사용하기 (0) | 2019.08.16 |
---|---|
[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] 오각형(Pentagram) 그리기 (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 |
댓글을 달아 주세요