I have the following code for DDA Line Algorithm in C :-

//DDA Line Algorithm

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

#define Round(a) ((int)(a+0.5))

void linDDA(int xa,int ya,int xb,int yb);

int main()
{
  int gdriver=DETECT,gmode;
  initgraph(&gdriver,&gmode,"E:\\TC\\bgi");
  linDDA(10,20,200,100);
  clrscr();
  getch();
  return 0;
}

void linDDA(int xa,int ya,int xb,int yb)
{
 int dx,dy,steps,k;
 float xincr,yincr,x=xa,y=ya;

 dx=xb-xa;
 dy=yb-ya;

 if(abs(dx)>abs(dy))
 steps=abs(dx);
 else
 steps=abs(dy);

 xincr=dx/(float)steps;
 yincr=dy/(float)steps;

 putpixel(Round(x),Round(y),20);           //plotting the first point

 for(k=0;k<steps;k++)
 {
  x+=xincr;
  y+=yincr;
  putpixel(Round(x),Round(y),20);
 }
}

The code compiles fine but in the output I get a white screen on a black background. There is no line printed in the output. Please help!!

You are clearing the screen, immediately AFTER you call the function!

Try clearing the screen BEFORE you call the function! ROFL! ;)

Well that proves that I seriously need some changes. This has happened with me a lot of times earlier too. Thanks anyway..

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.