DDALineView.cpp
Upload User: xmsintoyu
Upload Date: 2022-05-05
Package Size: 9731k
Code Size: 4k
Category:

Graph Drawing

Development Platform:

Visual C++

  1. // DDALineView.cpp : implementation of the CDDALineView class
  2. //
  3. #include "stdafx.h"
  4. #include "DDALine.h"
  5. #include "newDialog.h"
  6. #include "DDALineDoc.h"
  7. #include "DDALineView.h"
  8. #include <String.h>
  9. #ifdef _DEBUG
  10. #define new DEBUG_NEW
  11. #undef THIS_FILE
  12. static char THIS_FILE[] = __FILE__;
  13. #endif
  14. /////////////////////////////////////////////////////////////////////////////
  15. // CDDALineView
  16. IMPLEMENT_DYNCREATE(CDDALineView, CView)
  17. BEGIN_MESSAGE_MAP(CDDALineView, CView)
  18. //{{AFX_MSG_MAP(CDDALineView)
  19. ON_COMMAND(ID_MENUITEM32771, OnMenuitem32771)
  20. //}}AFX_MSG_MAP
  21. // Standard printing commands
  22. ON_COMMAND(ID_FILE_PRINT, CView::OnFilePrint)
  23. ON_COMMAND(ID_FILE_PRINT_DIRECT, CView::OnFilePrint)
  24. ON_COMMAND(ID_FILE_PRINT_PREVIEW, CView::OnFilePrintPreview)
  25. END_MESSAGE_MAP()
  26. /////////////////////////////////////////////////////////////////////////////
  27. // CDDALineView construction/destruction
  28. CDDALineView::CDDALineView()
  29. {
  30. // TODO: add construction code here
  31. }
  32. CDDALineView::~CDDALineView()
  33. {
  34. }
  35. BOOL CDDALineView::PreCreateWindow(CREATESTRUCT& cs)
  36. {
  37. // TODO: Modify the Window class or styles here by modifying
  38. //  the CREATESTRUCT cs
  39. return CView::PreCreateWindow(cs);
  40. }
  41. /////////////////////////////////////////////////////////////////////////////
  42. // CDDALineView drawing
  43. void CDDALineView::OnDraw(CDC* pDC)
  44. {
  45. CDDALineDoc* pDoc = GetDocument();
  46. ASSERT_VALID(pDoc);
  47. int width=::GetSystemMetrics(SM_CXSCREEN);
  48. int height=::GetSystemMetrics(SM_CYSCREEN);
  49. int xtemp=40;
  50. int ytemp;
  51. int pos=0;
  52. CString str;
  53. //绘制横线
  54. for(ytemp=height-160;ytemp>=0;ytemp-=40)
  55. {
  56. pDC->MoveTo(xtemp,ytemp);
  57. pDC->LineTo(width,ytemp);
  58. str.Format("%d",pos); //标写坐标
  59. pDC->TextOut(16,ytemp-6,str);
  60. pos++;
  61. }
  62. //绘制竖线
  63. ytemp=height-160;
  64. pos=0;
  65. for(xtemp=40;xtemp<=width;xtemp+=40)
  66. {
  67. pDC->MoveTo(xtemp,0);
  68. pDC->LineTo(xtemp,ytemp);
  69. str.Format("%d",pos); //标写坐标
  70. pDC->TextOut(xtemp-6,height-150,str);
  71. pos++;
  72. }
  73. }
  74. /////////////////////////////////////////////////////////////////////////////
  75. // CDDALineView printing
  76. BOOL CDDALineView::OnPreparePrinting(CPrintInfo* pInfo)
  77. {
  78. // default preparation
  79. return DoPreparePrinting(pInfo);
  80. }
  81. void CDDALineView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
  82. {
  83. // TODO: add extra initialization before printing
  84. }
  85. void CDDALineView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
  86. {
  87. // TODO: add cleanup after printing
  88. }
  89. /////////////////////////////////////////////////////////////////////////////
  90. // CDDALineView diagnostics
  91. #ifdef _DEBUG
  92. void CDDALineView::AssertValid() const
  93. {
  94. CView::AssertValid();
  95. }
  96. void CDDALineView::Dump(CDumpContext& dc) const
  97. {
  98. CView::Dump(dc);
  99. }
  100. CDDALineDoc* CDDALineView::GetDocument() // non-debug version is inline
  101. {
  102. ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CDDALineDoc)));
  103. return (CDDALineDoc*)m_pDocument;
  104. }
  105. #endif //_DEBUG
  106. /////////////////////////////////////////////////////////////////////////////
  107. // CDDALineView message handlers
  108. void CDDALineView::OnMenuitem32771() 
  109. {
  110. Invalidate();
  111. CDDALineDoc* pDoc = (CDDALineDoc*) GetDocument();
  112. newDialog ip;
  113. if(ip.DoModal()==IDOK)
  114. {
  115. pDoc->x0=ip.m_x0;
  116. pDoc->x1=ip.m_x1;
  117. pDoc->y0=ip.m_y0;
  118. pDoc->y1=ip.m_y1;
  119. }
  120. DDALine(pDoc->x0,pDoc->y0,pDoc->x1,pDoc->y1);
  121. }
  122. void CDDALineView::Draw(int x,int y)
  123. {
  124. int height=::GetSystemMetrics(SM_CYSCREEN);
  125. CDC *pDC=GetDC();
  126. int xx,yy;
  127. xx=40+40*x;
  128. yy=height-160-40*y;
  129. pDC->Ellipse(xx-10,yy-10,xx+10,yy+10);
  130. }
  131. void CDDALineView::DDALine(int x0, int y0, int x1, int y1)
  132. {
  133. int x;
  134. float dx,dy,y,k;
  135. dx=x1-x0;
  136. dy=y1-y0;
  137. k=dy/dx;
  138. if(k<=1 && k>=-1) //斜率小于1
  139. {
  140. y=y0;
  141. for(x=x0;x<=x1;x++)
  142. {
  143. Draw(x,int(y+0.5));
  144. y=y+k;
  145. }
  146. }
  147. else //斜率大于1
  148. {
  149. int yy;
  150. float xx;
  151. xx=x0;
  152. for(yy=y0;yy<=y1;yy++)
  153. {
  154. Draw(int(xx+0.5),yy);
  155. xx=xx+(1/k);
  156. }
  157. }
  158. }