画线算法(使用加减法)

来源:互联网 发布:知乎 惊悚悬疑电影 编辑:程序博客网 时间:2024/05/17 04:18

// 画线函数
// 梅文海
// 2005.11.29
void Line2(CDC* ADC,int x1,int y1,int x2,int y2,COLORREF AColor)
{
    int iW,iH;
    int x,y;
    iW=abs(x1-x2);      // 宽
    iH=abs(y1-y2);      // 高
    int iYAdd=1;
    if(x2<x1){          // 调整 Y 的方向
        x=x2;
        y=y2;
        if(y1<y2){
            iYAdd=-1;
        }
    }
    else{
        x=x1;
        y=y1;
        if(y2<y1){
            iYAdd=-1;
        }
    }

    int iCount=iH;          // 高宽的最大者
    if(iW>iH){
        iCount=iW;
    }
    int iXCount=0;
    int iYCount=0;
   
    ADC->SetPixel(x,y,AColor);
    for(int i=0;i<iCount-1;i++){
        iXCount+=iW;
        iYCount+=iH;
        if(iXCount>=iCount){
            x++;
            iXCount-=iCount;
        }
        if(iYCount>=iCount){
            y+=iYAdd;
            iYCount-=iCount;
        }
        ADC->SetPixel(x,y,AColor);
    }
}

凌丽软件 http://www.wosens.com

原创粉丝点击