Quick line drawing algorithmus needed
Displaying 1-7 of 7 total.
1
Please enter a numerical value for the importance of this sticky.
Enter 0 to unsticky.
Buckermann

Let's say I want to draw a line from (x5, y9) to (x8, y7), has anybody a quick algorithmus to find all points on the line between the two coordinates?

Posted on 2004-05-29 20:16:23

mcgrue


void Line(int x1, int y1, int x2, int y2, int color, int destimage);


...or am I missing something about your question?

Posted on 2004-05-29 20:44:31

Buckermann

Quote:Originally posted by mcgrue


void Line(int x1, int y1, int x2, int y2, int color, int destimage);
?

I guess I should have been more specific.
I needed a line algorithmus to find every point between (x1, y1) and (x2,y2). But not to draw a simple line. Of course, that's what the Line function is for.

But I already found one on GameDev.net (where elese?) and it works great.

Posted on 2004-05-29 21:09:54

RageCage

you shoulda read the post before yours... =p

http://www.verge-rpg.com/boards/display_thread.php?id=14431&PHPSESSID=c0644dfcc36f434521245d90a67a136f

edit: bah I'll full of stupid answers... this was like 3 posts after this thread, I just wasnt aware of it! well.. .uhh... my algorithm is cooler =p

Posted on 2004-05-29 23:31:58 (last edited on 2004-05-29 23:41:39)

Buckermann

Well, your line algorithmus may be well the best peice of code I have ever seen.
The only problem is, I don't fully understand it. And I'm hesitant to use something I don't understand.
I like it easy, like this:

//absolute value
int abs(int a)
{
if (a<0)
return 0-a;
else
return a;
}


int TB_LineDraw (int x1, int y1, int x2, int y2, int Image)
{
int deltax = abs(x2-x1); // The difference between the x's
int deltay = abs(y2-y1); // The difference between the y's
int x = x1; // Start x off at the first pixel
int y = y1; // Start y off at the first pixel
int xinc1;
int xinc2;
int yinc1;
int yinc2;
int num;
int numadd;
int numpixels;
int den;
int curpixel;
Log("Linedraw from "+str(x1)+","+str(y1)+" to "+str(x2)+","+str(y2));

if (x2 >= x1) // The x-values are increasing
{
xinc1 = 1;
xinc2 = 1;
}
else // The x-values are decreasing
{
xinc1 = 0-1;
xinc2 = 0-1;
}

if (y2 >= y1) // The y-values are increasing
{
yinc1 = 1;
yinc2 = 1;
}
else // The y-values are decreasing
{
yinc1 = 0-1;
yinc2 = 0-1;
}

if (deltax >= deltay) // There is at least one x-value for every y-value
{
xinc1 = 0; // Don't change the x when numerator >= denominator
yinc2 = 0; // Don't change the y for every iteration
den = deltax;
num = deltax / 2;
numadd = deltay;
numpixels = deltax; // There are more x-values than y-values
}
else // There is at least one y-value for every x-value
{
xinc2 = 0; // Don't change the x for every iteration
yinc1 = 0; // Don't change the y when numerator >= denominator
den = deltay;
num = deltay / 2;
numadd = deltax;
numpixels = deltay; // There are more y-values than x-values
}
Log("Numpixels ="+str(numpixels));
for (curpixel = 0; curpixel <= numpixels; curpixel++)
{

Tblit(x*16, y*16, image, screen); //blit the image along the line

num += numadd; // Increase the numerator by the top of the fraction
if (num >= den) // Check if numerator >= denominator
{
num -= den; // Calculate the new numerator value
x += xinc1; // Change the x as appropriate
y += yinc1; // Change the y as appropriate
}
x += xinc2; // Change the x as appropriate
y += yinc2; // Change the y as appropriate
}
return numpixels;
}

Posted on 2004-05-29 23:57:21

RageCage

wow, that's huge =p

Posted on 2004-05-30 05:35:50

Buckermann

Quote:Originally posted by RageCage

wow, that's huge =p

Yep, but it makes it easy to understand for the less gifted... like me :D

Posted on 2004-05-30 13:47:15


Displaying 1-7 of 7 total.
1
 
Newest messages

Ben McGraw's lovingly crafted this website from scratch for years.
It's a lot prettier this go around because of Jon Wofford.
Verge-rpg.com is a member of the lunarnet irc network, and would like to take this opportunity to remind you that regardless how babies taste, it is wrong to eat them.