Archived

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

pointguard1

Line Algorithm

Recommended Posts

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


Link to post
Share on other sites
Null and Void    1088
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 this post


Link to post
Share on other sites
Xsist10    122

[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]

Share this post


Link to post
Share on other sites