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

void midline(int,int,int,int);
void main(void)
{

   int gdriver = DETECT, gmode, errorcode;
   int xmax, ymax;


   initgraph(&gdriver, &gmode, "");


   errorcode = graphresult();

   if (errorcode != grOk)
   {
      printf("Graphics error: %s\n", grapherrormsg(errorcode));
      printf("Press any key to halt:");
      getch();
      exit(1);
   }

   setcolor(getmaxcolor());
   int x1,y1,x2,y2;
   printf("Enter x1:");
   scanf("%d",&x1);
   printf("Enter y1:");
   scanf("%d",&y1);
   printf("Enter x2:");
   scanf("%d",&x2);
   printf("Enter y2:");
   scanf("%d",&y2);
   midline(x1,y1,x2,y2);        //100 200 300 500
   getch();
}

void midline(int x1,int y1,int x2,int y2)
{
	int dx=x2-x1;
	int dy=y2-y1;

	int d;

	d=((2*dy)-dx);
	int inc1=2*dy;
	int inc2=(2*(dy-dx));

	int x=x1;
	int y=y1;
	putpixel(x,y,RED);

	while(x<x2)
	{   if(d<=0)
	   {
	       d+=inc1;
	       x++;
	   }
	   else
	   {
		d+=inc2;
		y++;
		x++;
	   }

	   putpixel(x,y,RED);
	}

}

it is giving straight line in maximum cases. but please run it once and give input "100 200 300 500" ...it is giving straight but at one point it bends. you can know my problem when you will see the output of what i am saying. please help!! i am giving image of output below..! see it plz!

yes I am getting a similar problem with my code too.
if i give coordinate line1(1,1,400,400)
It gives a straight line
but if I give line1(400,400,1,1)
It gives a bent line like yours.
Why is it happening could any one explain?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.