Bresenham’s Line Drawing Algorithm

#include<graphics.h>
#include<stdio.h>
#include<conio.h>

int main()
{
    int gd=DETECT,gmode;
    int dx,dy,x,y,x1,y1,x2,y2,p;
    clrscr();
    initgraph(&gd,&gmode,"C:\\TC\\BGI");
    printf("\nEnter the co-ordinates of first point:");
    scanf("%d%d",&x1,&y1);
    printf("\nEnter the co-ordinates of second point:");
    scanf("%d%d",&x2,&y2);
    dx=x2-x1;
    dy=y2-y1;
    p=2*dy-dx;
    x=x1;
    y=y1;
    putpixel(x,y,5);
    while(x<=x2)
    {
        if(p<0)
        {
            x=x+1;
            y=y;
            p=p+2*dy;
        }
        else
        {
            x=x+1;
            y=y+1;
            p=p+2*(dy-dx);
        }
        putpixel(x,y,5);
    }
getch();
closegraph();
}

OutputBresnem

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s