📄 hw4_5view.cpp
字号:
// hw4_5View.cpp : implementation of the CHw4_5View class
//
#include "stdafx.h"
#include "hw4_5.h"
#include "hw4_5Doc.h"
#include "hw4_5View.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CHw4_5View
IMPLEMENT_DYNCREATE(CHw4_5View, CView)
BEGIN_MESSAGE_MAP(CHw4_5View, CView)
//{{AFX_MSG_MAP(CHw4_5View)
// NOTE - the ClassWizard will add and remove mapping macros here.
// DO NOT EDIT what you see in these blocks of generated code!
//}}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()
/////////////////////////////////////////////////////////////////////////////
// CHw4_5View construction/destruction
CHw4_5View::CHw4_5View()
{
// TODO: add construction code here
}
CHw4_5View::~CHw4_5View()
{
}
BOOL CHw4_5View::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs
return CView::PreCreateWindow(cs);
}
/////////////////////////////////////////////////////////////////////////////
// CHw4_5View drawing
void CHw4_5View::OnDraw(CDC* pDC)
{
CHw4_5Doc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
// TODO: add draw code for native data here
CRect rect;
GetClientRect(&rect);
pDC->SetMapMode(MM_ANISOTROPIC);
pDC->SetWindowExt(rect.Width(), rect.Height());
pDC->SetViewportExt(rect.right, -rect.bottom);
// 左、上、下、右
pDC->SetViewportOrg(rect.right/2-150, rect.bottom/2-80);
// triangle1
ScanFill(pDC, pDoc->tr1);
// triangle2
ScanFill(pDC, pDoc->tr2);
// triangle3
ScanFill(pDC, pDoc->tr3);
// triangle4
ScanFill(pDC, pDoc->tr4);
// 左、上、右、下
pDC->SetViewportOrg(rect.right/2, rect.bottom/2-80);
// triangle1
ScanFill(pDC, pDoc->tr1);
// triangle2
ScanFill(pDC, pDoc->tr2);
// triangle4
ScanFill(pDC, pDoc->tr4);
// triangle3
ScanFill(pDC, pDoc->tr3);
// 左、右、上、下
pDC->SetViewportOrg(rect.right/2+150, rect.bottom/2-80);
// triangle1
ScanFill(pDC, pDoc->tr1);
// triangle4
ScanFill(pDC, pDoc->tr4);
// triangle2
ScanFill(pDC, pDoc->tr2);
// triangle3
ScanFill(pDC, pDoc->tr3);
// 左、右、下、上
pDC->SetViewportOrg(rect.right/2-150, rect.bottom/2+80);
// triangle1
ScanFill(pDC, pDoc->tr1);
// triangle4
ScanFill(pDC, pDoc->tr4);
// triangle3
ScanFill(pDC, pDoc->tr3);
// triangle2
ScanFill(pDC, pDoc->tr2);
// 左、下、上、右
pDC->SetViewportOrg(rect.right/2, rect.bottom/2+80);
// triangle1
ScanFill(pDC, pDoc->tr1);
// triangle3
ScanFill(pDC, pDoc->tr3);
// triangle2
ScanFill(pDC, pDoc->tr2);
// triangle4
ScanFill(pDC, pDoc->tr4);
// 左、下、右、上
pDC->SetViewportOrg(rect.right/2+150, rect.bottom/2+80);
// triangle1
ScanFill(pDC, pDoc->tr1);
// triangle3
ScanFill(pDC, pDoc->tr3);
// triangle4
ScanFill(pDC, pDoc->tr4);
// triangle2
ScanFill(pDC, pDoc->tr2);
}
/////////////////////////////////////////////////////////////////////////////
// CHw4_5View printing
BOOL CHw4_5View::OnPreparePrinting(CPrintInfo* pInfo)
{
// default preparation
return DoPreparePrinting(pInfo);
}
void CHw4_5View::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add extra initialization before printing
}
void CHw4_5View::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add cleanup after printing
}
/////////////////////////////////////////////////////////////////////////////
// CHw4_5View diagnostics
#ifdef _DEBUG
void CHw4_5View::AssertValid() const
{
CView::AssertValid();
}
void CHw4_5View::Dump(CDumpContext& dc) const
{
CView::Dump(dc);
}
CHw4_5Doc* CHw4_5View::GetDocument() // non-debug version is inline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CHw4_5Doc)));
return (CHw4_5Doc*)m_pDocument;
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CHw4_5View message handlers
/////////////////////////////////////////////////////////////////////////////
// functions
void CHw4_5View::ScanFill(CDC *pDC, list<CPoint> &polygon)
{
int i, x1, y1, x_miny, maxy, scanline;
list<CPoint>::iterator p_ite;
list<node>::iterator n_ite;
list<node> AET_temp;
p_ite= polygon.begin();
num = 1;
y_max = y_min = p_ite->y;
pDC->MoveTo(p_ite->x, p_ite->y);
for (++p_ite; p_ite != polygon.end(); ++p_ite, ++num)
{
pDC->LineTo(p_ite->x, p_ite->y);
if (p_ite->y < y_min) y_min = p_ite->y;
if (p_ite->y > y_max) y_max = p_ite->y;
}
p_ite = polygon.begin();
pDC->LineTo(p_ite->x, p_ite->y);
m_ET = new list<node>[y_max-y_min+1];
p_ite = polygon.begin();
for (i = 1; i <= num; ++i)
{
x1 = p_ite->x;
y1 = p_ite->y;
++p_ite;
if (p_ite == polygon.end()) p_ite = polygon.begin();
if (y1 < p_ite->y)
{
x_miny = x1;
maxy = p_ite->y;
m_ET[y1-y_min].push_back(node((float)x_miny, (float)(x1-p_ite->x)/(float)(y1-p_ite->y), maxy));
}
else if (y1 > p_ite->y)
{
x_miny = p_ite->x;
maxy = y1;
m_ET[p_ite->y-y_min].push_back(node((float)x_miny, (float)(x1-p_ite->x)/(float)(y1-p_ite->y), maxy));
}
}
// fillrect
scanline = y_min;
while (scanline <= y_max)
{
for (n_ite = m_ET[scanline-y_min].begin(); n_ite != m_ET[scanline-y_min].end(); ++n_ite)
AET.push_back(*n_ite);
AET.sort();
n_ite = AET.begin();
while (n_ite != AET.end())
{
node node_l = *n_ite;
++n_ite;
node node_r = *n_ite;
for (i = node_l.x; i < node_r.x; ++i)
pDC->SetPixel(i, scanline, RGB(255, 0, 0));
Sleep(1);
++n_ite;
}
++scanline;
n_ite = AET.begin();
while (n_ite != AET.end())
{
node temp = *n_ite;
if (temp.ymax > scanline)
AET_temp.push_back(node((temp.x+temp.k_r), temp.k_r, temp.ymax));
++n_ite;
}
AET.swap(AET_temp);
AET_temp.clear();
}
delete []m_ET;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -