요새 그나마 집에 빨리 돌아오기 때문에 PSP 프로그램을 만드는 도중 갑자기 심심해서 이런 저런 기능들을 올려두면 괜찮을것 같은 생각이 드는게 있어서 옛날에 만들어 사용했었던 부분을 약간 정리해봤습니다( 정리라고 하기엔 지저분한 소스네요 ). 일단 첫 번째는 폴리곤이랄까요 그 부분 이동하는 부분과 입력하는 부분을 대충 간단하게 구현해 봤습니다.
DrawPolygon.cs (Language : csharp)
using System ;
using System .Collections .Generic ;
using System .ComponentModel ;
using System .Data ;
using System .Drawing ;
using System .Linq ;
using System .Text ;
using System .Windows .Forms ;
namespace WindowsFormsApplication2
{
public partial class Form1 : Form
{
enum ProcessMode
{
Input,
Move
} ;
List<Point> m_PointList =
new List<Point>
( ) ;
ProcessMode m_Mode = ProcessMode.Input ;
int m_MoveIndex = -1 ;
public Form1( )
{
InitializeComponent( ) ;
}
private void Form1_MouseDown( object sender, MouseEventArgs e)
{
if ( e.Button == MouseButtons.Right )
{
m_Mode = ProcessMode.Move ;
return ;
}
switch ( m_Mode)
{
case ProcessMode.Input :
m_PointList.Add ( e.Location ) ;
break ;
case ProcessMode.Move :
for ( int i = 0 ; i < m_PointList.Count ; ++i )
if ( IsRectangle( e.Location , m_PointList[ i] , 5 ) )
{
m_MoveIndex = i;
break ;
}
break ;
}
Invalidate( ) ;
}
private void Form1_MouseUp( object sender, MouseEventArgs e)
{
m_MoveIndex = -1 ;
}
private void Form1_MouseMove( object sender, MouseEventArgs e)
{
if ( m_MoveIndex != -1 )
m_PointList[ m_MoveIndex] = e.Location ;
Invalidate( ) ;
}
private bool IsRectangle( Point Pt, Point Point, int Margin)
{
if ( ( Point.X - Margin <= Pt.X && Pt.X <= Point.X + Margin) &&
( Point.Y - Margin <= Pt.Y && Pt.Y <= Point.Y + Margin) )
return true ;
return false ;
}
private void Form1_Paint( object sender, PaintEventArgs e)
{
if ( m_PointList.Count > 1 )
{
Point
[ ] PtList =
new Point
[ m_PointList.
Count ] ;
for ( int i = 0 ; i < m_PointList.Count ; ++i)
{
PtList[ i] = m_PointList[ i] ;
}
System .
Drawing .
Drawing2D .
GraphicsPath Path =
new System .
Drawing .
Drawing2D .
GraphicsPath ( ) ;
Path.AddLines ( PtList) ;
e.Graphics .FillPath ( Brushes.Black , Path) ;
foreach ( Point Pt in PtList)
e.Graphics .FillRectangle ( Brushes.Gray , Pt.X - 5 , Pt.Y - 5 , 10 , 10 ) ;
}
}
}
}
간단하게 이렇게 구현이 되었습니다. 이건 뭐 별로 어렵지는 않은데, 귀찮아도 나중에 필요할데가 있을듯 하군요.
그리고 두 번째는 일종의 회전입니다. 마우스를 다운하고 마우스를 움직이면 해당 그림쪽이 회전되는 것입니다. 보통 X, Y의 간단한 계산으로는 자연스럽고 부드러운 회전이 불가능하고 atan2등의 삼각함수를 사용하면 부드럽게 가능합니다.
Spin.cs (Language : csharp)
using System ;
using System .Collections .Generic ;
using System .ComponentModel ;
using System .Data ;
using System .Drawing ;
using System .Linq ;
using System .Text ;
using System .Windows .Forms ;
namespace WindowsFormsApplication3
{
public partial class Form1 : Form
{
double Degree = 0.00 ;
bool IsDown = false ;
double MoveDegree = 0.00 ;
double FirstDegree = 0.00 ;
public Form1( )
{
InitializeComponent( ) ;
}
private void Form1_MouseDown( object sender, MouseEventArgs e)
{
FirstDegree = Math.Atan2 ( 150 - e.Y , 150 - e.X ) ;
IsDown = true ;
}
private void Form1_MouseUp( object sender, MouseEventArgs e)
{
IsDown = false ;
Degree -= MoveDegree;
MoveDegree = 0.00 ;
}
private void Form1_MouseMove( object sender, MouseEventArgs e)
{
if ( IsDown)
MoveDegree = FirstDegree + Math.Atan2 ( 150 - e.Y , 150 - e.X ) ;
Invalidate( ) ;
}
private void Form1_Paint( object sender, PaintEventArgs e)
{
Graphics g = e.Graphics ;
g.
MultiplyTransform ( new System .
Drawing .
Drawing2D .
Matrix ( 1 ,
0 ,
0 ,
1 ,
150 ,
150 ) ) ;
g.
MultiplyTransform ( new System .
Drawing .
Drawing2D .
Matrix ( ( float ) Math.
Cos ( Degree - MoveDegree
) ,
( float ) -Math.
Sin ( Degree - MoveDegree
) ,
( float ) Math.Sin ( Degree - MoveDegree) , ( float ) Math.Cos ( Degree - MoveDegree) , 0 , 0 ) ) ;
g.
MultiplyTransform ( new System .
Drawing .
Drawing2D .
Matrix ( 1 ,
0 ,
0 ,
1 ,
-150 ,
-150 ) ) ;
g.FillRectangle ( Brushes.Black , 100 , 100 , 100 , 100 ) ;
}
}
}
Atan2 함수를 사용하게 되면 0,0 포인트를 기준으로 X, Y좌표에 해당하는 각도를 반환해 줍니다. 그것을 이용한다면 C# 에서 기본적으로 Mapping 되어 지원되는 GDI+의 MultiplyMatrix로 회전식을 곱해주면 되겠죠^^. 물론 회전 수식 전엔 사각형 가운데로 원점을 이동시켜주어야 한다는 점이 존재합니다.
갑자기 허접하고 이상하긴 하지만 어딘가엔 필요할 것 같네요. 그럼 또 즐거운 하루 되세요.
크리에이티브 커먼즈 라이센스
트랙백 주소 :: http://www.filewiki.net/tc/trackback/85
댓글을 달아 주세요