432,369 Members | 966 Online
Need help? Post your question and get tips & solutions from a community of 432,369 IT Pros & Developers. It's quick & easy.

# scan conversion of line (DDA algorithm) using graphics in c

 P: 7 Here's a code for scan conversion of a line using DDA algorithm.... but i guess there's some problem in the sense that not every kind of line is getting plotted properly (at least that's what i think)... check it out plz... Expand|Select|Wrap|Line Numbers /*start*/ #include  #include  void drawaxes(); void slopelessthanone(double,double,double,double); void slopegreaterthanone(double,double,double,double); main() {     int gdriver = DETECT, gmode, errorcode;     int originx,originy;     double x1,y1,x2,y2,dx,dy,m;     initgraph(&gdriver, &gmode, "c:\\tc\\bgi");     errorcode = graphresult();     if (errorcode != grOk)     {         printf("Graphics error: %s\n", grapherrormsg(errorcode));         printf("Press any key to halt:");         getch();         exit(1);     }     originx=getmaxx()/2;     originy=getmaxy()/2;     printf("Enter coordinates of first point:");     scanf("%lf%lf",&x1,&y1);     printf("Enter coordinates of second point:");     scanf("%lf%lf",&x2,&y2);     if (x1>=0)                       /*rounding off*/         x1+=0.5;     else         x1-=0.5;     if (y1>=0)         y1+=0.5;     else         y1-=0.5;     if (x2>=0)         x2+=0.5;     else         x2-=0.5;     if (y2>=0)         y2+=0.5;     else         y2-=0.5;     dx=x2-x1;     dy=y2-y1;     m=dy/dx;     x1+=originx;        /*shifting the coordinate system*/     y1+=originy;     x2+=originx;     y2+=originy;     if (abs(m)<1)         slopelessthanone(x1,y1,x2,m);     if (abs(m)>=1)         slopegreaterthanone(y1,y2,x1,m);     getch();     closegraph();     return; } void drawaxes()   /*drawing coordinate axes*/ {     line(getmaxx()/2, 0, getmaxx()/2, getmaxy());     line(0, getmaxy()/2, getmaxx(), getmaxy()/2); } void slopelessthanone(double x1, double y1, double x2, double m) {     double x,y;     x=x1;     y=y1;     clrscr();     drawaxes();     if (x1=0)                 putpixel(x,y+0.5,WHITE);             else                 putpixel(x,y-0.5,WHITE);             x+=1;             y+=m;         }     else         while(x>=x2){             if (y>=0)                 putpixel(x,y+0.5,WHITE);             else                 putpixel(x,y-0.5,WHITE);             x-=1;             y-=m;         } } void slopegreaterthanone(double y1, double y2, double x1, double m) {     double x,y;     y=y1;     x=x1;     clrscr();     drawaxes();     if (y1=0)                 putpixel(x+0.5,y,WHITE);             else                 putpixel(x-0.5,y,WHITE);             y+=1;             x+=m;         }     else         while(y>=y2){             if (x>=0)                 putpixel(x+0.5,y,WHITE);             else                 putpixel(x-0.5,y,WHITE);             y-=1;             x-=m;         } }   I've devoted a lot of time to this... & honestly i don't know even if there is a mistake here... but certain lines are going way off from what they should look like... Aug 13 '07 #1