DDA Line Drawing Algorithm

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

void main()
{
    int gd=DETECT,gmode;
    int xa,xb,ya,yb;
    int dx,dy,steps,k,xinc,yinc,x,y;
    initgraph(&gd,&gmode,"C:\\TC\\BGI");
    printf("Enter X1 pixel position: ");
    scanf("%d",&xa);
    printf("Enter Y1 pixel position: ");
    scanf("%d",&ya);
    printf("Enter X2 pixel position: ");
    scanf("%d",&xb);
    printf("Enter Y2 pixel position: ");
    scanf("%d",&yb);
    dx=xb-xa;
    dy=yb-ya;
    if(abs(dx)>abs(dy))
    {
        steps=abs(dx);
    }
    else
    {
        steps=abs(dy);
    }
    xinc=dx/steps;
    yinc=dy/steps;
    x=xa;
    y=ya;
    putpixel(x,y,6);
    for(k=1;k<=steps;k++)
    {
        x=x+xinc;
        y=y+yinc;
        putpixel(x,y,6);
    }
    getch();
}

OutputDDA

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