• ### What is your GameDev Story?

#### Archived

This topic is now archived and is closed to further replies.

# Line Drawing Algorithms

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

## Recommended Posts

Hi there, I''ve been trying to use Bresenham''s line drawing algorithm in a 16-bit mode on a 640x480 screen. But when I use it, the diagonal lines have these gaping "holes" in them (a couple pixels wide at least). Needless to say, it looks like crap. Anyone have any suggestions? Sorry if this is a lame question, but I couldn''t find any decent answers elsewhere.

##### Share on other sites
post the crappy line plotter code you are using and maybe someone can point out what the problem(s) might be.

or email me the code to look over at orion76@hot.rr.com

I have a made a 16 bit color line plotter myself in Direct Draw 7
and it works fine

##### Share on other sites
I can think of several vague reasons why it would behave like that. So, can''t say anything until I see some code. A while ago I posted a complete line drawing function, which perhaps can help you. Look at the second-to-last post in this thread: http://www.gamedev.net/community/forums/topic.asp?topic_id=111209

##### Share on other sites
His problem with the gaps seems to be happenning because he's using a byte pointer to plot the pixels instead of using a word pointer or casting the byte pointer to a word pointer.

I had this sort of problem myself.

also the way the deltas are calculated

dx = x1 - x0; // if x1=0 and x1=2 then dx = 1 and not 2 pixels

should be

dx = x1 - x0 + 1; // this will calculate the range of pixels correctly

[edited by - CodeJunkie on December 8, 2002 8:26:06 PM]

##### Share on other sites
I''m not quite sure how Brensenham''s algorithim works myself, (if anyone could expalin it, that would be great) but shouldn''t the deltas be...

DeltaX = abs(EndX - StartX);
DeltaY = abs(EndY - StartY);

???

##### Share on other sites
exactly, thanks MattS423

otherwise a negative delta would cause weird side effects maybe even gaps

[EDIT]
this code would work as well
if(x1>=x0) // fixed it was wrong x1<=x0 sorry
{dx = x1 - x0 + 1;}
else
{dx = x0 - x1 + 1;}

and if the deltas aren't calculated with the right pixel ranges
the line's slope ratio will be off

Here's the heart of plotting a line:

    ShortAccum = 0; // maintains short/long range ratiopixels = longPixelRange;while(pixels--) // repeat until no more pixels to plot{ buffer[offset] = pixClr; // plot a pixel of course offset += longPixelDistance; // move to next pixel along the long range// Once the accumlator is equal or greater then the long range you can move towards the short distance ShortAccum += shortPixelRange;  if(ShortAccum>=longPixelRange)   {   offset += shortPixelDistance; // move to next pixel along the short range   ShortAccum -= longPixelRange; // maintain the short/long range ratio  }}

Well at least that's how I see it.

[edited by - CodeJunkie on December 8, 2002 9:12:21 PM]

[edited by - CodeJunkie on December 8, 2002 9:29:55 PM]

No problem.

##### Share on other sites
Well Code, I tried your WORD pointer idea such that:

*(WORD*)vbStart = color; // needs (WORD*), not (WORD)

and I divided the lpitch by two.

But, I get this weird-ass behaviour...For one, the coordinates seem off. And for things like polygons, they appear to be in two "pieces" across the screen from each other (right y coords, wrong x coords). I''m going to have a look at this, but God knows what''s going on. Oh the joys of it all.

##### Share on other sites
if you cast to a word pointer then don''t divide the pitch by 2 it can remain as it is

only if you choose to use a word pointer divide pitch by 2...Okay

sorry for the confusion

##### Share on other sites
Ok, well, I guess that really isn''t the problem then. I left the casting in *(WORD*)vbstart = color; and left the lpitch alone...But the gaping holes still appear. :\ I guess it''s back to the drawing board...Gah.

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 11
• 9
• 9
• 34
• 16
• ### Forum Statistics

• Total Topics
634123
• Total Posts
3015656
×