void　MidBresenhamLine(int　x0,int　y0,int　x1,int　y1,int　color){　int　dx,dy,d,UpIncre,DownIncre,x,y;if(x0>x1){x=x1;x1=x0;x0=x;y=y1;y1=y0;y0=y;}x=x0;y=y0;dx=x1-x0;dy=y1-y0;d=dx-2*dy;UpIncre=2*dx-2*dy;DownIncre=-2*dy;while(x<=x1){putpixel(x,y,color);X++;if(d<0){y++;d+=UpIncre;}else　d+=DownIncre;}}程序调试及运行结果的自我分析与自我评价//　testView.cpp　:　implementation　of　the　CTestView　class#include　"stdafx.h"#include　"test.h"#include　"testDoc.h"#include　"testView.h"#include　　//　ifstream、ofstream等位于其中#include　#include　　//　string类型需要#include　"DlgInput.h"　//CDlgInput类的头文件using　namespace　std;#ifdef　_DEBUG#define　new　DEBUG_NEW#undef　THIS_FILEstatic　char　THIS_FILE[]　=　__FILE__;#endif//　CTestViewIMPLEMENT_DYNCREATE(CTestView,　CView)BEGIN_MESSAGE_MAP(CTestView,　CView)　//{{AFX_MSG_MAP(CTestView)　ON_COMMAND(ID_MENUITEM32771,　OnMenuitem32771)　ON_COMMAND(ID_MENUBRESENHAMLINE,　OnMenubresenhamline)　ON_COMMAND(ID_MENUCLEARVIEW,　OnMenuclearview)　ON_COMMAND(ID_FILE_OPEN,　OnFileOpen)　ON_COMMAND(ID_FILE_SAVE,　OnFileSave)　//}}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/destructionCTestView::CTestView(){　//　TODO:　add　construction　code　here　m_nFlag　=　-1;　//　不是任何绘图类型}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　drawingvoid　CTestView::OnDraw(CDC*　pDC){　CTestDoc*　pDoc　=　GetDocument();　ASSERT_VALID(pDoc);　//　TODO:　add　draw　code　for　native　data　here　if(1==m_nFlag)　//　中点画线　{　MidPointLine(m_X0,　m_Y0,　m_X1,　m_Y1,　RGB(255,0,0)　);　}　else　if(2==m_nFlag)　//　Bresenham画线　{　BresenhamLine(m_X0,　m_Y0,　m_X1,　m_Y1,　RGB(0,255,0)CTestView　printingBOOL　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　_DEBUGvoid　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　handlersvoid　CTestView::OnMenuitem32771()　{　//　TODO:　Add　your　command　handler　code　here　m_nFlag　=　1;　//　中点画线　CDlgInput　dlg;　if(IDOK==dlg.DoModal())　{　m_X0=dlg.m_nX0;　m_Y0=dlg.m_nY0;　m_X1=dlg.m_nX1;　m_Y1=dlg.m_nY1;　RedrawWindow();　//重绘窗口　}　}void　CTestView::OnMenubresenhamline()　{　//　TODO:　Add　your　command　handler　code　here　m_nFlag　=　2;　//　Bresenham画线　CDlgInput　dlg;　if(IDOK==dlg.DoModal())　{　m_X0=dlg.m_nX0;　m_Y0=dlg.m_nY0;　m_X1=dlg.m_nX1;　m_Y1=dlg.m_nY1;　RedrawWindow();　//重绘窗口　}　}//　算法:　中点画线//　输入:　起点(x0,y0),终点(x1,y1);//　输入要求x0<=x1;void　CTestView::MidPointLine(　int　x0,　int　y0,　int　x1,　int　y1,　int　color　){　CDC　*　pDC=GetDC();　int　a,b,d0,d1,d2,d3,d4,d5,d,x,y;　a=y0-y1;　b=x1-x0;　//　之前的设置已经保证始终有x1>=x0　d=2*a+b;　d0=2*a-b;　d1=2*a;　d2=2*(a+b);　d3=2*b;　d4=2*(a-b);　d5=a-2*b;　x=x0;　y=y0;　pDC->SetPixel(x,y,color);　if(x==x1)　//　斜率k为无穷大　{　if(y<=y1)　{　while(y<=y1)　{　pDC->SetPixel(x,y,color);　y++;　}　}　else　{　while(y>=y1)　{　pDC->SetPixel(x,y,color);　y-if　斜率k为无穷大　else　//　斜率k为有限值　{//　double　k=-a/b;//　if(　k+1>1e-6　&&　k-1<1e-6　||　fabs(k-1)<1e-6　||　fabs(k+1)<1e-6)　//　|k|<=1(即:-1<=　k　<=1),与1e-6比较是浮点数比较方法　if(　-b<=-a　&&　-a<=b　)　//　用浮点数比较在|k|=1.0f时容易出问题,所以直接用整数比较(将斜率k转换为a与b的比较;之前的设置已经保证b为正数)　{　if(y<=y1)　{　while(xSetPixel(x,y,color);　}　}　else　{　while(xSetPixel(x,y,color)if(　|k|<=1　)　else　//　|k|>1　{　if(y<=y1)　{　while(xSetPixel(x,y,color);　}　}　else　{　while(xSetPixel(x,y,color)else(　|k|>1　)　}//　else　斜率k为有限值　ReleaseDC(pDC);}//　算法:　Bresenham画线//　输入:　起点(x0,y0),终点(x1,y1);//　输入要求x0<=x1;void　CTestView::BresenhamLine(　int　x0,　int　y0,　int　x1,　int　y1,　int　color　){　CDC　*　pDC=GetDC();　int　x,y,dx,dy,e;dx=x1-x0;dy=y1-y0;e=-dx;x=x0;y=y0;while(x<=x1){　pDC->SetPixel(x,y,color);　x++;　e=e+2*dy;　if(e>0)　{y++;　e=e-2*dx;　}}}void　CTestView::OnMenuclearview()　{　//　TODO:　Add　your　command　handler　code　here　m_X0=0;　m_Y0=0;　m_X1=0;　m_Y1=0;　RedrawWindow();//重绘窗口　}//　打开过去保存的文件,该文件包含直线的端点坐标void　CTestView::OnFileOpen(){　//　TODO:　在此添加命令处理程序代码　if(　m_nFlag!=1　&&　m_nFlag!=2　)　{　MessageBox("请先在菜单中选择绘制直线的方法!","提示",MB_ICONWARNING);　return;　}　CFileDialog　dlgFile　(TRUE,　_T("txt"),　_T(""),　OFN_FILEMUSTEXIST|　OFN_HIDEREADONLY,　_T("线段端点坐标文件(*.txt)|*.txt||"),　this);　if(　IDOK　==　dlgFile.DoModal())　{　CString　fileName　=　dlgFile.GetFileName();　ifstream　rFile;　rFile.open(fileName,ios::in);　if　(　!　rFile.is_open()　)　{　MessageBox("文件打开失败!","提示",MB_ICONWARNING);　return;　}　CString　strLine0;　string　strLine;　int　nX,nY;　//　起点、终点,两个坐标　rFile>>strLine;　strLine0　=　strLine.c_str();　nX　=　atoi(　strLine0.Left(　strLine0.Find(解析文件,如“220,221”表示一个点的x、y坐标　nY　=　atoi(　strLine0.Mid(　strLine0.Find(",")+1　)　);　m_X0　=　nX;　m_Y0　=　nY;　rFile>>strLine;　strLine0　=　strLine.c_str();　nX　=　atoi(　strLine0.Left(　strLine0.Find(nY　=　atoi(　strLine0.Mid(　strLine0.Find(",")+1　)　);　m_X1　=　nX;　m_Y1　=　nY;　RedrawWindow();　rFile.close(保存当前视图上绘制的所有直线的端点坐标void　CTestView::OnFileSave(){　//　TODO:　在此添加命令处理程序代码　CFileDialog　dlgFile(FALSE,　_T("txt"),　_T(""),　OFN_OVERWRITEPROMPT,　_T("线段端点坐标文件(*.txt)|*.txt||"),　this);　if(　IDOK　==　dlgFile.DoModal())　//　保存文件　{　CString　strFileName　=　dlgFile.GetFileName();　//　包含完整路径的文件名称　ofstream　wFile;　wFile.open(strFileName,ios::out|ios::ate|ios::app);　if　(　!　wFile.is_open()　)　{　MessageBox(strFileName+"文件创建失败!","提示",MB_ICONWARNING);　return;　}　wFile<