📄 hw4_3view.cpp
字号:
// hw4_3View.cpp : implementation of the CHw4_3View class
//
#include "stdafx.h"
#include "hw4_3.h"
#include "hw4_3Doc.h"
#include "hw4_3View.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CHw4_3View
IMPLEMENT_DYNCREATE(CHw4_3View, CView)
BEGIN_MESSAGE_MAP(CHw4_3View, CView)
//{{AFX_MSG_MAP(CHw4_3View)
// 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_3View construction/destruction
CHw4_3View::CHw4_3View()
{
// TODO: add construction code here
}
CHw4_3View::~CHw4_3View()
{
}
BOOL CHw4_3View::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs
return CView::PreCreateWindow(cs);
}
/////////////////////////////////////////////////////////////////////////////
// CHw4_3View drawing
void CHw4_3View::OnDraw(CDC* pDC)
{
CHw4_3Doc* 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, rect.bottom/2);
// square1
DrawEdge(pDC, pDoc->square1, RGB(0, 0, 0));
ScanFill(pDC, pDoc->square1, RGB(255, 0, 0));
// square2
DrawEdge(pDC, pDoc->square2, RGB(0, 0, 0));
ScanFill(pDC, pDoc->square2, RGB(255, 0, 0));
}
/////////////////////////////////////////////////////////////////////////////
// CHw4_3View printing
BOOL CHw4_3View::OnPreparePrinting(CPrintInfo* pInfo)
{
// default preparation
return DoPreparePrinting(pInfo);
}
void CHw4_3View::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add extra initialization before printing
}
void CHw4_3View::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add cleanup after printing
}
/////////////////////////////////////////////////////////////////////////////
// CHw4_3View diagnostics
#ifdef _DEBUG
void CHw4_3View::AssertValid() const
{
CView::AssertValid();
}
void CHw4_3View::Dump(CDumpContext& dc) const
{
CView::Dump(dc);
}
CHw4_3Doc* CHw4_3View::GetDocument() // non-debug version is inline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CHw4_3Doc)));
return (CHw4_3Doc*)m_pDocument;
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// functions
void CHw4_3View::DrawEdge(CDC *pDC, list<CPoint> &polygon, int color)
{
list<CPoint>::iterator p_ite;
p_ite= polygon.begin();
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);
p_ite = polygon.begin();
pDC->LineTo(p_ite->x, p_ite->y);
}
void CHw4_3View::ScanFill(CDC *pDC, list<CPoint> &polygon, int color)
{
int i, x1, y1, x_maxy, miny, scanline;
list<CPoint>::iterator p_ite;
list<node>::iterator n_ite;
list<node> AET_temp;
if (polygon.size() <= 2)
{
pDC->TextOut(0, 0, "Not a polygon!");
return ;
}
p_ite= polygon.begin();
num = 1;
y_max = y_min = p_ite->y;
for (++p_ite; p_ite != polygon.end(); ++p_ite, ++num)
{
if (p_ite->y < y_min) y_min = p_ite->y;
if (p_ite->y > y_max) y_max = 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_maxy = x1;
miny = p_ite->y;
m_ET[y1-y_min].push_back(node((float)x_maxy, (float)(x1-p_ite->x)/(float)(y1-p_ite->y), miny));
}
else if (y1 < p_ite->y)
{
x_maxy = p_ite->x;
miny = y1;
m_ET[p_ite->y-y_min].push_back(node((float)x_maxy, (float)(x1-p_ite->x)/(float)(y1-p_ite->y), miny));
}
}
// fillrect
scanline = y_max;
while (scanline >= y_min)
{
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, color);
Sleep(1);
++n_ite;
}
--scanline;
n_ite = AET.begin();
while (n_ite != AET.end())
{
node temp = *n_ite;
if (temp.ymin < scanline)
AET_temp.push_back(node((temp.x-temp.k_r), temp.k_r, temp.ymin));
++n_ite;
}
AET.swap(AET_temp);
AET_temp.clear();
}
delete []m_ET;
}
/////////////////////////////////////////////////////////////////////////////
// CHw4_3View message handlers
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -