Jump to content
  • Advertisement

Archived

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

pointguard1

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.

If you intended to correct an error in the post then please contact us.

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


Link to post
Share on other sites
Advertisement
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

[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

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!