### Highlights from Bresenham's Line

4.25
4.2 | 9 ratings Rate this file 19 Downloads (last 30 days) File Size: 2.18 KB File ID: #12939 Version: 1.0

# Bresenham's Line

### Narupon Chattrapiban (view profile)

08 Nov 2006 (Updated )

Generate a line profile of a 2d image

File Information
Description

Generate a line profile of a 2d image
using Bresenham's algorithm.

For a demo purpose, try

bresenham();

Acknowledgements

This file inspired Tactics Toolbox and 2 D Local Grid Map.

MATLAB release MATLAB 6.5 (R13)
06 Nov 2013 JK Hwang

### JK Hwang (view profile)

Hi, I think that your code is very helpful. However, I found a minor bug in the code.
----------
if bitshift(error,1) >= delx, % same as -> if 2*error >= delx,
y_n = y_n + ystep;
error = error - delx;
end

----------
In case of a coordinate of first point is (2,3) and second point is (3,11), it does not work and print out error message. I think this error caused by 'bitshift' function. I changed the function to '2*error', then the problem is solved.

28 Feb 2013 Raz Shimoni

### Raz Shimoni (view profile)

I have been using this function for some time now, and saw that other Bresenham's Line functions were more recently uploaded to MFE. I haven't compared performances yet, still this works great.

17 May 2010 Fernando Gonzalez

### Fernando Gonzalez (view profile)

12 Apr 2010 Christian

### Christian (view profile)

Hi! Nice Code!

However, i think there is a little bug:

----------
if steep,
myline(n) = mymat(x_n,y_n);
outmat(x_n,y_n) = 0;
X(n) = x_n;
Y(n) = y_n;
else
myline(n) = mymat(y_n,x_n);
outmat(y_n,x_n) = 0;
X(n) = y_n;
Y(n) = x_n;
end
-------

...this does not work for me, i think the code within the if and else statements should be swapped:

---------
if steep,
myline(n) = mymat(y_n,x_n);
outmat(y_n,x_n) = 0;
X(n) = y_n;
Y(n) = x_n;
else
myline(n) = mymat(x_n,y_n);
outmat(x_n,y_n) = 0;
X(n) = x_n;
Y(n) = y_n;
end
-------

Best,
Christian

12 Apr 2010 Christian

### Christian (view profile)

09 Oct 2009 Tina Hegarty

### Tina Hegarty (view profile)

Brilliant. You probably just saved me a few hours! Many thanks.

26 Mar 2007 Canuck Hanks

Very nice with demo! Thanks!

14 Mar 2007 Ryan Crow

Well done! Demo and visualization included.

27 Nov 2006 julio choque

excellent

09 Nov 2006 scardanzan ivan

an image with an example , please

Comment only