⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 hw4_6view.cpp

📁 给定四个点绘制图4-30所示的不同转角的两个正方形
💻 CPP
字号:
// hw4_6View.cpp : implementation of the CHw4_6View class
//

#include "stdafx.h"
#include "hw4_6.h"

#include "hw4_6Doc.h"
#include "hw4_6View.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CHw4_6View

IMPLEMENT_DYNCREATE(CHw4_6View, CView)

BEGIN_MESSAGE_MAP(CHw4_6View, CView)
	//{{AFX_MSG_MAP(CHw4_6View)
	ON_COMMAND(ID_FILL, OnFill)
	//}}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_6View construction/destruction

CHw4_6View::CHw4_6View()
{
	// TODO: add construction code here

}

CHw4_6View::~CHw4_6View()
{
}

BOOL CHw4_6View::PreCreateWindow(CREATESTRUCT& cs)
{
	// TODO: Modify the Window class or styles here by modifying
	//  the CREATESTRUCT cs

	return CView::PreCreateWindow(cs);
}

/////////////////////////////////////////////////////////////////////////////
// CHw4_6View drawing

void CHw4_6View::OnDraw(CDC* pDC)
{
	CHw4_6Doc* 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);

	// draw edges
	DrawEdge(pDC, pDoc->sqr1, RGB(0, 0, 0));
	DrawEdge(pDC, pDoc->sqr2, RGB(0, 0, 0));
	DrawEdge(pDC, pDoc->sqr3, RGB(0, 0, 0));
	DrawEdge(pDC, pDoc->sqr4, RGB(0, 0, 0));
	// square1
	ScanFill(pDC, pDoc->sqr1, RGB(255, 0, 0));
	// square2
	ScanFill(pDC, pDoc->sqr2, RGB(255, 255, 0));
	// square3
	ScanFill(pDC, pDoc->sqr3, RGB(0, 0, 0));
	// square4
	ScanFill(pDC, pDoc->sqr4, RGB(0, 0, 255));
}

/////////////////////////////////////////////////////////////////////////////
// CHw4_6View printing

BOOL CHw4_6View::OnPreparePrinting(CPrintInfo* pInfo)
{
	// default preparation
	return DoPreparePrinting(pInfo);
}

void CHw4_6View::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: add extra initialization before printing
}

void CHw4_6View::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: add cleanup after printing
}

/////////////////////////////////////////////////////////////////////////////
// CHw4_6View diagnostics

#ifdef _DEBUG
void CHw4_6View::AssertValid() const
{
	CView::AssertValid();
}

void CHw4_6View::Dump(CDumpContext& dc) const
{
	CView::Dump(dc);
}

CHw4_6Doc* CHw4_6View::GetDocument() // non-debug version is inline
{
	ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CHw4_6Doc)));
	return (CHw4_6Doc*)m_pDocument;
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CHw4_6View message handlers

void CHw4_6View::OnFill() 
{
	// TODO: Add your command handler code here
	/*CDC *pDC = GetDC();
	CHw4_6Doc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);

	// square1
	ScanFill(pDC, pDoc->sqr1, RGB(255, 0, 0));
	// square2
	ScanFill(pDC, pDoc->sqr2, RGB(255, 255, 0));
	// square3
	ScanFill(pDC, pDoc->sqr3, RGB(0, 0, 0));
	// square4
	ScanFill(pDC, pDoc->sqr4, RGB(0, 0, 255));
	*/
}


/////////////////////////////////////////////////////////////////////////////
// functions

void CHw4_6View::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)
		pDC->LineTo(p_ite->x, p_ite->y);
	p_ite = polygon.begin();
	pDC->LineTo(p_ite->x, p_ite->y);
}

void CHw4_6View::ScanFill(CDC *pDC, list<CPoint> &polygon, int color)
{
	int i, x1, y1, x_miny, maxy, 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_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, color);
			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 + -