upsidedown

SEM 6:CG Reflection

Apr 3rd, 2013
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.59 KB | None | 0 0
  1.  
  2. // reflectionView.cpp : implementation of the CreflectionView class
  3. //
  4.  
  5. #include "stdafx.h"
  6. // SHARED_HANDLERS can be defined in an ATL project implementing preview, thumbnail
  7. // and search filter handlers and allows sharing of document code with that project.
  8. #ifndef SHARED_HANDLERS
  9. #include "reflection.h"
  10. #endif
  11.  
  12. #include "reflectionDoc.h"
  13. #include "reflectionView.h"
  14.  
  15. #ifdef _DEBUG
  16. #define new DEBUG_NEW
  17. #endif
  18.  
  19.  
  20. // CreflectionView
  21.  
  22. IMPLEMENT_DYNCREATE(CreflectionView, CView)
  23.  
  24. BEGIN_MESSAGE_MAP(CreflectionView, CView)
  25.     ON_WM_CONTEXTMENU()
  26.     ON_WM_RBUTTONUP()
  27. END_MESSAGE_MAP()
  28.  
  29. // CreflectionView construction/destruction
  30.  
  31. CreflectionView::CreflectionView()
  32. {
  33.     // TODO: add construction code here
  34.  
  35. }
  36.  
  37. CreflectionView::~CreflectionView()
  38. {
  39. }
  40.  
  41. BOOL CreflectionView::PreCreateWindow(CREATESTRUCT& cs)
  42. {
  43.     // TODO: Modify the Window class or styles here by modifying
  44.     //  the CREATESTRUCT cs
  45.  
  46.     return CView::PreCreateWindow(cs);
  47. }
  48.  
  49. // CreflectionView drawing
  50.  
  51.  
  52. POINT reflect(POINT p, int type)
  53. {
  54.     POINT a;
  55.     switch(type)
  56.     {
  57.     case 1: //about x axis
  58.         a.x=p.x;
  59.         a.y=-p.y;
  60.         break;
  61.     case 2: //about y axis
  62.         a.x=-p.x;
  63.         a.y=p.y;
  64.         break;
  65.     case 3: //about xy axis
  66.         a.x=-p.x;
  67.         a.y=-p.y;
  68.         break;
  69.     }
  70.  
  71.     return a;
  72.    
  73. }
  74.  
  75.  
  76. POINT translate(POINT x,POINT origin)
  77. {
  78.     x.x-=origin.x;
  79.     x.y-=origin.y;
  80.     return x;
  81. }
  82.  
  83. POINT translate_back(POINT x,POINT origin)
  84. {
  85.     x.x+=origin.x;
  86.     x.y+=origin.y;
  87.  
  88.     return x;
  89. }
  90.  
  91.  
  92. void CreflectionView::OnDraw(CDC* x)
  93. {
  94.     CreflectionDoc* pDoc = GetDocument();
  95.     ASSERT_VALID(pDoc);
  96.     if (!pDoc)
  97.         return;
  98.  
  99.     //draw line, origin at 500,250
  100.     POINT origin;
  101.     origin.x=500;
  102.     origin.y=250;
  103.  
  104.     x->MoveTo(500,0);
  105.     x->LineTo(500,1000);
  106.     x->MoveTo(0,250);
  107.     x->LineTo(1000,250);
  108.  
  109.  
  110.     //draw triangle
  111.     POINT a,b,c;
  112.     a.x=250;
  113.     a.y=75;
  114.  
  115.     b.x=250;
  116.     b.y=200;
  117.  
  118.     c.x=400;
  119.     c.y=200;
  120.  
  121.     x->MoveTo(a);
  122.     x->LineTo(b);
  123.     x->LineTo(c);
  124.     x->LineTo(a);
  125.  
  126.     a=translate(a,origin);
  127.     b=translate(b,origin);
  128.     c=translate(c,origin);
  129.  
  130.     POINT a1,b1,c1,a2,b2,c2,a3,b3,c3;
  131.     //reflect and draw about x axis
  132.     a1=reflect(a,1);
  133.     b1=reflect(b,1);
  134.     c1=reflect(c,1);
  135.  
  136.     a1=translate_back(a1,origin);
  137.     b1=translate_back(b1,origin);
  138.     c1=translate_back(c1,origin);
  139.  
  140.     x->MoveTo(a1);
  141.     x->LineTo(b1);
  142.     x->LineTo(c1);
  143.     x->LineTo(a1);
  144.  
  145.  
  146.    
  147.     //reflect and draw about y axis
  148.     a2=reflect(a,2);
  149.     b2=reflect(b,2);
  150.     c2=reflect(c,2);
  151.  
  152.     a2=translate_back(a2,origin);
  153.     b2=translate_back(b2,origin);
  154.     c2=translate_back(c2,origin);
  155.  
  156.     x->MoveTo(a2);
  157.     x->LineTo(b2);
  158.     x->LineTo(c2);
  159.     x->LineTo(a2);
  160.  
  161.  
  162.     //reflect and draw about xy axis
  163.     a3=reflect(a,3);
  164.     b3=reflect(b,3);
  165.     c3=reflect(c,3);
  166.  
  167.     a3=translate_back(a3,origin);
  168.     b3=translate_back(b3,origin);
  169.     c3=translate_back(c3,origin);
  170.  
  171.     x->MoveTo(a3);
  172.     x->LineTo(b3);
  173.     x->LineTo(c3);
  174.     x->LineTo(a3);
  175.  
  176.  
  177.  
  178.  
  179.  
  180.     // TODO: add draw code for native data here
  181. }
  182.  
  183. void CreflectionView::OnRButtonUp(UINT /* nFlags */, CPoint point)
  184. {
  185.     ClientToScreen(&point);
  186.     OnContextMenu(this, point);
  187. }
  188.  
  189. void CreflectionView::OnContextMenu(CWnd* /* pWnd */, CPoint point)
  190. {
  191. #ifndef SHARED_HANDLERS
  192.     theApp.GetContextMenuManager()->ShowPopupMenu(IDR_POPUP_EDIT, point.x, point.y, this, TRUE);
  193. #endif
  194. }
  195.  
  196.  
  197. // CreflectionView diagnostics
  198.  
  199. #ifdef _DEBUG
  200. void CreflectionView::AssertValid() const
  201. {
  202.     CView::AssertValid();
  203. }
  204.  
  205. void CreflectionView::Dump(CDumpContext& dc) const
  206. {
  207.     CView::Dump(dc);
  208. }
  209.  
  210. CreflectionDoc* CreflectionView::GetDocument() const // non-debug version is inline
  211. {
  212.     ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CreflectionDoc)));
  213.     return (CreflectionDoc*)m_pDocument;
  214. }
  215. #endif //_DEBUG
Advertisement
Add Comment
Please, Sign In to add comment