NEFU计算机图形学实验二

区域填充:根据种子填充算法的基本原理,编写图形填充的应用程序,并运行演示填充效果。

// hiView.cpp : implementation of the CHiView class
//

#include "stdafx.h"
#include "hi.h"
#include <windows.h>
#include "hiDoc.h"
#include "hiView.h"
#include <afxtempl.h>

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

/
// CHiView

void EdgeMark(int x1,int y1,int x2,int y2,int bcolor,CDC*pDC)
{
	int x;
	float k, y = y1;
	k=1.0*(y2-y1)/(x2-x1);
	for(x=x1; x<=x2; x++)
	{
		pDC->SetPixel(x,(int)(y+0.5),bcolor);
		y=y+k;
	}
}
void ZhongZiTC4(int seedx, int seedy, int fcolor, int bcolor, CDC* pDC)
{
    CArray<CPoint, CPoint> stack;
    stack.Add(CPoint(seedx, seedy));

    while (stack.GetSize()>0)
    {
        CPoint current = stack.GetAt(stack.GetUpperBound());
        stack.RemoveAt(stack.GetUpperBound());

        int x = current.x;
        int y = current.y;

        if (pDC->GetPixel(x, y) != bcolor && pDC->GetPixel(x, y) != fcolor)
        {
            pDC->SetPixel(x, y, fcolor);
            stack.Add(CPoint(x, y + 1));
            stack.Add(CPoint(x, y - 1));
            stack.Add(CPoint(x - 1, y));
            stack.Add(CPoint(x + 1, y));
        }
    }
}
void SeedFill4 (int cnt,POINT*pts,int seedx,int seedy,int fcolor,int bcolor,CDC*pDC)
{
	int i; 
	POINT v1,v2;
	for(i=0; i<cnt-1; i++)
	{
		v1=pts[i]; 
		v2=pts[i+1];
		EdgeMark(v1.x,v1.y,v2.x,v2.y,bcolor,pDC);
	}
	ZhongZiTC4 (seedx,seedy,fcolor,bcolor,pDC);
}

IMPLEMENT_DYNCREATE(CHiView, CView)


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

/
// CHiView construction/destruction

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

}

CHiView::~CHiView()
{
}

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

	return CView::PreCreateWindow(cs);
}

/
// CHiView drawing

 void CHiView::OnDraw(CDC* pDC)
{
    // 设置画笔颜色
    CPen pen(PS_SOLID, 1, RGB(0, 0, 255));
    pDC->SelectObject(&pen);

    // 绘制矩形
    pDC->Rectangle(25, 25, 100, 75);

    // 设置填充颜色
    CBrush brush(RGB(139, 0, 0));
    pDC->SelectObject(&brush);

    // 设置种子点和填充颜色
    int seedx = 50, seedy = 50;
    int fcolor = RGB(139, 0, 0), bcolor = RGB(0, 0, 255);

    // 填充种子点
    ZhongZiTC4(seedx, seedy, fcolor, bcolor, pDC);
}

void CHiView::ZhongZiTC4(int seedx, int seedy, COLORREF fcolor, COLORREF bcolor, CDC* pDC)
{
    CArray<CPoint, CPoint> stack;
    stack.Add(CPoint(seedx, seedy));

    while (stack.GetSize() > 0)
    {
        CPoint current = stack.GetAt(stack.GetUpperBound());
        stack.RemoveAt(stack.GetUpperBound());

        int x = current.x;
        int y = current.y;

        if (pDC->GetPixel(x, y) != bcolor && pDC->GetPixel(x, y) != fcolor)
        {
            pDC->SetPixel(x, y, fcolor);
            stack.Add(CPoint(x, y + 1));
            stack.Add(CPoint(x, y - 1));
            stack.Add(CPoint(x - 1, y));
            stack.Add(CPoint(x + 1, y));
        }
    }
}

/
// CHiView printing

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

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

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

/
// CHiView diagnostics

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

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

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

/
// CHiView message handlers
// hiView.h : interface of the CHiView class
//
/

#if !defined(AFX_HIVIEW_H__2B74F3D9_013A_447B_9B8C_1E81ABDB4769__INCLUDED_)
#define AFX_HIVIEW_H__2B74F3D9_013A_447B_9B8C_1E81ABDB4769__INCLUDED_

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


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

// Attributes
public:
	CHiDoc* GetDocument();

// Operations
public:
	void ZhongZiTC4(int seedx, int seedy, COLORREF fcolor, COLORREF bcolor, CDC* pDC);

// Overrides
	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CHiView)
	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 ~CHiView();
#ifdef _DEBUG
	virtual void AssertValid() const;
	virtual void Dump(CDumpContext& dc) const;
#endif

protected:

// Generated message map functions
protected:
	//{{AFX_MSG(CHiView)
		// 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 hiView.cpp
inline CHiDoc* CHiView::GetDocument()
   { return (CHiDoc*)m_pDocument; }
#endif

/

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

#endif // !defined(AFX_HIVIEW_H__2B74F3D9_013A_447B_9B8C_1E81ABDB4769__INCLUDED_)

直线段裁剪:根据编码裁剪算法的基本原理,编写直线段裁剪的应用程序,并运行演示裁剪效果。

// zhiView.cpp : implementation of the CZhiView class
//

#include "stdafx.h"
#include "zhi.h"

#include "zhiDoc.h"
#include "zhiView.h"

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

/
// CZhiView
#define LEFT 1
#define RIGHT 2
#define BOTTOM 4
#define TOP 8

void Encode(int x,int y,int *code,int XL,int XR,int YB,int YT)
	{
		int c=0;
		if(x<XL)c=c|LEFT;
			else if(x>XR)c=c|RIGHT;
		if(y<YB)c=c|BOTTOM;
			else if(y>YT)c=c|TOP;
			(*code)=c;

	}

	void C_S_Line(POINT &p1,POINT &p2,int XL,int XR,int YB,int YT)
	{
		int x1,x2,y1,y2,x,y,code1,code2,code;
		x1=p1.x;x2=p2.x,y1=p1.y;y2=p2.y;
		Encode(x1,y1,&code1,XL,XR,YB,YT);
		Encode(x2,y2,&code2,XL,XR,YB,YT);
		while(code1!=0||code2!=0){
			if((code1&code2)!=0)return;
			code=code1;
			if(code1==0)
				code=code2;
			if((LEFT&code)!=0)
			{
				x=XL;y=y1+(y2-y1)*(XL-x1)/(x2-x1);
			}
			else if((code&RIGHT)!=0)
			{x=XR; y=y1+(y2-y1)*(XR-x1)/(x2-x1);}
			else if((code & BOTTOM)!=0)
			{y=YB; x=x1+(x2-x1)*(YB-y1)/(y2-y1);}
			else if((TOP & code)!=0)
			{	y=YT; x=x1+(x2-x1)*(YT-y1)/(y2-y1);}
			if(code==code1)
			{x1=x; y1=y; Encode(x,y,&code1,XL,XR,YB,YT);}
			else
			{x2=x; y2=y; Encode(x,y,&code2,XL,XR,YB,YT);}
		}
		p1.x=x1; p1.y=y1; p2.x= x2; p2.y=y2;
		
	}

IMPLEMENT_DYNCREATE(CZhiView, CView)

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

/
// CZhiView construction/destruction

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

}

CZhiView::~CZhiView()
{
}

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

	return CView::PreCreateWindow(cs);
}

/
// CZhiView drawing

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

	int sig,XL,XR,YB,YT;
	float x1,y1,x2,y2;
	CPoint wMin(150,100),wMax(300,300),p1(100,80),p2(300,330);
	int color;
	x1=(float)p1.x;y1=p1.y;
	x2=p2.x;y2=p2.y;
	XL=wMin.x;XR=wMax.x;
	YB=wMin.y;YT=wMax.y;

	color=RGB(28,28,28);
	CPen pen1(PS_SOLID,1,color);
	pDC->SelectObject(&pen1);
	pDC->Rectangle(XL,YB,XR,YT);

	color=RGB(255,0,0);
	CPen pen2(PS_DOT,1,color);
	pDC->SelectObject(&pen2);
	pDC->MoveTo(x1,y1);
	pDC->LineTo(x2,y2);
	C_S_Line(p1,p2,XL,XR,YB,YT);

	color=RGB(0,0,255);
	CPen pen3(PS_SOLID,3,color);
	pDC->SelectObject(&pen3);
	pDC->MoveTo(p1.x,p1.y);
	pDC->LineTo(p2.x,p2.y);
	
}

/
// CZhiView printing

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

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

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

/
// CZhiView diagnostics

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

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

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

/
// CZhiView message handlers

多边形裁剪:根据逐边裁剪算法的基本原理,编写多边形裁剪的应用程序,并运行演示裁剪效果。

// duo1View.cpp : implementation of the CDuo1View class
//

#include "stdafx.h"
#include "duo1.h"

#include "duo1Doc.h"
#include "duo1View.h"

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

/
// CDuo1View
void TestIntersect(int edge, int tyke, POINT p1, POINT p2, POINT &pout, int &yes, int &isIn, CDC* pDC)
{
	float dx, dy, m;
	isIn = 0;
	yes = 0;
	dy = p2.y - p1.y;
	dx = p2.x - p1.x;
	m = dy / dx;
	switch(tyke)
	{
	case 1:  /*right*/
		if(p2.x <= edge)
		{
			isIn = 1;
			if(p1.x > edge)
				yes = 1;
		}
		else if(p1.x <= edge)
			yes = 1;
		break;
	case 2:  /*bottom*/
		if(p2.y >= edge)
		{
			isIn = 1;
			if(p1.y < edge)
				yes = 1;
		}
		else if(p1.y >= edge)
			yes = 1;
		break;
	case 3:  /*left*/
		if(p2.x >= edge)
		{
			isIn = 1;
			if(p1.x < edge)
				yes = 1;
		}
		else if(p1.x >= edge)
			yes = 1;
		break;
	case 4:  /*top*/
		if(p2.y <= edge)
		{
			isIn = 1;
			if(p1.y > edge)
				yes = 1;
		}
		else if(p1.y <= edge)
			yes = 1;
		break;
	default: 
		break;
	}
	if(yes)
	{
		if((tyke == 1) || (tyke == 3))
		{
			pout.x = edge;
			pout.y = p1.y + m * (pout.x - p1.x);
		}
		if((tyke ==2) || (tyke == 4))
		{
			pout.y = edge;
			pout.x = p1.x + (pout.y - p1.y) / m;
		}
	}
}
void clipSingleEdge(int edge, int tyke, int nin, POINT pin[50], int &nout, POINT pout[50], CDC* pDC)
{
	int i, k = 0, yes, isIn;
	POINT p, tp, pinters;
	p.x = pin[nin - 1].x;
	p.y = pin[nin - 1].y;
	for(i = 0; i < nin; i ++)
	{
		TestIntersect(edge, tyke, p, pin[i], pinters, yes, isIn, pDC);
	    if(yes)
		{
			pout[k].x = pinters.x;
			pout[k].y = pinters.y;
			k ++;
		}
		if(isIn)
		{
			pout[k].x = pin[i].x;
			pout[k].y = pin[i].y;
			k ++;
		}
		p.x = pin[i].x;
		p.y = pin[i].y;
	}
    nout = k;
}
void ClipEdgePolygon(POINT ps[50], int &n, int XL, int XR, int YB, int YT, CDC* pDC)
{
    int n1 = 0;
    POINT pt[50];
    clipSingleEdge(XR, 1, n, ps, n1, pt, pDC);
    clipSingleEdge(YB, 2, n1, pt, n, ps, pDC);
    clipSingleEdge(XL, 3, n, ps, n1, pt, pDC);
    clipSingleEdge(YT, 4, n1, pt, n, ps, pDC);
}


IMPLEMENT_DYNCREATE(CDuo1View, CView)

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

/
// CDuo1View construction/destruction

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

}

CDuo1View::~CDuo1View()
{
}

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

	return CView::PreCreateWindow(cs);
}

/
// CDuo1View drawing

void CDuo1View::OnDraw(CDC* pDC)
{
    // 获取文档指针
    CDuo1Doc* pDoc = static_cast<CDuo1Doc*>(GetDocument());
    ASSERT_VALID(pDoc);
    
    // 绘制代码
    int i, XL, XR, YB, YT, n = 3, nout = 0;
    int color;
    CPoint ply[50] = {CPoint(75, 50), CPoint(160, 120), CPoint(200, 250)};
    CPoint wMin(100, 100), wMax(200, 200);
    XL = wMin.x;
    YB = wMin.y;
    XR = wMax.x;
    YT = wMax.y;

    // 绘制边框
    color = RGB(28, 28, 28);
    CPen pen1(PS_SOLID, 1, color);
    pDC->SelectObject(&pen1);
    pDC->Rectangle(XL, YB, XR, YT);

    // 绘制多边形
    color = RGB(255, 0, 0);
    CPen pen2(PS_DASHDOT, 1, color);
    pDC->SelectObject(&pen2);
    pDC->Polygon(ply, n);

    // 多边形裁剪
    ClipEdgePolygon(ply, n, XL, XR, YB, YT, pDC);

    // 绘制裁剪后的多边形
    color = RGB(0, 0, 255);
    CPen pen3(PS_SOLID, 1, color); // 使用蓝色画笔
    pDC->SelectObject(&pen3);
    pDC->Polygon(ply, n); // 绘制裁剪后的多边形
}
/
// CDuo1View printing

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

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

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

/
// CDuo1View diagnostics

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

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

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

/
// CDuo1View message handlers

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值