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

📄 hw4_7view.cpp

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

#include "stdafx.h"
#include "hw4_7.h"

#include "hw4_7Doc.h"
#include "hw4_7View.h"

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

/////////////////////////////////////////////////////////////////////////////
// CHw4_7View

IMPLEMENT_DYNCREATE(CHw4_7View, CView)

BEGIN_MESSAGE_MAP(CHw4_7View, CView)
	//{{AFX_MSG_MAP(CHw4_7View)
		// 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_7View construction/destruction

CHw4_7View::CHw4_7View()
{
	// TODO: add construction code here
	BkColor = RGB(255, 255, 255);
}

CHw4_7View::~CHw4_7View()
{
}

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

	return CView::PreCreateWindow(cs);
}

/////////////////////////////////////////////////////////////////////////////
// CHw4_7View drawing

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

	DrawEdge(pDC, pDoc->plg);
	MarginFill(pDC, pDoc->plg, RGB(255, 0, 0));
}

/////////////////////////////////////////////////////////////////////////////
// CHw4_7View printing

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

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

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

/////////////////////////////////////////////////////////////////////////////
// CHw4_7View diagnostics

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

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

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

/////////////////////////////////////////////////////////////////////////////
// CHw4_7View message handlers

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

void CHw4_7View::DrawEdge(CDC *pDC, list<CPoint> &polygon)
{
	list<CPoint>::iterator p_ite;

	p_ite= polygon.begin();
	pDC->MoveTo(p_ite->x, p_ite->y);
	right = p_ite->x;

	for (++p_ite; p_ite != polygon.end(); ++p_ite)
	{
		pDC->LineTo(p_ite->x, p_ite->y);
		if (p_ite->x > right) right = p_ite->x;
	}
	p_ite = polygon.begin();
	pDC->LineTo(p_ite->x, p_ite->y);
}

void CHw4_7View::MarginFill(CDC *pDC, list<CPoint> &polygon, COLORREF color)
{
	list<CPoint>::iterator p_ite;
	int y, x1, y1, ymax, ymin, i, j;
	double x, k_r;

	if (polygon.size() <= 2)
	{
		pDC->TextOut(0, 0, "Not a polygon!");
		return;
	}

	p_ite = polygon.begin();
	x1 = p_ite->x;
	y1 = p_ite->y;
	for (i = 1; i <= polygon.size(); ++i)
	{
		++p_ite;
		if (p_ite == polygon.end()) p_ite = polygon.begin();
		if (y1 != p_ite->y)
		{
			k_r = (double)(x1 - p_ite->x) / (double)(y1 - p_ite->y);
			if (y1 < p_ite->y)
			{
				ymin = y1;
				ymax = p_ite->y;
				x = x1;
			}
			else if (y1 > p_ite->y)
			{
				ymin = p_ite->y;
				ymax = y1;
				x = p_ite->x;
			}

			for (y = ymin; y < ymax; ++y)
			{
				for (j = (int)(x+0.5); j < right; ++j)
				{
					if (pDC->GetPixel(j, y) == BkColor)
					{
						pDC->SetPixel(j, y, color);
					}
					else if(pDC->GetPixel(j, y) != RGB(0,0,0))
					{
						pDC->SetPixel(j, y, BkColor);
					}
				}
				x += k_r;
			}
		}
		x1 = p_ite->x;
		y1 = p_ite->y;
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -