Drawing aline using bresenham algorithm

Printing a line using bresenham algorithm


#include<stdio.h>                                                   
#include<conio.h>
#include<graphics.h>                                                    
void main()
{
            int x,y,x1,y1,x2,y2,dx,dy,xi,yi,s,p,i;                                                                                                                                                    
            int gdriver=DETECT,gmode,gerror;                                                                                               
            initgraph(&gdriver,&gmode,"c:\\tc\\bgi");                                
            printf("\nEnter First Point (x1,y1):");
            scanf("%d%d",&x1,&y1);                                                  
            printf("\nEnter Second Point (x2,y2):");
            scanf("%d%d",&x2,&y2);                                                   
            x=x1;
            y=y1;
            putpixel(x,y,2);                    
            dx=x2-x1;
            dy=y2-y1;
            p=(2*dy-dx);                        
            while(x<=x2)
            {
                        if(p<0)
                        {
                                    x=x+1;
                                    p=p+2*dy;                                                         
                        }       
                        else
                        {
                                    x=x+1;
                                    y=y+1;
                                    p=p+2*dy-2*dx;                                              
                        }
                        putpixel(x,y,7);        
            }

            getch();
            closegraph();
}



output




Post a Comment

0 Comments