Error while running DDA Line drawing algorithm in C

452 views Asked by At

I wrote a code to implement DDA line drawing algorithm in C. The line always generated on the top left corner and very thin,i want the line to be in the middle of the screen. Can anyone tell me what is the problem? My code:

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

void main()
{
    int x,y,x1,y1,x2,y2,temp;
    int gd=DETECT,gm;
    initgraph(&gd,&gm,NULL);    
    printf("Enter the x1,y1:\n");
    scanf("%d %d",&x1,&y1);
    printf("Enter the x2,y2:\n");
    scanf("%d %d",&x2,&y2);
    int dx=abs(x2-x1);
    int dy=abs(y2-y1);
    if(dx>dy)
    {
        temp=dx;
    }
    else
    {
        temp=dy;
    }
    float xin=dx/temp;
    float yin=dy/temp;
    x=x1;
    y=y1;
    for(int k=0; k<temp; k++)
    {
        x=x+xin;
        y=y+yin;
        putpixel(x,y,RED);
        delay(100);
    }
getch();
closegraph;
}

I have also attached an image of the output.enter image description here

0

There are 0 answers