vc++画直线
void CAbcView::myBresenhamLine(int x1, int y1, int x2, int y2){
CDC* myDDC;
myDDC=GetDC();
int x,y;
float p,k,dx,dy;
if(x1>x2||y1>y2)
{
x=x1;
y=y1;
x1=x2;
y1=y2;
x2=x;
y2=y;
}
dx=(float)x2-x1;
dy=(float)y2-y1;
x=x1;
y=y1;
p=(float)2*dy-dx;
myDDC->SetPixel(x1,y1,RGB(255,0,0));
if(x1==x2&&y1==y2)
{}
else
{
if(x1==x2)
{
for(int i=y1+1;i<=y2;i++)
{
myDDC->SetPixel(x1,i,RGB(255,0,0));
}
}
else
{
if(y1==y2)
{
for(int j=x1+1;j<=x2;j++)
{
myDDC->SetPixel(j,y1,RGB(255,0,0));
}
}
else
{
if(dx!=0)
{
k=(float)dy/dx;
if(k>0&&k<=1)
{
for(int a=x1;a<=x2;a++)
{
if(p>0)
{
x=x+1;
y=y+1;
p=p+2*dy-2*dx;
}
else
{
x=x+1;
y=y;
p=p+2*dy;
}
myDDC->SetPixel(x,y,RGB(255,0,0));
}
}
else
{
for(int b=y1;b<=y2;b++)
{
if(p>0)
{
x=x+1;
y=y+1;
p=p+2*dx-2*dy;
}
else
{
x=x;
y=y+1;
p=p+2*dx;
}
myDDC->SetPixel(x,y,RGB(255,0,0));
}
}
}
}
}
}
}