Archived

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

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.

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 on other sites
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 loopingunsigned 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 multiplicationvb_start = vb_start + ((unsigned int)yo<<6) +                      ((unsigned int)yo<<8) +                      (unsigned int)xo;// compute deltasdx = x1-xo;dy = y1-yo;// SECTION 2 /////////////////////////////////////////////////////////////////// test which direction the line is going in i.e. slope angleif (dx>=0)   {   x_inc = 1;   } // end if line is moving rightelse   {   x_inc = -1;   dx    = -dx;  // need absolute value   } // end else moving left// SECTION 3 /////////////////////////////////////////////////////////////////// test y component of slopeif (dy>=0)   {   y_inc = 320; // 320 bytes per line   } // end if line is moving downelse   {   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 lineif (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/ <= 1else   {// 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

*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/

Thanks, it did.

• 24
• 24
• 9
• 15
• 60