breseham画直线算法

#include<graphics.h>

void main()
{
    int driver,mode;
    int xa,ya,xb,yb,c;
    int dx,dy,steps,k;
    int inc,tmp,const1,const2,p;
    int x,y;
    int wx,wy;
    driver=DETECT;
    k=1;
    mode=0;
    initgraph(&driver,&mode,"");
    printf("please enter the start point:/n");
    printf(" xa= ");
    scanf("%d",&xa);
    printf(" ya= ");
    scanf("%d",&ya);
    printf("please enter the end point:/n");
    printf(" xb= ");
    scanf("%d",&xb);
    printf(" yb= ");
    scanf("%d",&yb);
   
    dx=xb-xa;
    dy=yb-ya;

    if(abs(dx)>abs(dy))
    {
        if(dx*dy>=0)
            inc=1;
        else
            inc=-1;
        if(dx<0)
        {
            tmp=xa;
            xa=xb;
            xb=tmp;
            tmp=ya;
            ya=yb;
            yb=tmp;
            dx=-dx;
            dy=-dy;
        }
        p=2*dy-dx;
        const1=2*dy;
        const2=2*(dy-dx);
        x=xa;
        y=ya;
        putpixel(x,y,YELLOW);
        k++;
        while(x<xb)
        {
            x++;
            if(p<0)
                p+=const1;
            else
            {
                y+=inc;
                p+=const2;
            }
            putpixel(x,y,YELLOW);
            k++;
        }
    }
   
    else{
    if(dx*dy<=0)
        inc=1;
    else
        inc=-1;
        if(dy<0){
            tmp=xa;
            xa=xb;
            xb=tmp;
            tmp=ya;
            ya=yb;
            yb=tmp;
            dx=-dx;
            dy=-dy;
        }
        p=2*dx-dy;
        const1=2*dx;
        const2=2*(dx-dy);
        x=xa;
        y=ya;
        putpixel(x,y,YELLOW);
        k++;
        while(y<yb){
            y++;
            if(p>0)
                p+=const1;
            else{
                x+=inc;
                p+=const2;
            }
            printf("%d,",x);
            putpixel(x,y,YELLOW);
            k++;
        }
    }
    getch();
   
}

你可能感兴趣的:(c,算法)