void CMy3View::BresenhamLine(int x0, int y0, int x1, int y1)
{
int dx,dy,d,step,flagX,flagY,Dir_of_step,Not_dir;
dx=x1-x0;
dy=y1-y0;
double scale=dy*1.0/dx;
x=new float[1000];
y=new float[1000];
x[0]=x0;
y[0]=y0;
if(scale<0)
step=-1;
else
step=1;
//初始化总步长num,主方向Dir_of_step,非主方向Not_dir,X方向的标志位flagX,Y方向的标志位flagY
if(fabs(scale)<=1)
{
num=abs(dx);
Dir_of_step=dx;
Not_dir=dy;
flagX=1;
flagY=0;
}
else
{
num=abs(dy);
Dir_of_step=dy;
Not_dir=dx;
flagX=0;
flagY=1;
}
//初始化d
d=2*step*Not_dir-Dir_of_step;
//进行判断
for(int i=1;i<=num;i++)
{
if(d>=0)
{
y[i]=y[i-1]+abs(Dir_of_step)/Dir_of_step*pow(step*1.0,flagX*1.0);//Y方向的增量
x[i]=x[i-1]+abs(Dir_of_step)/Dir_of_step*pow(step*1.0,flagY*1.0);//X方向的增量
d+=abs(Dir_of_step)/Dir_of_step*(2*step*Not_dir-2*Dir_of_step);
}
else
{
y[i]=y[i-1]+abs(Dir_of_step)/Dir_of_step*flagY;//Y方向的增量
x[i]=x[i-1]+abs(Dir_of_step)/Dir_of_step*flagX;//X方向的增量
d+=abs(Dir_of_step)/Dir_of_step*2*step*Not_dir;
}
}
m_press=true;
}
void CMy3View::BresenhamLine_draw(CDC *pDC)
{
COLORREF color;
color=RGB(0,200,0);
for(int i=0;i<=num;i++)
pDC->SetPixel(floor(x[i]+0.5),floor(y[i]+0.5),color);
}