📄 cgdemoview.cpp
字号:
// cgDemoView.cpp : implementation of the CCgDemoView class
//
#include "stdafx.h"
#include "cgDemo.h"
#include "cgDemoDoc.h"
#include "cgDemoView.h"
#include "TransMatrix.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CCgDemoView
IMPLEMENT_DYNCREATE(CCgDemoView, CView)
BEGIN_MESSAGE_MAP(CCgDemoView, CView)
//{{AFX_MSG_MAP(CCgDemoView)
ON_WM_CREATE()
ON_WM_SIZE()
ON_WM_LBUTTONDOWN()
ON_WM_LBUTTONDBLCLK()
ON_WM_MOUSEMOVE()
//}}AFX_MSG_MAP
// Standard printing commands
ON_COMMAND(ID_FILE_PRINT, CView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_DIRECT, CView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_PREVIEW, CView::OnFilePrintPreview)
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CCgDemoView construction/destruction
CCgDemoView::CCgDemoView()
{
// TODO: add construction code here
m_inputNumbers = 0;
m_transMatrixp = new CTransMatrix();
}
CCgDemoView::~CCgDemoView()
{
}
BOOL CCgDemoView::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs
return CView::PreCreateWindow(cs);
}
/////////////////////////////////////////////////////////////////////////////
// CCgDemoView drawing
void CCgDemoView::OnDraw(CDC* pDC)
{
CCgDemoDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
// TODO: add draw code for native data here
CRect viewRect;
int cx,cy;
POINT point1,point2;
// Screen center point
GetClientRect(&viewRect);
cx = viewRect.right/2;
cy = viewRect.bottom/2;
// Draw accordination
point1.x = 0;
point1.y = cy;
point2.x = viewRect.right;
point2.y = cy;
pDC->MoveTo(point1);
pDC->LineTo(point2);
point1.x = cx;
point1.y = 0;
point2.x = cx;
point2.y = viewRect.bottom;
pDC->MoveTo(point1);
pDC->LineTo(point2);
// Trans Object first by m_transMatrix in Doc
for (int i = 0; i < pDoc->m_objPoints; i++) {
pDoc->m_xViewPoint[i] = pDoc->m_xPoint[i];
pDoc->m_yViewPoint[i] = pDoc->m_yPoint[i];
}
m_transMatrixp->Trans2Dpoints(pDoc->m_objPoints,
pDoc->m_xViewPoint,
pDoc->m_yViewPoint,
pDoc->m_transMatrix);
// Then display.
point1.x = cx + pDoc->m_xViewPoint[0];
point1.y = cy - pDoc->m_yViewPoint[0];
for (i = 1; i < pDoc->m_objPoints; i++) {
point2.x = cx + pDoc->m_xViewPoint[i];
point2.y = cy - pDoc->m_yViewPoint[i];
pDC->MoveTo(point1);
pDC->LineTo(point2);
point1 = point2;
}
}
/////////////////////////////////////////////////////////////////////////////
// CCgDemoView printing
BOOL CCgDemoView::OnPreparePrinting(CPrintInfo* pInfo)
{
// default preparation
return DoPreparePrinting(pInfo);
}
void CCgDemoView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add extra initialization before printing
}
void CCgDemoView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add cleanup after printing
}
/////////////////////////////////////////////////////////////////////////////
// CCgDemoView diagnostics
#ifdef _DEBUG
void CCgDemoView::AssertValid() const
{
CView::AssertValid();
}
void CCgDemoView::Dump(CDumpContext& dc) const
{
CView::Dump(dc);
}
CCgDemoDoc* CCgDemoView::GetDocument() // non-debug version is inline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CCgDemoDoc)));
return (CCgDemoDoc*)m_pDocument;
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CCgDemoView message handlers
int CCgDemoView::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CView::OnCreate(lpCreateStruct) == -1)
return -1;
// TODO: Add your specialized creation code here
p_viewClientDC = new CClientDC(this);
return 0;
}
void CCgDemoView::OnSize(UINT nType, int cx, int cy)
{
CView::OnSize(nType, cx, cy);
// TODO: Add your message handler code here
m_wndWidth = cx;
m_wndHeight = cy;
}
void CCgDemoView::OnLButtonDown(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
m_inputX[m_inputNumbers] = point.x;
m_inputY[m_inputNumbers] = point.y;
m_inputNumbers++;
lsPoint = point;
curPoint = point;
p_viewClientDC->SetROP2(R2_NOTXORPEN);
p_viewClientDC->MoveTo(lsPoint);
p_viewClientDC->LineTo(curPoint);
CView::OnLButtonDown(nFlags, point);
}
void CCgDemoView::OnLButtonDblClk(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
m_inputX[m_inputNumbers] = point.x;
m_inputY[m_inputNumbers] = point.y;
m_inputNumbers++;
m_inputX[m_inputNumbers] = m_inputX[0];
m_inputY[m_inputNumbers] = m_inputY[0];
m_inputNumbers++;
// FillPolygon(m_inputNumbers, m_inputX, m_inputY);
m_inputNumbers = 0;
CView::OnLButtonDblClk(nFlags, point);
}
void CCgDemoView::OnMouseMove(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
if (m_inputNumbers) {
p_viewClientDC->SetROP2(R2_NOTXORPEN);
p_viewClientDC->MoveTo(lsPoint);
p_viewClientDC->LineTo(curPoint);
curPoint = point;
p_viewClientDC->MoveTo(lsPoint);
p_viewClientDC->LineTo(curPoint);
}
CView::OnMouseMove(nFlags, point);
}
// Fill polygon functions.
void CCgDemoView::FillPolygon(int n, int *x, int *y)
{
m_pEdges = 0;
pLoadPolygon(n,x,y);
if (m_pEdges <2 ) return;
m_iBegin = m_iEnd = 0;
m_scanLine = (int)m_yMax[0];
pInclude();
pUpdateXvalue();
while (m_iBegin != m_iEnd) {
pFillScanLine();
m_scanLine--;
pInclude();
pUpdateXvalue();
}
}
void CCgDemoView::pLoadPolygon(int n,int *x,int *y)
{
int i;
float x1,x2,y1,y2;
x1 = x[0]; y1 = y[0]+0.5;
for (i = 1; i < n;i++) {
x2 = x[i]; y2 = y[i]+0.5;
if (y2-y1) pInsertLine(x1,y1,x2,y2);
x1 = x2; y1 = y2;
}
}
void CCgDemoView::pInsertLine(float x1,float y1,float x2,float y2)
{
int i = m_pEdges;
float maxY,minY;
}
void CCgDemoView::pUpdateXvalue()
{
int i,j,start;
}
void CCgDemoView::pXsort(int i, int begin)
{
float temp;
}
static char m_patData[] = {
0, 0, 0, 1, 0, 0, 0, 0,
0, 0, 1, 0, 1, 0, 0, 0,
0, 1, 0, 0, 0, 1, 0, 0,
0, 1, 0, 0, 0, 1, 0, 0,
0, 1, 1, 1, 1, 1, 0, 0,
0, 1, 0, 0, 0, 1, 0, 0,
0, 1, 0, 0, 0, 1, 0, 0,
1, 1, 1, 1, 1, 1, 1, 1 };
void CCgDemoView::pFillScanLine()
{
POINT point1,point2;
/*
for (int i = m_iBegin; i < m_iEnd; i += 2) {
point1.x = m_Xa[i];
point2.x = m_Xa[i+1];
point1.y = point2.y = m_scanLine;
p_viewClientDC->MoveTo(point1);
p_viewClientDC->LineTo(point2);
} */
CRect vRect;
GetClientRect(&vRect);
for (int i = m_iBegin; i < m_iEnd; i += 2) {
point1.x = m_Xa[i];
point2.x = m_Xa[i+1];
point1.y = point2.y = m_scanLine;
// point1.x = vRect.right / 2 + m_Xa[i];
// point2.x = vRect.right / 2 + m_Xa[i+1];
// point1.y = point2.y = vRect.bottom / 2 - m_scanLine;
while (point1.x < point2.x) {
int x,y;
x = (point1.x - 10) % 8;
y = (point1.y - 10) % 8;
if (m_patData[x+y*8])
p_viewClientDC->SetPixel(point1, RGB(0, 255, 255));
point1.x++;
}
}
}
void CCgDemoView::pInclude()
{
while (m_iEnd < m_pEdges && m_yMax[m_iEnd] > m_scanLine) {
m_Xa[m_iEnd] = m_Xa[m_iEnd] - 0.5 * m_Dx[m_iEnd];
m_Dx[m_iEnd] = -m_Dx[m_iEnd];
m_iEnd++;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -