• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
  • Skip to footer
projectsgeek

ProjectsGeek

Download Mini projects with Source Code, Java projects with Source Codes

  • Home
  • Java Projects
  • C++ Projects
  • VB Projects
  • PHP projects
  • .Net Projects
  • NodeJs Projects
  • Android Projects
    • Project Ideas
      • Final Year Project Ideas
      • JSP Projects
  • Assignment Codes
    • Fundamentals of Programming Language
    • Software Design Laboratory
    • Data Structure and Files Lab
    • Computer Graphics Lab
    • Object Oriented Programming Lab
    • Assembly Codes
  • School Projects
  • Forum

Bressenham and DDA Line Drawing algorithms

April 5, 2011 by ProjectsGeek Leave a Comment

Bressenham DDA Line drawing and Circle Drawing Algorithms

Write a program for Bressenham and DDA Line Drawing algorithms using C++ language. Simulate these algorithms using C++ graphics classes and functions.
User has to provide input initially and then by selecting proper option user will get the output.

DDA Line Drawing algorithm Code

#include  
 #include  
 #include  
 #include  
 #include  
 class algo  
 {  
   float x1,x2,y1,y2;  
   public:  
   int sign(float a)  
   {  
   if(a>0)  
     return 1;  
   if(a<0)  
     return -1;  
   return 0;  
   }  
 void dda(float,float,float,float);  
 void br_line(float,float,float,float);  
 void br_c(float,float,float);  
 };  
 void main()  
 {  
   algo a;  
   int ch,gdriver=DETECT,gmode;  
   initgraph(&gdriver,&gmode,"c:\\tc\\bgi");  
   do  
   {  
     cleardevice();  
     cout<< "\n\t\t\t#### MAIN MENU ####";  
     cout<< "\n\t\t1.DDA LINE DRAWING ALGORITHM";  
     cout<< "\n\t\t2.BRESENHAM LINE DRAWIG ALGORITHM";  
     cout<< "\n\t\t3.BRESENHAM CIRCLE DRAWNG ALGORITHM";  
     cout<< "\n\t\t4.EXIT";  
     cout<< "\n\t\tENTER UR CHOICE:";  
     cin>> ch;  
     clrscr();  
     switch(ch)  
     {  
       case 1:  float x2,x1,y2,y1;  
         cout<< "\n\t\tDDA LINE DRAWING ALGO";  
         cout<< "\n\tEnter (x1,y1) co-ordinates of first point:";  
         cin>>x1>>y1;  
         cout<< "\n\tEnter (x2,y2) co-ordinates. of last point:";  
         cin>>x2>>y2;  
         a.dda(x1,y1,x2,y2);  
         getch();  
         break;  
       case 2:  cout<< "\n\t\tBRESENHAM LINE DRAWIG ALGO";  
         cout<< "\n\tEnter (x1,y1) co-ordinates of first point";  
         cin>>x1>>y1;  
         cout<< "\n\tEnter (x2,y2) co-ordinates of end point";  
         cin>>x2>>y2;  
         a.br_line(x1,y1,x2,y2);  
         getch();  
         break;  
       case 3:  cout<< "\n\t\tBRESENHAM CIRCLE DRAWNG ALGO";  
         cout<< "\n\tEnter center co-ordinates.";  
         cin>>x1>>y1;  
         float r;  
         cout<< "\n\tEnter radius";  
         cin>>r;  
         a.br_c(x1,y1,r);  
         getch();  
         break;  
       case 4:  break;  
       default:cout<< "\t\tInvalid Choice\n";  
         getch();  
         break;  
     }  
   }while(ch!=4);  
   closegraph();  
 }  
 void algo:: dda(float x1,float y1,float x2,float y2)  
 {  
   float dx,dy,len,i,xn,yn,x,y,midx,midy;  
   midx=getmaxx()/2;  
   midy=getmaxy()/2;  
   line(0,midy,2*midx,midy);  
   line(midx,0,midx,2*midy);  
   x1+=midx;  
   y1=-y1;  
   y1+=midy;  
   x2+=midx;  
   y2=-y2;  
   y2+=midy;  
   dx=abs(x2-x1);  
   dy=abs(y2-y1);  
   if(dx>=dy)  
     len=dx;  
   else  
     len=dy;  
   dx=(x2-x1)/len;  
   dy=(y2-y1)/len;  
   x=x1+0.5*sign(dx);  
   y=y1+0.5*sign(dy);  
   putpixel(x,y,WHITE);  
   i=1;  
   while(i<=len)  
   {  
     putpixel(x,y,RED);  
     x=x+dx;  
     y=y+dy;  
     i++;  
   }  
   getch();  
 }  
 void algo:: br_line(float x1,float y1,float x2,float y2)  
 {  
   float dx,dy,s1,s2,e,i,change,x,y,midx,midy,x3,y3,exch;  
   midx=getmaxx()/2;  
   midy=getmaxy()/2;  
   line(0,midy,2*midx,midy);  
   line(midx,0,midx,2*midy);  
   x1+=midx;  
   y1=-y1;  
   y1+=midy;  
   x2+=midx;  
   y2=-y2;  
   y2+=midy;  
   x=abs(x2-x1);  
   y=abs(y2-y1);  
   x3=x1;  
   y3=y1;  
   s1=sign(x2-x1);  
   s2=sign(y2-y1);  
   e=2*y-x;  
   if(y>x)  
   {  
     int temp=x;  
     x=y;  
     y=temp;  
     exch=1;  
   }  
   else  
     exch=0;  
   i=1;  
   while(i<=x)  
   {  
     putpixel(x3,y3,WHITE);  
     while(e>=0)  
     {  
       if(exch==1)  
         x3+=s1;  
       else  
         y3+=s2;  
       e=e-2*x;  
     }  
     if(exch==1)  
       y3+=s2;  
     else  
       x3+=s1;  
     e=e+2*y;  
     i++;  
   }  
 }  
 void algo:: br_c(float a,float b,float r)  
 {  
   float d,x,y,midx,midy;  
   midx=getmaxx()/2;  
   midy=getmaxy()/2;  
   line(0,midy,2*midx,midy);  
   line(midx,0,midx,2*midy);  
   x+=midx;  
   y=-y;  
   y+=midy;  
   x1=0,y1=r;  
   d=3-2*r;  
   while(x1<=y1)  
   {  
     if(d<0)  
     {  
       d=d+4*x1+6;  
     }  
     else  
     {  
       d=d+4*(x1-y1)+10;  
       y1=y1-1;  
     }  
     x1=x1+1;  
     putpixel(x1+a+(getmaxx()/2),y1+b+(getmaxy()/2),BLUE);  
     putpixel(y1+a+(getmaxx()/2),x1+b+(getmaxy()/2),BLUE);  
     putpixel(-x1+a+(getmaxx()/2),-y1+b+(getmaxy()/2),BLUE);  
     putpixel(-x1+a+(getmaxx()/2),y1+b+(getmaxy()/2),BLUE);  
     putpixel(y1+a+(getmaxx()/2),-x1+b+(getmaxy()/2),BLUE);  
     putpixel(-y1+a+(getmaxx()/2),x1+b+(getmaxy()/2),BLUE);  
     putpixel(x1+a+(getmaxx()/2),-y1+b+(getmaxy()/2),BLUE);  
     putpixel(-y1+a+(getmaxx()/2),-x1+b+(getmaxy()/2),BLUE);  
   }  
 }

 

 

Other Projects to Try:

  1. Graphics Editor code Using C++ Language
  2. Transformations computer graphics using C++ Language
  3. Matrix Operations in C Language
  4. Friend Function Implementation using C++
  5. Boundary and Flood Fill Algorithms in C++ Language

Filed Under: Computer Graphics Tagged With: Computer Graphics

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

Tags

.Net Projects Download Android Project Ideas Android Projects Angular 2 Assembly Codes C # Projects C & C++ Projects C++ Projects Class Diagrams Computer Graphics Database Project Data Mining Projects DataScience Projects Datastructure Assignments Download Visual Basic Projects Electronics project Hadoop Projects Installation Guides Internet of Things Project IOS Projects Java Java Interview Questions Java Projects JavaScript JavaScript Projects java tutorial JSON JSP Projects Mechanical Projects Mongodb Networking Projects Node JS Projects OS Problems php Projects Placement Papers Project Ideas Python Projects seminar and presentation Struts

Search this Website


Footer

Download Java Project
Download Visual Basic Projects
Download .Net Projects
Download VB Projects
Download C++ Projects
Download NodeJs Projects
Download School Projects
Download School Projects
Ask Questions - Forum
Latest Projects Ideas
Assembly Codes
Datastructure Assignments
Computer Graphics Lab
Operating system Lab
australia-and-India-flag
  • Home
  • About me
  • Contact Form
  • Submit Your Work
  • Site Map
  • Privacy Policy