mfc绘制直线

void CLine::LineTo(CDC*pDC,CP2 p1)

{

       P1=p1;

       CP2p,t;

       COLORREFclr=RGB(255,0,0);

       if(fabs(P0.X-P1.X)<1e-6)

       {

              if(P0.Y>P1.Y)

              {

                     t=P0;P0=P1;P1=t;

              }

              for(p=P0;p.Y

              {

                     pDC->SetPixelV(Round(p.X),Round(p.Y),clr);

              }

       }

else

{

       doublek,d;

       k=(P1.Y-P0.Y)/(P1.X-P0.X);

       if(k>1.0)

       {

              if(P0.Y>P1.Y)

              {t=P0;P0=P1;P1=t;

              }

              d=1-0.5*k;

              for(p=P0;p.Y

              {

                     pDC->SetPixelV(Round(p.X),Round(p.Y),clr);

       

                     if(d>=0)

                     {p.X++;

                  d+=1-k;

                     }

                 else

                     d+=1;

              }

       }

       if(0.0<=k&& k<=1.0)

       {if(P0.X>P1.X)

       {t=P0;P0=P1;P1=t;

       }

       d=0.5-k;

       for(p=P0;p.X

       {pDC->SetPixelV(Round(p.X),Round(p.Y),clr);

       if(d<0)

       {

              p.Y++;

              d+=1-k;

       }

       else

              d-=k;

       }

       }

       if(k>=-1.0 && k<0.0)

       {

              if(P0.X>P1.X)

              {

                     t=P0;P0=P1;P1=t;

              }

              d=-0.5-k;

              for(p=P0;p.X

              {

                     pDC->SetPixelV(Round(p.X),Round(p.Y),clr);

                     if(d>0)

                     {

                            p.Y--;

                            d-=1+k;

                     }

                     else

                            d-=k;

              }

       }

    if(k<-1.0)

    {

              if(P0.Y

              {

                     t=P0;P0=P1;P1=t;

              }

              d=-1-0.5*k;

              for(p=P0;p.Y>P1.Y;p.Y--)

              {

                     pDC->SetPixelV(Round(p.X),Round(p.Y),clr);

                            if(d<0)

                            {

                                   p.X++;

                                   d-=1+k;

                            }

                            else

                                   d-=1;

              }

       }

}

P0=p1;

}

 


你可能感兴趣的:(mfc绘制直线)