Bresenham算法画直线个别区域不能显示问题
计算机图形实验 Bresenham算法画直线自己编的代码运行后部分区域不能显示(第一象限0到45°,第三象限225到270°)调试好久不能解决 本人新手 求大神指导
C/C++ code
void CGaowei_drawlineView::OnLButtonDown(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
c1=point;
HCURSOR hCur = LoadCursor(NULL,IDC_CROSS);
::SetCursor(hCur);
flag=1;
CView::OnLButtonDown(nFlags, point);
}
void CGaowei_drawlineView::OnLButtonUp(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
HCURSOR hCur = LoadCursor( NULL , IDC_ARROW) ;
::SetCursor(hCur);
flag=0;
c2=point;
// CDC *pDC=GetDC(); /* CWnd中的成员函数 */
//pDC->MoveTo(c1);
//pDC->LineTo(c2);
drawing(c1,c2);
CView::OnLButtonUp(nFlags, point);
}
void CGaowei_drawlineView::OnMouseMove(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
if(flag==1){
HCURSOR hCur = LoadCursor( NULL , IDC_CROSS ) ;
::SetCursor(hCur);
// CDC *dc=GetDC();
//dc->SetROP2(R2_NOT);
//draw_line(c1,last);
//draw_line(c1,point);
//dc->MoveTo(c1);
//dc->LineTo(last);
//dc->MoveTo(c1);
//dc->LineTo(point);
}
last=point;
CView::OnMouseMove(nFlags, point);
}
void CGaowei_drawlineView::drawing(CPoint c1, CPoint c2)
{
bool flag=1;
if(c1.x>c2.x){fanzhuan_points(c1,c2);}
if(abs(c1.y-c2.y)>abs(c1.x-c2.x)){flag=0;fanzhuan_single(c1);fanzhuan_single(c2);}
int dx=c2.x-c1.x;
int dy=c2.y-c1.y;
int dx2=abs(dx)*2;
int dy2=abs(dy)*2;
int erro=-abs(dx);
int ix=c1.x;
int iy=c1.y;
int inc=1;
CDC *pDC=GetDC();
int c=RGB(255,0,0);
if(dy>0)inc=1;
if(dy<=0)inc=-1;
for(ix=c1.x;ix<c2.x;ix++)
{
if(flag==1)pDC->SetPixel(ix,iy,c);
if(flag==0)pDC->SetPixel(iy,ix,c);
erro=erro+dy2;
if(erro>0)
{
iy+=inc;
erro=erro-dx2;
}
}
}
void CGaowei_drawlineView::fanzhuan_points(CPoint &c1, CPoint& c2)
{
CPoint temp=c1;
c1=c2;
c2=temp;
}
void CGaowei_drawlineView::fanzhuan_single(CPoint &c1)
{
int temp=c1.x;
c1.x=c1.y;
c1.y=temp;
}