By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,369 Members | 966 Online
Bytes IT Community
+ Ask a Question
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
  1. /*start*/
  2. #include <graphics.h>
  3. #include <stdio.h>
  4. void drawaxes();
  5. void slopelessthanone(double,double,double,double);
  6. void slopegreaterthanone(double,double,double,double);
  7. main()
  8. {
  9.     int gdriver = DETECT, gmode, errorcode;
  10.     int originx,originy;
  11.     double x1,y1,x2,y2,dx,dy,m;
  12.     initgraph(&gdriver, &gmode, "c:\\tc\\bgi");
  13.     errorcode = graphresult();
  14.     if (errorcode != grOk)
  15.     {
  16.         printf("Graphics error: %s\n", grapherrormsg(errorcode));
  17.         printf("Press any key to halt:");
  18.         getch();
  19.         exit(1);
  20.     }
  21.     originx=getmaxx()/2;
  22.     originy=getmaxy()/2;
  23.     printf("Enter coordinates of first point:");
  24.     scanf("%lf%lf",&x1,&y1);
  25.     printf("Enter coordinates of second point:");
  26.     scanf("%lf%lf",&x2,&y2);
  27.     if (x1>=0)                       /*rounding off*/
  28.         x1+=0.5;
  29.     else
  30.         x1-=0.5;
  31.     if (y1>=0)
  32.         y1+=0.5;
  33.     else
  34.         y1-=0.5;
  35.     if (x2>=0)
  36.         x2+=0.5;
  37.     else
  38.         x2-=0.5;
  39.     if (y2>=0)
  40.         y2+=0.5;
  41.     else
  42.         y2-=0.5;
  43.     dx=x2-x1;
  44.     dy=y2-y1;
  45.     m=dy/dx;
  46.     x1+=originx;        /*shifting the coordinate system*/
  47.     y1+=originy;
  48.     x2+=originx;
  49.     y2+=originy;
  50.     if (abs(m)<1)
  51.         slopelessthanone(x1,y1,x2,m);
  52.     if (abs(m)>=1)
  53.         slopegreaterthanone(y1,y2,x1,m);
  54.     getch();
  55.     closegraph();
  56.     return;
  57. }
  58. void drawaxes()   /*drawing coordinate axes*/
  59. {
  60.     line(getmaxx()/2, 0, getmaxx()/2, getmaxy());
  61.     line(0, getmaxy()/2, getmaxx(), getmaxy()/2);
  62. }
  63. void slopelessthanone(double x1, double y1, double x2, double m)
  64. {
  65.     double x,y;
  66.     x=x1;
  67.     y=y1;
  68.     clrscr();
  69.     drawaxes();
  70.     if (x1<x2)
  71.         while(x<=x2){
  72.             if (y>=0)
  73.                 putpixel(x,y+0.5,WHITE);
  74.             else
  75.                 putpixel(x,y-0.5,WHITE);
  76.             x+=1;
  77.             y+=m;
  78.         }
  79.     else
  80.         while(x>=x2){
  81.             if (y>=0)
  82.                 putpixel(x,y+0.5,WHITE);
  83.             else
  84.                 putpixel(x,y-0.5,WHITE);
  85.             x-=1;
  86.             y-=m;
  87.         }
  88. }
  89. void slopegreaterthanone(double y1, double y2, double x1, double m)
  90. {
  91.     double x,y;
  92.     y=y1;
  93.     x=x1;
  94.     clrscr();
  95.     drawaxes();
  96.     if (y1<y2)
  97.         while(y<=y2){
  98.             if (x>=0)
  99.                 putpixel(x+0.5,y,WHITE);
  100.             else
  101.                 putpixel(x-0.5,y,WHITE);
  102.             y+=1;
  103.             x+=m;
  104.         }
  105.     else
  106.         while(y>=y2){
  107.             if (x>=0)
  108.                 putpixel(x+0.5,y,WHITE);
  109.             else
  110.                 putpixel(x-0.5,y,WHITE);
  111.             y-=1;
  112.             x-=m;
  113.         }
  114. }
  115.  
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
Share this Question
Share on Google+
2 Replies


sicarie
Expert Mod 2.5K+
P: 4,677
What do you mean "certain lines are going way off"? Is there a mistake in the logic of the program that you can't find out? Where? What's "way off" and how is it not correct (what should it be)?
Aug 13 '07 #2

weaknessforcats
Expert Mod 5K+
P: 9,197
Code like this:

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;
may not work as expected. In general, operators like +,- >=, etc cannot be used with floating point. Variables close in value may be incorrectly evaluated due to the automatic rounding of floaring point. Generally, you work with the absolute value within a specified error tolerance.

I did a Google on floating point arithmetic and found 666,000 web pages.
Aug 13 '07 #3

Post your reply

Sign in to post your reply or Sign up for a free account.