●TestView.cpp
// TestView.cpp : implementation of the CTestView class //
#include \#include \
#include \#include \
#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_MENUMbline, OnMENUMbline) ON_COMMAND(ID_MENUMbcircle, OnMENUMbcircle) //}}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() {
- 13 -
}
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
- 14 -
{
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::Mbline()//Bresenham函数 {
CClientDC dc(this);
COLORREF rgb=RGB(0,0,255);//定义直线颜色为蓝色 double x,y,d,k;
x=x0;y=y0;k=(y1-y0)/(x1-x0);d=0.5-k; //定义x,y,k及d的初始值 for(x=x0;x<=x1;x++) { dc.SetPixel(ROUND(x),ROUND(y),rgb); if(d<0) { y++;
d+=1-k;//d<0时,取右上方像素点,d的增量为1-k } else
d-=k;//d>=0时,取正右方向像素点,d的增量为-k } }
void CTestView::OnMENUMbline()//菜单函数 {
// TODO: Add your command handler code here InputDlg dlg;
if(dlg.DoModal()==IDOK) { x0=dlg.m_x0;//变量x0即为InputDlg中的m_x0
- 15 -
y0=dlg.m_y0;//变量y0即为InputDlg中的m_y0 x1=dlg.m_x1;//变量x1即为InputDlg中的m_x1 y1=dlg.m_y1;//变量y1即为InputDlg中的m_y1 }
AfxGetMainWnd()->SetWindowText(\实验:直线中点Bresenham算法\ RedrawWindow(); Mbline(); }
void CTestView::GetMaxX()//得到客户区的最大横坐标 {
CRect Rect;
GetClientRect(&Rect); MaxX=Rect.right; }
void CTestView::GetMaxY()//得到客户区最大纵坐标 {
CRect Rect;
GetClientRect(&Rect); MaxY=Rect.bottom; }
void CTestView::Mbcircle()//Bresenham算法 {
double x,y,d;
d=1.25-R;x=0;y=R; //定义d,x,y的初始值 for(x=0;x
void CTestView::CirclePoint(double x, double y)//八分法画圆子函数 {
CClientDC dc(this);
COLORREF rgb=RGB(0,0,255);//定义圆的颜色
- 16 -
dc.SetPixel(ROUND(x)+MaxX/2,ROUND(y)+MaxY/2,rgb);//x,y dc.SetPixel(ROUND(y)+MaxX/2,ROUND(x)+MaxY/2,rgb);//y,x dc.SetPixel(ROUND(y)+MaxX/2,-ROUND(x)+MaxY/2,rgb);//y,-x dc.SetPixel(ROUND(x)+MaxX/2,-ROUND(y)+MaxY/2,rgb);//x,-y dc.SetPixel(-ROUND(x)+MaxX/2,-ROUND(y)+MaxY/2,rgb);//-x,-y dc.SetPixel(-ROUND(y)+MaxX/2,-ROUND(x)+MaxY/2,rgb);//-y,-x dc.SetPixel(-ROUND(y)+MaxX/2,ROUND(x)+MaxY/2,rgb);//-y,x dc.SetPixel(-ROUND(x)+MaxX/2,ROUND(y)+MaxY/2,rgb);//-x,y }
void CTestView::OnMENUMbcircle()//菜单函数 {
// TODO: Add your command handler code here InputDlg2 dlg;
if(dlg.DoModal()==IDOK) { R=dlg.m_r;//R即为InputDlg2中的圆的半径r }
AfxGetMainWnd()->SetWindowText(\实验:圆中点Bresenham算法\ RedrawWindow();
GetMaxX();GetMaxY(); Mbcircle(); }
- 17 -