Jump to content
  • Advertisement
Sign in to follow this  
ashkash

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.

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
Advertisement
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!