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

📄 testview.cpp

📁 Bresenham算法
💻 CPP
字号:
// TestView.cpp : implementation of the CTestView class
//

#include "stdafx.h"
#include "Test.h"

#include "TestDoc.h"
#include "TestView.h"
#define   ROUND(a) int(a+0.5) //四舍五入
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CTestView

IMPLEMENT_DYNCREATE(CTestView, CView)

BEGIN_MESSAGE_MAP(CTestView, CView)
	//{{AFX_MSG_MAP(CTestView)
	ON_COMMAND(ID_MENUMbellipse, OnMENUMbellipse)
	//}}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()

/////////////////////////////////////////////////////////////////////////////
// CTestView construction/destruction

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

}

CTestView::~CTestView()
{
}

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

	return CView::PreCreateWindow(cs);
}

/////////////////////////////////////////////////////////////////////////////
// CTestView drawing

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

/////////////////////////////////////////////////////////////////////////////
// CTestView printing

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

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

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

/////////////////////////////////////////////////////////////////////////////
// CTestView diagnostics

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

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

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

/////////////////////////////////////////////////////////////////////////////
// CTestView message handlers
void CTestView::GetMaxX()//得到客户区的最大横坐标
{
	CRect Rect;
	GetClientRect(&Rect);
	MaxX=Rect.right;	
}

void CTestView::GetMaxY()//得到客户区最大纵坐标
{
	CRect Rect;
	GetClientRect(&Rect);
	MaxY=Rect.bottom;	
}

void CTestView::Mbellipse()//椭圆的Bresenham算法
{
	double x,y,d1,d2;
	x=0;y=b;
	d1=b*b+a*a*(-b+0.25);
	EllipsePoint(x,y);
	while(b*b*(x+1)<a*a*(y-0.5))//椭圆AC弧段
	{
		if (d1<0)
		{
			d1+=b*b*(2*x+3);
		}
		else
		{
			d1+=b*b*(2*x+3)+a*a*(-2*y+2);
			y--;
		}
		x++;
		EllipsePoint(x,y);
	}
	d2=b*b*(x+0.5)*(x+0.5)+a*a*(y-1)*(y-1)-a*a*b*b;//椭圆CB弧段
	while(y>0)
	{
		if (d2<0)
		{
			d2+=b*b*(2*x+2)+a*a*(-2*y+3);
			x++;
		}
		else
		{
			d2+=a*a*(-2*y+3);
			
		}
		y--;
		EllipsePoint(x,y);
	}
}
       
void CTestView::EllipsePoint(double x, double y)//四分法画椭圆
{
	CClientDC dc(this);
	COLORREF  rgb=RGB(0,0,255);//定义椭圆的颜色
	dc.SetPixel(ROUND(x)+MaxX/2,ROUND(y)+MaxY/2,rgb);
	dc.SetPixel(ROUND(-x)+MaxX/2,ROUND(y)+MaxY/2,rgb);
	dc.SetPixel(ROUND(x)+MaxX/2,ROUND(-y)+MaxY/2,rgb);
	dc.SetPixel(ROUND(-x)+MaxX/2,ROUND(-y)+MaxY/2,rgb);
}

void CTestView::OnMENUMbellipse()//菜单函数 
{
	// TODO: Add your command handler code here
	InputDlg dlg;
	if(dlg.DoModal()==IDOK)
	{
		a=dlg.m_a;
		b=dlg.m_b;
	}
	AfxGetMainWnd()->SetWindowText("案例4:椭圆中点Bresenham算法");
	RedrawWindow();
	GetMaxX();GetMaxY();
	Mbellipse();	
}


⌨️ 快捷键说明

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