블로그는 귀차니즘

First Sensation
  • 공지
  • 지역로그
  • 태그
  • 방명록

심심해서 구현해본 그래픽 요소

Programming Tip 2008/07/03 23:58 귀차니스트
  요새 그나마 집에 빨리 돌아오기 때문에 PSP 프로그램을 만드는 도중 갑자기 심심해서 이런 저런 기능들을 올려두면 괜찮을것 같은 생각이 드는게 있어서 옛날에 만들어 사용했었던 부분을 약간 정리해봤습니다( 정리라고 하기엔 지저분한 소스네요 ). 일단 첫 번째는 폴리곤이랄까요 그 부분 이동하는 부분과 입력하는 부분을 대충 간단하게 구현해 봤습니다.

DrawPolygon.cs (Language : csharp)
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9.  
  10. namespace WindowsFormsApplication2
  11. {
  12.     public partial class Form1 : Form
  13.     {
  14.         enum ProcessMode
  15.         {
  16.             Input,
  17.             Move
  18.         };
  19.  
  20.         List<Point> m_PointList = new List<Point>();
  21.         ProcessMode m_Mode = ProcessMode.Input;
  22.         int m_MoveIndex = -1;
  23.  
  24.         public Form1()
  25.         {
  26.             InitializeComponent();
  27.         }
  28.  
  29.         private void Form1_MouseDown(object sender, MouseEventArgs e)
  30.         {
  31.             if (e.Button == MouseButtons.Right)
  32.             {
  33.                 m_Mode = ProcessMode.Move;
  34.                 return;
  35.             }
  36.  
  37.             switch (m_Mode)
  38.             {
  39.                 case ProcessMode.Input:
  40.                     m_PointList.Add(e.Location);
  41.                     break;
  42.                 case ProcessMode.Move:
  43.                     for( int i = 0; i < m_PointList.Count; ++i )
  44.                         if (IsRectangle(e.Location, m_PointList[i], 5))
  45.                         {
  46.                             m_MoveIndex = i;
  47.                             break;
  48.                         }
  49.                     break;
  50.             }
  51.  
  52.             Invalidate();
  53.         }
  54.  
  55.         private void Form1_MouseUp(object sender, MouseEventArgs e)
  56.         {
  57.             m_MoveIndex = -1;
  58.         }
  59.  
  60.         private void Form1_MouseMove(object sender, MouseEventArgs e)
  61.         {
  62.             if (m_MoveIndex != -1)
  63.                 m_PointList[m_MoveIndex] = e.Location;
  64.  
  65.             Invalidate();
  66.         }
  67.  
  68.         private bool IsRectangle(Point Pt, Point Point, int Margin)
  69.         {
  70.             if ((Point.X - Margin <= Pt.X && Pt.X <= Point.X + Margin) &&
  71.                 (Point.Y - Margin <= Pt.Y && Pt.Y <= Point.Y + Margin))
  72.                 return true;
  73.  
  74.             return false;
  75.         }
  76.  
  77.         private void Form1_Paint(object sender, PaintEventArgs e)
  78.         {
  79.             if (m_PointList.Count > 1)
  80.             {
  81.                 Point[] PtList = new Point[m_PointList.Count];
  82.                 for (int i = 0; i < m_PointList.Count; ++i)
  83.                 {
  84.  
  85.                     PtList[i] = m_PointList[i];
  86.                 }
  87.  
  88.                 System.Drawing.Drawing2D.GraphicsPath Path = new System.Drawing.Drawing2D.GraphicsPath();
  89.                 Path.AddLines(PtList);
  90.  
  91.                 e.Graphics.FillPath(Brushes.Black, Path);
  92.                 foreach (Point Pt in PtList)
  93.                     e.Graphics.FillRectangle(Brushes.Gray, Pt.X - 5, Pt.Y - 5, 10, 10);
  94.             }
  95.         }
  96.     }
  97. }
  98.  



  간단하게 이렇게 구현이 되었습니다. 이건 뭐 별로 어렵지는 않은데, 귀찮아도 나중에 필요할데가 있을듯 하군요.
  그리고 두 번째는 일종의 회전입니다. 마우스를 다운하고 마우스를 움직이면 해당 그림쪽이 회전되는 것입니다. 보통 X, Y의 간단한 계산으로는 자연스럽고 부드러운 회전이 불가능하고 atan2등의 삼각함수를 사용하면 부드럽게 가능합니다.

Spin.cs (Language : csharp)
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9.  
  10. namespace WindowsFormsApplication3
  11. {
  12.     public partial class Form1 : Form
  13.     {
  14.         double Degree = 0.00;
  15.         bool IsDown = false;
  16.  
  17.         double MoveDegree = 0.00;
  18.         double FirstDegree = 0.00;
  19.  
  20.         public Form1()
  21.         {
  22.             InitializeComponent();
  23.         }
  24.  
  25.         private void Form1_MouseDown(object sender, MouseEventArgs e)
  26.         {
  27.             FirstDegree = Math.Atan2( 150 - e.Y, 150 - e.X);
  28.             IsDown = true;
  29.         }
  30.  
  31.         private void Form1_MouseUp(object sender, MouseEventArgs e)
  32.         {
  33.             IsDown = false;
  34.             Degree -= MoveDegree;
  35.             MoveDegree = 0.00;
  36.         }
  37.  
  38.         private void Form1_MouseMove(object sender, MouseEventArgs e)
  39.         {
  40.             if (IsDown)
  41.                 MoveDegree = FirstDegree + Math.Atan2(150 - e.Y, 150 - e.X);
  42.  
  43.             Invalidate();
  44.         }
  45.  
  46.         private void Form1_Paint(object sender, PaintEventArgs e)
  47.         {
  48.             Graphics g = e.Graphics;
  49.  
  50.             g.MultiplyTransform(new System.Drawing.Drawing2D.Matrix(1, 0, 0, 1, 150, 150 ));
  51.             g.MultiplyTransform(new System.Drawing.Drawing2D.Matrix((float)Math.Cos(Degree - MoveDegree), (float)-Math.Sin(Degree - MoveDegree),
  52.                 (float)Math.Sin(Degree - MoveDegree), (float)Math.Cos(Degree - MoveDegree), 0, 0));
  53.             g.MultiplyTransform(new System.Drawing.Drawing2D.Matrix(1, 0, 0, 1, -150, -150 ));
  54.             g.FillRectangle(Brushes.Black, 100, 100, 100, 100);
  55.         }
  56.     }
  57. }
  58.  



  Atan2 함수를 사용하게 되면 0,0 포인트를 기준으로 X, Y좌표에 해당하는 각도를 반환해 줍니다. 그것을 이용한다면 C# 에서 기본적으로 Mapping 되어 지원되는 GDI+의 MultiplyMatrix로 회전식을 곱해주면 되겠죠^^. 물론 회전 수식 전엔 사각형 가운데로 원점을 이동시켜주어야 한다는 점이 존재합니다.
  갑자기 허접하고 이상하긴 하지만 어딘가엔 필요할 것 같네요. 그럼 또 즐거운 하루 되세요.

크리에이티브 커먼즈 라이센스
Creative Commons License
이 저작물은 크리에이티브 커먼즈 코리아 저작자표시-비영리-동일조건변경허락 2.0 대한민국 라이센스에 따라 이용하실 수 있습니다.

"Programming Tip" 분류의 다른 글

OpenMP 라고 아십니까?? (0)2008/07/27
Child Window Mouse Message 문제. (0)2008/07/01
버퍼 오버플로우, 오버런이란?? (0)2008/04/27
GDI+ 사용 시 Smart Pointer 란?? (0)2008/04/12
Com Library in C++ Builder - C++ 빌더에서 Com 라이브러리, .Net라이브러... (0)2008/03/29
2008/07/03 23:58 2008/07/03 23:58
TAG GDI+, Graphi
받은 트랙백이 없고, 댓글이 없습니다.

트랙백 주소 :: http://www.filewiki.net/tc/trackback/85

댓글을 달아 주세요

◀ 이전페이지 1 다음페이지 ▶

블로그 이미지
First Sensation 귀차니스트
rss
  • 관리자
  • 글쓰기

카테고리

  • 전체 (118)
    • Computer (3)
    • Language (14)
    • Reverse Engineering (1)
    • Algorithm (9)
    • TopCoder (3)
    • Library (2)
    • Programming (21)
    • Programming Tip (9)
    • PSP-Programming (10)
    • Program (5)
    • Small Talk (33)
    • Document (4)
    • OS Develope (4)

최근에 올라온 글

  • Script Interpreter - b....
  • VirtualHttpServer - 가.... (2)
  • 음.. 여러가지 일이 있.... (2)
  • 어후.. 드디어 인터럽트....
  • Kernel Image에 어이없....

최근에 달린 댓글

  • 헠 ㅋ 다음에도 들러주세용 ㅋㅋ. 귀차니스트 03/09
  • ㅎㅎ RSS로 첨 온 글이네.ㅋ. 당구리 02/22
  • 음.. 한글화 파일 0.5 버젼은.... 귀차니스트 02/22
  • 관리자만 볼 수 있는 댓글입.... 비밀방문자 01/30
  • 어떤 의미이신지 잘 모르겠네.... 귀차니스트 01/23

달력

«   2010/03   »
일 월 화 수 목 금 토
  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      

링크

  • kkamagui 프로그래밍 세상.
  • runner님의 이글루.
  • 당구리의 마굿간.
  • 동우fly.
  • 류광의 번역 이야기.
  • 서광열의 프로그래밍 언....
  • 준호씨의 블로그.
  • 최익필의 이름없는 블로그.
  • 위키는 귀차니즘.

최근에 받은 트랙백

  • 한게임 테트리스 인공지.... 고니's Life 2009
  • ACM 706 (Uva ID) : LCD.... 알고리즘 트레이닝 : Oh... 2009
  • 문제 4 : LCD 디스플레.... 최익필의 이름없는 블로그 2009
  • 궁극의 예외처리. 이름없는 블로그 2008
  • Maximum sum. 티스토리 지점 2008

글 보관함

  • 2010/03 (1)
  • 2010/02 (1)
  • 2010/01 (1)
  • 2009/12 (3)
  • 2009/08 (1)

태그목록

  • 클라리넷
  • interface design guide
  • Singleton
  • FTP
  • Shell
  • Application.Run
  • C
  • 병렬처리
  • Raw
  • Warcraft3
  • Library
  • 재귀적합성
  • 팡야계산기
  • Decompiler
  • SSD
  • Code
  • High Precision Event Timer
  • 한글화
  • AppWizard
  • Mouse Message
  • 입양
  • 표준
  • 개인정보유출
  • FreeType
  • boost::Tokenizer
  • TopCoder
  • Textcube
  • boost::array
  • 홈브류
  • PSP

지역로그 : 태그 : 방명록 : 관리자 : 글쓰기
귀차니스트’s Blog is powered by Textcube 1.7.5 : Risoluto / Designed by DesignNia.net