#### Archived

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

# Line Algorithm

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

## Recommended Posts

I''m a newb. I read this somewhere but didn''t write it down. What''s the line drawing algorithm? it should be a function that takes X and Y args thanks Newbie to game programming? visit KaBooMgames Great for Newbs, Masters welcome as well

##### Share on other sites
This works if the slope is greater than 0 and less than 1, it''s normally known as Bresenham''s Line-Drawing Algorithm:
  void Line(int xa, int ya, int xb, int yb) { int dx = abs(xa-xb), dy = abs(ya-yb); int p = 2*dy-dx; int twoDy = 2*dy, twoDyDx = 2*(dy-dx); int x, y, xEnd; if(xa > xb) { x = xb; y = yb; xEnd = xa; } else { x = xa; y = ya; xEnd = yb; } SetPixel(x,y); while(x < xEnd) { x++; if(p<0) p += twoDy; else { y++; p += twoDyDx; } SetPixel(x,y); }}

[/source]

[Resist Windows XP''s Invasive Production Activation Technology!]

##### Share on other sites
Thanks

Newbie to game programming?
visit
KaBooMgames
Great for Newbs, Masters welcome as well

##### Share on other sites
[pre]void line(float x1, float y1, float x2, float y2){   double hl=fabs(x2-x1);   double vl=fabs(y2-y1);   double length=hl;   float  deltax=(x2-x1)/(float)length;   float  deltay=(y2-y1)/(float)length;             if (hl < vl)      length = vl;   for (int i=0; i<(int)length; i++)   {      unsigned long x=(int)(x1+=deltax);      unsigned long  y=(int)(y1+=deltay);      if ((x<640)&&(y<480)) SetPixel(x,y);   }} [/pre]

1. 1
2. 2
3. 3
4. 4
Rutin
18
5. 5

• 11
• 12
• 9
• 12
• 37
• ### Forum Statistics

• Total Topics
631420
• Total Posts
2999989
×