//Program to plot a line by Direct Draw Algorithm


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

void drawline(int x1, int y1, int x2, int y2)
{
    int temp;
    if (x2<x1)
    {
        temp=x2;
        x2=x1;
        x1=temp;
        temp=y2;
        y2=y1;
        y1=temp;
    }
    float x=x1, y=y1;
    int dx, dy, step;
    float xi, yi;
    while (x<x2)
    {

            dx=x2-x1;
            dy=y2-y1;
            step=abs(dx)>abs(dy)?abs(dx):abs(dy);
            xi=double(dx)/step;
            yi=double(dy)/step;
            for (int k=1; k<=step; k++)
            {
                y=y+yi;
                x=x+xi;
                putpixel(x+getmaxx()/2,getmaxy()/2-y,5);
            }
    }
}

void main()
{
    int x1, x2, y1, y2;
    int gdriver = DETECT, gmode, errorcode;
    initgraph(&gdriver, &gmode, "");
    line(0,getmaxy()/2,getmaxx(),getmaxy()/2);
    line(getmaxx()/2,0,getmaxx()/2,getmaxy());
    cout<<"Enter x1 ";
    cin>>x1;
    cout<<"Enter y1 ";
    cin>>y1;
    cout<<"Enter x2 ";
    cin>>x2;
    cout<<"Enter y2 ";
    cin>>y2;
    drawline(x1,y1,x2,y2);
    getch();
}