Important alert: (current site time 10/23/2014 4:55:15 PM EDT)
 

VB icon

Bresenham's Line Plotting Algorithm

Email
Submitted on: 12/22/2009 8:37:14 AM
By: Eric mrE521  
Level: Intermediate
User Rating: Unrated
Compatibility: C, C++ (general), Microsoft Visual C++
Views: 5093
author picture
 
     Lets you plot a line using Bresenham's line algorithm. Uses a callback to let you use a custom pixel plotting function
 

INCLUDE files:

Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
//**************************************
//INCLUDE files for :Bresenham's Line Plotting Algorithm
//**************************************
#include "stdlib.h"
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: Bresenham's Line Plotting Algorithm
// Description:Lets you plot a line using Bresenham's line algorithm. Uses a callback to let you use a custom pixel plotting function
// By: Eric mrE521
//
// Inputs:Coordinates and color of line, pixel plotting function
//
// Returns:The drawing of a line
//
// Assumes:You will need a pixel plotting function
//
//This code is copyrighted and has// limited warranties.Please see http://www.Planet-Source-Code.com/vb/scripts/ShowCode.asp?txtCodeId=12994&lngWId=3//for details.//**************************************

void lineBresenham(unsigned long x0, unsigned long y0, unsigned long x1,unsigned long y1, unsigned char r, unsigned char g, unsigned char b, void (__stdcall*plotter)(unsigned long x,unsigned long y,unsigned char r,unsigned char g,unsigned char b))
{
	int steep = abs(y1 - y0) > abs(x1 - x0);
	if(steep)
	{
		unsigned long temp = x0;
		x0 = y0;
		y0 = temp;
		temp = x1;
		x1 = y1;
		y1 = temp;
	}
	if(x0 > x1)
	{
		unsigned long temp = x0;
		x0 = x1;
		x1 = temp;
		temp = y0;
		y0 = y1;
		y1 = temp;
	}
	long deltax = x1 - x0;
	long deltay = abs(y1 - y0);
	long error = deltax >> 1;//Same as division by 2
	long ystep;
	long y = y0;
	if(y0 < y1) ystep = 1;
	else ystep = -1;
	if(steep)
		for(unsigned long x = x0; x < x1; x++)
		{
			plotter(y,x,r,g,b);
			error -= deltay;
			if(error < 0)
			{
				y += ystep;
				error += deltax;
			}
		}
	else
		for(unsigned long x = x0; x < x1; x++)
		{
			plotter(x,y,r,g,b);
			error -= deltay;
			if(error < 0)
			{
				y += ystep;
				error += deltax;
			}
		}
}


Other 2 submission(s) by this author

 


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


 There are no comments on this submission.
 

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.