Jump to content
  • Advertisement

Archived

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

acw83

Code to draw a 2D line.

This topic is 6938 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

I am going to kill someone if I can''t get help with this. My head hurts from trying to figure this out and after a lot of coding and recoding, I can''t get it right. Here is my function: void PutLine(int32* pBuffer, int x1, int y1, int x2, int y2, int color) { // FILL ME IN!!! } Please someone help me out! I need one that''ll draw any kind of 2D line. Spped isn''t an issue, just one that works!!!

Share this post


Link to post
Share on other sites
Advertisement
What you''re looking for is the Bresenham Line Algorithm. This is code from one of Andre LaMothe''s books, so it''s in 8-bit color, but you should be able to adapt it...


void Bline(int xo, int yo, int x1,int y1, unsigned char color)
{
// this function uses Bresenham''s algorithm IBM (1965) to draw a line from
// (xo,yo) - (x1,y1)

int dx, // difference in x''s
dy, // difference in y''s
x_inc, // amount in pixel space to move during drawing
y_inc, // amount in pixel space to move during drawing
error=0, // the discriminant i.e. error i.e. decision variable
index; // used for looping



unsigned char far *vb_start = video_buffer; // directly access the video
// buffer for speed


// SECTION 1 /////////////////////////////////////////////////////////////////

// pre-compute first pixel address in video buffer
// use shifts for multiplication


vb_start = vb_start + ((unsigned int)yo<<6) +
((unsigned int)yo<<8) +
(unsigned int)xo;

// compute deltas

dx = x1-xo;
dy = y1-yo;

// SECTION 2 /////////////////////////////////////////////////////////////////

// test which direction the line is going in i.e. slope angle

if (dx>=0)
{
x_inc = 1;

} // end if line is moving right
else
{
x_inc = -1;
dx = -dx; // need absolute value

} // end else moving left

// SECTION 3 /////////////////////////////////////////////////////////////////

// test y component of slope

if (dy>=0)
{
y_inc = 320; // 320 bytes per line

} // end if line is moving down
else
{
y_inc = -320;
dy = -dy; // need absolute value

} // end else moving up

// SECTION 4 /////////////////////////////////////////////////////////////////

// now based on which delta is greater we can draw the line

if (dx>dy)
{

// draw the line

for (index=0; index<=dx; index++)
{
// set the pixel

*vb_start = color;

// adjust the discriminate

error+=dy;

// test if error overflowed

if (error>dx)
{

error-=dx;

// move to next line

vb_start+=y_inc;

} // end if error overflowed

// move to the next pixel

vb_start+=x_inc;

} // end for

} // end if /slope/ <= 1
else
{

// SECTION 5 /////////////////////////////////////////////////////////////////

// draw the line

for (index=0; index<=dy; index++)
{
// set the pixel

*vb_start = color;

// adjust the discriminate

error+=dx;

// test if error overflowed

if (error>0)
{

error-=dy;

// move to next line

vb_start+=x_inc;

} // end if error overflowed

// move to the next pixel

vb_start+=y_inc;

} // end for

} // end else /slope/ > 1

} // end Bline


Some things about the code:
*320 is the width of the screen in this case. You can change it to the pitch of the destination surface.
*video_buffer would be replaced with your pBuffer.
*the code needs to be changed from specialization for 8-bit color to 32-bit color.

Hope this helps

lntakitopi@aol.com
http://geocities.com/guanajam/

Share this post


Link to post
Share on other sites

  • 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!