Page 56 - DCAP313_LAB_ON_COMPUTER_GRAPHICS
P. 56

Lab on Computer Graphics



                   Notes                              y     =     y  1  + (dy) * (x – x 1 )/(dx)
                                                      pixel(x, y)
                                                }
                                 3.3.2 C Code DDA Line Algorithm
                                             #include <graphics.h>
                                             #include <stdio.h>
                                             #include <conio.h>
                                             #include <math.h>
                                           void main()
                                           {
                                                int gd=DETECT,gm=DETECT,s,dx,dy,m,x 1 ,y 1 ,x 2 ,y 2 ;
                                                fl  o  a  t     x  i ,y i ,x,y;
                                                clrscr();
                                             printf(“Enter the sarting point x 1  & y 1n ”);
                                                scanf(“%d%d”,&x 1 ,&y 1 );
                                             printf(“Enter the end point x 2  & y 2n ”);
                                                scanf(“%d%d”,&x 2 ,&y 2 );
                                                initgraph(&gd,&gm, “”);
                                                cleardevice();
                                                d  x  =  x  2 –x 1 ;
                                                d  y  =  y  2 –y 1 ;
                                                if(abs(dx)>abs(dy))
                                                   s=abs(dx);
                                             else
                                                   s=abs(dy);
                                             x i =dx/(float)s;
                                             y i =dy/(float)s;
                                                x  =  x  1 ;
                                                y  =  y  1 ;
                                             putpixel(x 1 ,y 1 ,4);
                                                for(m=0; m < s; m++)
                                             {
                                                   x  +  =  x  i ;
                                                   y  +  =  y  i ;
                                                   putpixel(x,y,4);
                                             }
                                                   getch();
                                           }
                                 Self Assessment Questions

                                    6.  Which one is a line algorithm for …………….. line conversion?
                                       (a)  Bresenham's                (b)  Beizer
                                       (c)  Both a and b               (d)  Midpoint



        50                                LOVELY PROFESSIONAL UNIVERSITY
   51   52   53   54   55   56   57   58   59   60   61