Search Here

C program to draw Line using DDA algorithm



/* C program for DDA Line drawing */

#include<graphics.h>
#include<stdio.h>
#include<math.h>
#define ROUND(a) ((int)(a+0.5))

int main()
{
    int x1,y1,x2,y2,dx,dy,steps,gd,gm;
    float x_inc,y_inc,x,y;
    
    printf("Enter the co-ordinates of 1st point:");
    scanf("%d %d",&x1,&y1);
    printf("Enter the co-ordinates of 2nd point:");
    scanf("%d %d",&x2,&y2);
    
    dx=abs(x1-x2);
    dy=abs(y1-y2);
    if(dx>dy)
        steps=dx;
    else
        steps=dy;
        
    x_inc = (float)dx/steps;
    y_inc = (float)dy/steps;
    detectgraph(&gd,&gm);
   
    //initgraph(&gd, &gm, "C:\\TC\\BGI"); /* For Windows Turbo-C users */
    initgraph(&gd,&gm,NULL); /* For Linux users */
    setbkcolor(WHITE);
    
    putpixel(x1,y1,BLACK);
    while(steps>=1)
    {
        x += x_inc;
        y += y_inc;
        putpixel(ROUND(x),ROUND(y),BLACK);
        steps--;
    }
    delay(5000);
   
    closegraph();
    return 0;
}



C program to draw a line using DDA algorithm
Output

No comments:

Post a Comment