Logo 
Search:

C++ Programming Articles

Submit Article
Home » Articles » C++ Programming » AlgorithmsRSS Feeds

DAA line drowing algo

Posted By: Mayur Humbal     Category: C++ Programming     Views: 8122

daa line drowing algo with co-ordinate graphics

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


void axis(void);

void ploat(int x1,int y1,int x2,int y2);


int main(void)
{

   int gdriver = DETECT, gmode, errorcode,x,y,x1,y1,x2,y2;
   char msg[80];

   /* initialize graphics and local variables */
   initgraph(&gdriver, &gmode, "C:\\TURBOC3\\BGI");



   cout<<"\nenter co-oridinates point 1:";
   cin>>x1>>y1;
   cout<<"\nenter co-oridinates point 2:";
   cin>>x2>>y2;

   cleardevice();
   axis();
   ploat(x1,y1,x2,y2);


   /* move the C.P. to location (20, 30) */
   moveto(getmaxx()/2, getmaxy()/2);

   /* plot a pixel at the C.P. */
  // putpixel(getx()+x, gety()-y, getmaxcolor());

   /* create and output a message at (20, 30) */
   //sprintf(msg, " (%d, %d)", getx(), gety());
   outtextxy(getmaxx()/2,getmaxy()/2,"(0,0)");


   //sprintf(msg, " (%d, %d)", getx(), gety());
   //outtext(msg);

   /* clean up */
   getch();
   closegraph();
   return 0;
}

void axis(void)
{
int i,x,y;
x=getmaxx();
y=getmaxy();
for(i=0;i<=x;i++)
{
putpixel(i,y/2,getmaxcolor());
putpixel(x/2,i,getmaxcolor());
}
}

void ploat(int x1,int y1,int x2,int y2)
{
int i,l,delx,dely;
float dx,dy,x,y;
char msg[80];

delx=x2-x1;
dely=y2-y1;

if(abs(delx) > abs(dely))
{
l=abs(delx);
}
else
{
l=abs(dely);
}

dx=abs(delx)/(float)l;
dy=abs(dely)/(float)l;


cout<<"\nformer dx:"<<dx<<" dy:"<<dy;

if(delx<0 && dely<0)
{
dx=-dx;
//dx=-(abs(delx)/(float)l);
}
else if(delx<0 && dely>0)
{
dx=-dx;
dy=-dy;
//dx=-(abs(delx)/(float)l);
//dy=-(abs(dely)/(float)l);
}
else if(delx>0 && dely>0)
{
dy=-dy;
//dy=-(abs(dely)/(float)l);
}

cout<<"\n dx:"<<dx<<" dy:"<<dy;

moveto(getmaxx()/2, getmaxy()/2);
x=getx()+x1;
y=gety()-y1;

sprintf(msg,"(%d,%d)",x1,y1);
outtextxy(x,y,msg);

putpixel(x,y,getmaxcolor());
//ploating points


for(i=0;i<l;i++)
{
x=x+dx;
y=y+dy;
putpixel(x,y,getmaxcolor());
}

sprintf(msg,"(%d,%d)",x2,y2);
outtextxy(x,y,msg);

}
  
Share: 

 
 

Didn't find what you were looking for? Find more on DAA line drowing algo Or get search suggestion and latest updates.

Mayur Humbal
Mayur Humbal author of DAA line drowing algo is from Australia.
 
View All Articles

Related Articles and Code:


 
Please enter your Comment

  • Comment should be atleast 30 Characters.
  • Please put code inside [Code] your code [/Code].

 
Asad Iqbal from Pakistan Comment on: Jan 13
this code is not running on my laptop(64 bit, with windows 7) on boland c++ 5.2, giving an error that
" ignoring reentrant command"
also
"error BGI graphics not supported under Windows"

How can I make it compatible with my system??

Prem Tiwari from India Comment on: Jan 20
The best of programing languages site.

View All Comments