Bresenham's Line Algorithm

This topic is 4406 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

Hi am trying to write a program in C that uses Bresenham's line drawing algorithm to plot points. I have used some pseudocode that I have got from wikipedia to implement the alogorithm. The psuedocode is function line(x0, x1, y0, y1) boolean steep := abs(y1 - y0) > abs(x1 - x0) if steep then swap(x0, y0) swap(x1, y1) if x0 > x1 then swap(x0, x1) swap(y0, y1) int deltax := x1 - x0 int deltay := abs(y1 - y0) int error := 0 int ystep int y := y0 if y0 < y1 then ystep := 1 else ystep := -1 for x from x0 to x1 if steep then plot(y,x) else plot(x,y) error := error + deltay if 2×error ≥ deltax y := y + ystep error := error - deltax My program looks very similar yet when I give it points such as (0,0) to (10,20), (0,0) to (-20,10), (0,0) to (20,-10), (0,0) to (-10,20), (0,0) to (10,20), etc., it is not able to plot them. Any help would be appreciated, thanks.

Share on other sites
Define "not able to plot them". And seeing some actual code might help...

Share on other sites
I actually figured out the problem and it is working fine now. Basically I had a function that would print all the points needed to plot the line between two given points and when the two given points were the ones mentioned above it would not print any points. Now it works fine. The problem was the way I was implementing the swap function. thanks.

1. 1
2. 2
3. 3
Rutin
15
4. 4
khawk
13
5. 5
frob
12

• 9
• 9
• 11
• 11
• 23
• Forum Statistics

• Total Topics
633663
• Total Posts
3013236
×