图形学问题,再线等待。
我用Bresenham算法画连续线时,线段不能相互衔接,错开很多。为什么?
代码如下:
void CMapViewView::Bre_SETLINE(long x1, long y1, long x2, long y2, CDC *pDC, COLORREF rgb)
{
CRect rect;
GetClientRect(&rect);
CPoint pt;
int x,y,dx,dy,i;
float k,e;
dx=(int)(x2-x1);
if(x1==x2)
return;
dy=(int)(y2-y1);
k=dy/dx;
e=-dx;
x=(int)x1;
y=(int)y1;
for(i=0;i<=dx;i++)
{
pt.x=x;
pt.y=y;
if(rect.PtInRect(pt))
{
pDC->SetPixel(x,y,egb);
}
x=x+1;
e=e+2*dy;
if(e>=0)
{
y=y+1;
e=e-2*dx;
}
}
}
例如作如下调用:
Bre_SETLINE(0,0,200,300, pDC,RGB(255,0,0));
Bre_SETLINE(200,300,800,500, pDC,RGB(255,0,0));
两条线却不能连接在一起。为什么。