ÄÚÈÝ·¢²¼¸üÐÂʱ¼ä : 2026/6/14 3:33:03ÐÇÆÚÒ» ÏÂÃæÊÇÎÄÕµÄÈ«²¿ÄÚÈÝÇëÈÏÕæÔĶÁ¡£
¼ÆËã»úͼÐÎѧʵÑ鱨¸æ
ʵÑé1 ʹÓûÏßËã·¨£¬»æÖÆÖ±Ïß¶Î
ÐÕÃû ¶Å°¬Á« ϵ±ð°à¼¶ µØÐÅ101 ѧºÅ 1008140925 ʵÑéÈÕÆÚ 2011.10.10 Ö¸µ¼½Ìʦ ʵÑé³É¼¨ Ò»£® ʵÑéÄ¿µÄ¼°ÒªÇó
£¨1£©ÕÆÎÕͼÐÎѧÖг£ÓõÄÈýÖÖ»ÏßËã·¨£ºÊýֵ΢·Ö·¨¡¢Öеã»Ïß·¨ºÍBresenham»ÏßËã·¨¡£ £¨2£©ÕÆÎÕ»æÖÆÖ±ÏߵijÌÐòÉè¼Æ·½·¨¡£
£¨3£©ÕÆÎÕʹÓÃÎļþÀ´±£´æÖ±Ï߶εķ½·¨¡£ £¨4£©ÕÆÎÕ´ÓÎı¾ÎļþÖлָ´³öÖ±Ïߵķ½·¨¡£
¶þ£® ʵÑéÄÚÈÝ
ʹÓÃVC++ 6.0¿ª·¢»·¾³£¬·Ö±ðʵÏÖÖеã»ÏßËã·¨ºÍBresenham»ÏßËã·¨£¬»æÖÆÖ±Ïߣ¨×¢Ò⣬²»ÄÜʹÓÃVCÖÐÒÑÓеĻæÖÆÖ±Ïߵĺ¯Êý£©£¬²¢ÒÔÎı¾ÎļþµÄÐÎʽ±£´æ»æÖƵĽá¹û£¬¿ÉÒÔ´ÓÎı¾ÎļþÖлָ´³öÒÔǰ»æÖƹýµÄÖ±Ïß¡£
Èý£® Ëã·¨Éè¼ÆÓë·ÖÎö
ÊäÈëP0(X0,Y0) ºÍP1£¨X1£¬Y1£© ¼ÆËã³õʼֵ¡÷x£¬¡÷y d=¡÷x-2¡÷y,x=X0,y=Y0 (x,y) ¸üÐÂΪ£¨x+1,y+1£©,d¸üÐÂΪd+2¡÷x-2¡÷y (x,y)¸üÐÂΪ£¨x+1,y£©,d¸üÐÂΪd-2¡÷y ½áÊø
BresenhamËã·¨»æÖÆÖ±ÏߵijÌÐò£¨½ö°üº¬ÕûÊýÔËË㣩¡£ 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 \#include \#include \#include \#include
#include
#include \//CDlgInputÀàµÄÍ·Îļþ
using namespace std; #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_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/destruction
CTestView::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 drawing
void 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 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::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)