Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C++
Hello,
 
I am doing DDA line drawing algorithm in C++.
 
I have finished it, error free, but the graphics are not displaying after running it.
 
It just brings me back to the programs.
I am confused so I need your help guys.
 
I know its easy for you guys, Help!!
 
#include<iostream.h>
#include<graphics.h>
#include<conio.h>
#include<math.h>
#include<dos.h>
#include<stdlib.h>
#include<stdio.h>
class lines
{
    private:
    int length,x1,y1,x2,y2,x,y,dx,dy,wx,wy,w,width;
    public:
	lines(); // Constructor
	void showline();
	int sign(int);
};
int lines::sign(int xx)
{
    if(xx<0)
	return -1;
    if(xx==0)
	return 0;
    if(xx>0)
	return 1;
	return 0;
}
lines::lines()
{
    x=0;y=0;
    cout<<"Enter The Co-Ordinates(x1,y1):=";
    cin>>x1>>y1;
    cout<<"Enter The Co-Ordinates(x2,y2):=";
    cin>>x2>>y2;
    cout<<"Enter The Width Of The Line:=";
    cin>>width;
}
void lines::showline()
{
    char *s,*s1;
    if(abs(x2-x1)>=abs(y2-y1))
	length=abs(x2-x1);
    else
	length=abs(y2-y1);
	w=width;
	wx=((w-1)/2)*(sqrt((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1))/abs(y2-y1));
	wy=((w-1)/2)*(sqrt((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1))/abs(x2-x1));
	dx=(x2-x1)/length;
	dy=(y2-y1)/length;
	if(dy>dx)
	    wy=wx;
	    x=x1+0.5*sign(dx);
	    y=y1+0.5*sign(dy);
	    int i=1;
	    setcolor(0);
	    while(i<=length)
	    {
		for(int j=0;j<wy;j++)
		    putpixel((x+320),480-(y+240+j),6);
		    for(j=0;j<wy;j++)
			putpixel((x+320),480-(y+240-j),6);
			putpixel((x+320),480-(y+240),6);
			x+=dx;
			y+=dy;
			i++;
	    }
	    setcolor(15);
	    outtextxy(20,10,"The Points Are:=");
	    sprintf(s,"A(%d,%d)",x1,y1);
	    outtextxy(20,20,s);
	    sprintf(s,"B(%d,%d)",x2,y2);
	    outtextxy(20,30,s);
	    getch();
}
void main()
{
	   int gd=DETECT,gm,i,j,xx=240,xxx=380;
	   clrscr();
	   lines a;
	   char *mess[]={"D","D","A"," ","A","L","G","O","R","I","T","H","M"};
	   initgraph(&gd,&gm,"..\\bgi");
	   cleardevice();
	   rectangle(120,40,320,240);
	   rectangle(320,40,520,240);
	   rectangle(120,240,320,440);
	   rectangle(320,240,520,440);
	   for(i=0,j=12;i<8,j>=6;i++,j--)
	   {
	       xx+=10;
	       outtextxy(xx,10,mess[i]);
	       xxx-=10;
	       outtextxy(xxx,10,mess[j]);
	       delay(100);
	   }
	   for(i=130;i<=510;i+=10)
	      for(j=50;j<=430;j+=10)
		  putpixel(i,j,15);
		      for(i=130;i<=510;i+=10)
		      {
			  if(i==320)
			      continue;
			      outtextxy(i,237,"+");
		      }
		      for(i=50;i<=430;i+=10)
		      {
			  if(i==240)
			      continue;
			      outtextxy(317,i,"-");
		      }
		      outtextxy(310,230,"O");
		      outtextxy(530,240,"X");
		      outtextxy(320,450,"-Y");
		      outtextxy(100,240,"-X");
		      outtextxy(320,30,"Y");
		      a.showline();
		      closegraph();
}
Posted 16-Dec-10 5:16am
damaxy393
Edited 16-Dec-10 21:52pm
v3
Comments
Dalek Dave at 17-Dec-10 3:52am
   
Edited for Grammar and Readability.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Maybe the BGI driver is not found, you should include it in you project, see the following discussion http://www.programmersheaven.com/mb/CandCPP/35332/35332/help-grphics-error-devicle-driver-not-found-egavgabgi/[^].
Smile | :)
  Permalink  
Comments
Dalek Dave at 17-Dec-10 3:52am
   
Good Call!
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

Are you using the correct block scoping rules for your if and for statements:
 
Without your {} only a single line is the for/else part. The way you have the code formatted suggests that you think your blocks are bigger.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

CPallini has a good point; to check if this is indeed the case, you need to call graphresult()[^] after initgraph().
 
Running your program in DOSBox[^] could possibly be a solution too, it tends to be much better at running MS-DOS programs than Windows NT based operating systems.
  Permalink  
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

What system are you running this on; this (MS-DOS) code went out with Windows 3.1? If you want to use graphics on any modern version of Windows then you need to write a Windows[^] program and use GDI or GDI+.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 5

yes..now its working...i copy the BGI driver to my project..Thanks to all of you..You guys are great!! Smile | :)
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 478
1 Sergey Alexandrovich Kryukov 305
2 PhilLenoir 115
3 RajeeshMenoth 110
4 Uma Shankar Patel 80
0 Sergey Alexandrovich Kryukov 6,557
1 OriginalGriff 5,966
2 CPallini 2,473
3 Richard MacCutchan 1,687
4 Abhinav S 1,540


Advertise | Privacy | Mobile
Web04 | 2.8.140821.2 | Last Updated 17 Dec 2010
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100