数字媒体技术计算机图形学vc++实验2:多边形边缘填充

1.

新建项目,命名为Fill

2.

在FillView.h中声明多边形数组,颜色
与绘制多边形函数DrawPolygon()
并在FillView.cpp中完成对函数的定义
在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

FillView.cpp初始化多边形坐标与颜色
在这里插入图片描述

然后在FillView.cpp的OnDraw中添加如下代码,使其在开始时绘制出多边形
在这里插入图片描述
写完后的FillView.cpp,FillView.h代码如下

// FillView.cpp : implementation of the CFillView class
//

#include "stdafx.h"
#include "Fill.h"

#include "FillDoc.h"
#include "FillView.h"

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

/
// CFillView

IMPLEMENT_DYNCREATE(CFillView, CView)

BEGIN_MESSAGE_MAP(CFillView, CView)
	//{
   
   {AFX_MSG_MAP(CFillView)
		// 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()

/
// CFillView construction/destruction

CFillView::CFillView()
{
	// TODO: add construction code here
	FillColor=RGB(0,0,0);
	Point[0]=CPoint(550,400);
	Point[1]=CPoint(350,600);
	Point[2]=CPoint(250,350);
	Point[3]=CPoint(350,50);
	Point[4]=CPoint(500,250);
	Point[5]=CPoint(600,50);
	Point[6]=CPoint(800,450);	
}

CFillView::~CFillView()
{
}

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

	return CView::PreCreateWindow(cs);
}

/
// CFillView drawing

void CFillView::OnDraw(CDC* pDC)
{
	CFillDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	// TODO: add draw code for native data here
	DrawPolygon();

}

/
// CFillView printing

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

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

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

/
// CFillView diagnostics

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

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

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

/
// CFillView message handlers
void CFillView::DrawPolygon()
{
	CClientDC dc(this);
	int m,n;
	for(int j=0;j<=6;j++)
	{
		m=j,n=j+1;
		if(7==n) n=0;
		dc.MoveTo(Point[m]);
		dc.LineTo(Point[n]);
	}
}
// FillView.h : interface of the CFillView class
//
/

#if !defined(AFX_FILLVIEW_H__8D88FCC0_6E53_4D6E_9EBA_CC16041FDEA5__INCLUDED_)
#define AFX_FILLVIEW_H__8D88FCC0_6E53_4D6E_9EBA_CC16041FDEA5__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000


class CFillView : public CView
{
protected: // create from serialization only
	CFillView();
	DECLARE_DYNCREATE(CFillView)

// Attributes
public:
	CFillDoc* GetDocument();

// Operations
public:
	void DrawPolygon();
	

// Overrides
	// ClassWizard generated virtual function overrides
	//{
   
   {AFX_VIRTUAL(CFillView)
	public:
	virtual void OnDraw(CDC* pDC);  // overridden to draw this view
	virtual BOOL PreCreateWindow(CREATESTRUCT& cs);
	protected:
	virtual BOOL OnPreparePrinting(CPrintInfo* pInfo);
	virtual void OnBeginPrinting(CDC* pDC, CPrintInfo* pInfo);
	virtual void OnEndPrinting(CDC* pDC, CPrintInfo* pInfo);
	//}}AFX_VIRTUAL

// Implementation
public:
	virtual ~CFillView();
#ifdef _DEBUG
	virtual void AssertValid() const;
	virtual void Dump(CDumpContext& dc) const;
#endif

protected:
	CPoint Point[7];
	COLORREF FillColor;

// Generated message map functions
protected:
	//{
   
   {AFX_MSG(CFillView)
		// NOTE - the ClassWizard will add and remove member functions here.
		//    DO NOT EDIT what you see in these blocks of generated code !
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()
};

#ifndef _DEBUG  // debug version in FillView.cpp
inline CFillDoc* CFillView::GetDocument()
   { return (CFillDoc*)m_pDocument; }
#endif

/

//{
   
   {AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before the previous line.

#endif // !defined(AFX_FILLVIEW_H__8D88FCC0_6E53_4D6E_9EBA_CC16041FDEA5__INCLUDED_)

到此可以初步编译运行
在这里插入图片描述

3.

添加菜单项目:ID_MENUFillColor
FillColor
在这里插入图片描述
声名其对应的函数
在这里插入图片描述
FillView.cpp为其添加代码
在这里插入图片描述
尝试编译运行,成功

4.

FillView.h添加有关屏幕空间最大坐标的定义
在这里插入图片描述
FillView.h声明函数,获取屏幕最大X,Y坐标
在这里插入图片描述
在FillView.Cpp中添加其实现
在这里插入图片描述

在这里插入图片描述

5.

实现核心功能函数:Draw()函数
即多边形边缘填充

首先,在FillView.cpp的代码中
添加如下语句,实现四舍五入
在这里插入图片描述
然后在FillView.h中声明函数Draw()
在这里插入图片描述
然后在FillView.cpp中添加对其的实现
在这里插入图片描述

void CFillView::Draw()
{

	COLORREF CBackColor=RGB(255,255,255);
	CClientDC dc(this);
	int m,n,ymin,ymax;	
	double x,y,k;
	for(int i=0;i<=6;i++)
	{
		m=i,n=i+1;
		if(7==n) n=0;
		k=(double(Point[m].x-Point[n].x))/(Point[m].y-Point[n].y);
		if(Point[m].y<Point[n].y)
		{
			ymin=Point[m].y;
			ymax=Point[n].y;
			x=Point[m].x;
		}
		else
		{
			ymin=Point[n].y;
			ymax=Point[m].y;
			x=Point[n].x;
		}
		for(y=ymin;y<ymax;y++)
		{			
			for(int j=ROUND(x);j<MaxX;j++)
			{
				if(dc.GetPixel(j,ROUND(y))==FillColor)
				{
					dc.SetPixel(j,ROUND(y),CBackColor);
				}
				else
				{
					dc.SetPixel(j,ROUND(y),FillColor);
				}		
			}
			x+=k;
		}		
	}	
}

6.

整合功能进入菜单项目
添加菜单项目
EdgeFill
ID_MENUEdgeFill

在这里插入图片描述
建立其对应的函数
在这里插入图片描述
在FillView.cpp中添加其实现
在这里插入图片描述
顺利运行

最终FillView.cpp与FileView.h如下

// FillView.cpp : implementation of the CFillView class
//

#include "stdafx.h"
#include "Fill.h"

#include "FillDoc.h"
#include "FillView.h"

#define   ROUND(a) int(a+0.5)

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

/
// CFillView

IMPLEMENT_DYNCREATE(CFillView, CView)

BEGIN_MESSAGE_MAP(CFillView, CView)
	//{
   
   {AFX_MSG_MAP(CFillView)
	ON_COMMAND(ID_MENUFillColor, OnMENUFillColor)
	ON_COMMAND(ID_MENUEdgeFill, OnMENUEdgeFill)
	//}}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()

/
// CFillView construction/destruction

CFillView::CFillView()
{
	// TODO: add construction code here
	FillColor=RGB(0,0,0);
	Point[0]=CPoint(550,400);
	Point[1]=CPoint(350,600);
	Point[2]=CPoint(250,350);
	Point[3]=CPoint(350,50);
	Point[4]=CPoint(500,250);
	Point[5]=CPoint(600,50);
	Point[6]=CPoint(800,450);	
}

CFillView::~CFillView()
{
}

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

	return CView::PreCreateWindow(cs);
}

/
// CFillView drawing

void CFillView::OnDraw(CDC* pDC)
{
	CFillDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	// TODO: add draw code for native data here
	DrawPolygon();

}

/
// CFillView printing

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

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

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

/
// CFillView diagnostics

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

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

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

/
// CFillView message handlers
void CFillView::GetMaxX()
{
	CRect rect;
	GetClientRect(rect);
    MaxX=rect.right;
}
void CFillView::GetMaxY()
{
	CRect rect;
	GetClientRect(rect);
    MaxY=rect.bottom;
}

void CFillView::Draw()
{
	COLORREF CBackColor=RGB(255,255,255);
	CClientDC dc(this);
	int m,n,ymin,ymax;	
	double x,y,k;
	for(int i=0;i<=6;i++)
	{
		m=i,n=i+1;
		if(7==n) n=0;
		k=(double(Point[m].x-Point[n].x))/(Point[m].y-Point[n].y);
		if(Point[m].y<Point[n].y)
		{
			ymin=Point[m].y;
			ymax=Point[n].y;
			x=Point[m].x;
		}
		else
		{
			ymin=Point[n].y;
			ymax=Point[m].y;
			x=Point[n].x;
		}
		for(y=ymin;y<ymax;y++)
		{			
			for(int j=ROUND(x);j<MaxX;j++)
			{
				if(dc.GetPixel(j,ROUND(y))==FillColor)
				{
					dc.SetPixel(j,ROUND(y),CBackColor);
				}
				else
				{
					dc.SetPixel(j,ROUND(y),FillColor);
				}		
			}
			x+=k;
		}		
	}	
}

void CFillView::DrawPolygon()
{
	CClientDC dc(this);
	int m,n;
	for(int j=0;j<=6;j++)
	{
		m=j,n=j+1;
		if(7==n) n=0;
		dc.MoveTo(Point[m]);
		dc.LineTo(Point[n]);
	}
}

void CFillView::OnMENUFillColor() 
{
	// TODO: Add your command handler code here
	CColorDialog ccd(FillColor);
	if(ccd.DoModal()==IDOK)
	{
		FillColor=ccd.GetColor();

	}	
}

void CFillView::OnMENUEdgeFill() 
{
	// TODO: Add your command handler code here
	RedrawWindow();
	
	GetMaxX();GetMaxY();
	Draw();
}

// FillView.h : interface of the CFillView class
//
/

#if !defined(AFX_FILLVIEW_H__8D88FCC0_6E53_4D6E_9EBA_CC16041FDEA5__INCLUDED_)
#define AFX_FILLVIEW_H__8D88FCC0_6E53_4D6E_9EBA_CC16041FDEA5__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000


class CFillView : public CView
{
protected: // create from serialization only
	CFillView();
	DECLARE_DYNCREATE(CFillView)

// Attributes
public:
	CFillDoc* GetDocument();

// Operations
public:
	void DrawPolygon();
	void GetMaxX();
	void GetMaxY();
	void Draw();
	

// Overrides
	// ClassWizard generated virtual function overrides
	//{
   
   {AFX_VIRTUAL(CFillView)
	public:
	virtual void OnDraw(CDC* pDC);  // overridden to draw this view
	virtual BOOL PreCreateWindow(CREATESTRUCT& cs);
	protected:
	virtual BOOL OnPreparePrinting(CPrintInfo* pInfo);
	virtual void OnBeginPrinting(CDC* pDC, CPrintInfo* pInfo);
	virtual void OnEndPrinting(CDC* pDC, CPrintInfo* pInfo);
	//}}AFX_VIRTUAL

// Implementation
public:
	virtual ~CFillView();
#ifdef _DEBUG
	virtual void AssertValid() const;
	virtual void Dump(CDumpContext& dc) const;
#endif

protected:
	int MaxX,MaxY;
	CPoint Point[7];
	COLORREF FillColor;

// Generated message map functions
protected:
	//{
   
   {AFX_MSG(CFillView)
	afx_msg void OnMENUFillColor();
	afx_msg void OnMENUEdgeFill();
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()
};

#ifndef _DEBUG  // debug version in FillView.cpp
inline CFillDoc* CFillView::GetDocument()
   { return (CFillDoc*)m_pDocument; }
#endif

/

//{
   
   {AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before the previous line.

#endif // !defined(AFX_FILLVIEW_H__8D88FCC0_6E53_4D6E_9EBA_CC16041FDEA5__INCLUDED_)

猜你喜欢

转载自blog.csdn.net/m0_62316271/article/details/130564657
今日推荐