Important alert: (current site time 4/18/2014 9:32:00 PM EDT)
 

VB icon

DDA and BRESANHAM line drawing algorithm

Email
Submitted on: 10/21/2004 8:10:46 AM
By: Fairdeal_Coder  
Level: Intermediate
User Rating: By 1 Users
Compatibility: C, C++ (general), Borland C++
Views: 39192
(About the author)
 
     This program implements DDA and BRESANHAM line drawing algorothm in C graphics mode 2 .

 
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
 
Terms of Agreement:   
By using this code, you agree to the following terms...   
  1. You may use this code in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
  2. You MAY NOT redistribute this code (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
  3. You may link to this code from another website, but ONLY if it is not wrapped in a frame. 
  4. You will abide by any additional copyright restrictions which the author may have placed in the code or code's description.
				
//**************************************
// Name: DDA and BRESANHAM line drawing algorithm
// Description:This program implements DDA and BRESANHAM line drawing algorothm in C graphics mode 2 .
// By: Fairdeal_Coder
//
// Inputs:I have chucked some dummy inputs myself to demo the program right now , but if someone wants to change the inputs then ammend in main() edit the parameter values of DD_Line() for DDA and bres() for BRESANHAM function .
//
// Returns:Code does'nt return any values as such , simply displays lines created by both algos .
//
// Assumes:It is tried and tested on DOS and WIN98 DOS-SHELL , the user is assumed to have one of those OS running in order to use this program .
//
// Side Effects:No side effects known .
//
//This code is copyrighted and has// limited warranties.Please see http://www.Planet-Source-Code.com/vb/scripts/ShowCode.asp?txtCodeId=8511&lngWId=3//for details.//**************************************

// AUTHOR : SYED FARAZ RAZA RIZVI		
// LINE DRAWING BY DDA ALGORITM & BRESANHAM ALGORITHM
#include<conio.h>
#include<stdio.h>
#include<stdlib.h>
#include<graphics.h>
#include<dos.h>
#include<iostream.h>
#include<math.h>
void DD_Line(int x1,int y1,int x2,int y2);
void bres(int x1,int y1,int x2,int y2,int=6);
int round (double);
void main(void)
{
int driver=9,mode=2;
initgraph(&driver,&mode,"C:\\TC");
outtextxy(250,10,"DD_Line Algo");
DD_Line(50,50,100,50);
getch();
cleardevice();
outtextxy(250,10,"Bresenham's Line Algo");
bres(50,30,100,50);
getch();
closegraph();
}
void DD_Line(int x1,int y1,int x2,int y2)
{
float dx,dy,y,m,x;
int xtra1,xtra2,mark=2;
if(x1>x2)
{
xtra1=y1;y1=y2;y2=xtra1;
xtra2=x1;x1=x2;x2=xtra2;
printf("%d %d %d %d",x1,y1,x2,y2);
}
dx=x2-x1;
dy=y2-y1;
if (fabs(dx)>fabs(dy))
{
m=dy/dx;
y=y1;
for(x=x1;x<x2;x++)
{
putpixel(x,round(y),mark);
y=y+m;mark=5;
}
}
else
{
m=dx/dy;
x=x1;mark=3;
if(y1<y2)
{
for(y=y1;y<y2;y++)
{
putpixel(round(x),y,mark);
x=x+m;mark=6;
}
}
else
{
for(y=y1;y>y2;y--)
{
putpixel(round(x),y,mark);
x=x-m;mark=6;
}
}
}
}
int round(double y)
{
double fraction,integer;
fraction=modf(y,&integer);
if(fraction>=0.5)
return ((int)integer+1);
else
return ((int)integer);
}
void bres(int x1,int y1,int x2,int y2,int col)
{
int x,y,dx,dy,e_inc=0,e_no=0,e=0;
y=y1;
dx=x2-x1;
dy=y2-y1;
e_no=dy+dy;
e=e_no-dx;
e_inc=e-dx;
for(x=x1;x<x2;x++)
{
putpixel(x,y,col);
if(e<0)
{e=e+e_no;}
else
{
y=y+1;
e=e+e_inc;
}
}
}


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Intermediate category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments

1/23/2009 12:30:46 PMfurqan

it has been of great help to me in my academic studies...
(If this comment was disrespectful, please report it.)

 
1/23/2009 12:31:47 PMfurqan

it has been of great help to me in my stueids :)
(If this comment was disrespectful, please report it.)

 
2/15/2009 11:17:07 AMJohnson

The BRESANHAM algo doesn't draw vertical lines. Is there something wrong with the code or that its not possible using that algo.
(If this comment was disrespectful, please report it.)

 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.