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;
}