optimize function draw_line

This commit is contained in:
idea4good 2020-04-01 12:31:33 +08:00
parent 570b42fb60
commit 4934965c52
2 changed files with 42 additions and 169 deletions

103
GuiLite.h
View File

@ -601,95 +601,32 @@ public:
}
void draw_line(int x1, int y1, int x2, int y2, unsigned int rgb, unsigned int z_order)
{
int dx, dy, e;
dx = x2 - x1;
dy = y2 - y1;
if ((dx >= 0) && (dy >= 0))
int dx, dy, x, y, e;
(x1 > x2) ? (dx = x1 - x2) : (dx = x2 - x1);
(y1 > y2) ? (dy = y1 - y2) : (dy = y2 - y1);
if (((dx > dy) && (x1 > x2)) || ((dx <= dy) && (y1 > y2)))
{
if (dx >= dy)
x = x2; y = y2;
x2 = x1; y2 = y1;
x1 = x; y1 = y;
}
x = x1; y = y1;
if (dx > dy)
{
e = dy - dx / 2;
for (; x1 <= x2; ++x1, e += dy)
{
e = dy - dx / 2;
for (; x1 <= x2; x1++, e += dy)
{
draw_pixel(x1, y1, rgb, z_order);
if (e > 0) { y1++; e -= dx; }
}
}
else
{
e = dx - dy / 2;
for (; y1 <= y2; y1++, e += dx)
{
draw_pixel(x1, y1, rgb, z_order);
if (e > 0) { x1++; e -= dy; }
}
draw_pixel(x1, y1, rgb, z_order);
if (e > 0) { e -= dx; (y > y2) ? --y1 : ++y1; }
}
}
else if ((dx >= 0) && (dy < 0))
else
{
dy = -dy;
if (dx >= dy)
e = dx - dy / 2;
for (; y1 <= y2; ++y1, e += dx)
{
e = dy - dx / 2;
for (; x1 <= x2; x1++, e += dy)
{
draw_pixel(x1, y1, rgb, z_order);
if (e > 0) { y1--; e -= dx; }
}
}
else
{
e = dx - dy / 2;
for (; y1 >= y2; y1--, e += dx)
{
draw_pixel(x1, y1, rgb, z_order);
if (e > 0) { x1++; e -= dy; }
}
}
}
else if ((dx < 0) && (dy >= 0))
{
dx = -dx;
if (dx >= dy)
{
e = dy - dx / 2;
for (; x1 >= x2; x1--, e += dy)
{
draw_pixel(x1, y1, rgb, z_order);
if (e > 0) { y1++; e -= dx; }
}
}
else
{
e = dx - dy / 2;
for (; y1 <= y2; y1++, e += dx)
{
draw_pixel(x1, y1, rgb, z_order);
if (e > 0) { x1--; e -= dy; }
}
}
}
else if ((dx < 0) && (dy < 0))
{
dx = -dx;
dy = -dy;
if (dx >= dy)
{
e = dy - dx / 2;
for (; x1 >= x2; x1--, e += dy)
{
draw_pixel(x1, y1, rgb, z_order);
if (e > 0) { y1--; e -= dx; }
}
}
else
{
e = dx - dy / 2;
for (; y1 >= y2; y1--, e += dx)
{
draw_pixel(x1, y1, rgb, z_order);
if (e > 0) { x1--; e -= dy; }
}
draw_pixel(x1, y1, rgb, z_order);
if (e > 0) { e -= dy; (x > x2) ? --x1 : ++x1; }
}
}
}

View File

@ -265,99 +265,35 @@ public:
void draw_line(int x1, int y1, int x2, int y2, unsigned int rgb, unsigned int z_order)
{
int dx, dy, e;
dx = x2 - x1;
dy = y2 - y1;
int dx, dy, x, y, e;
if ((dx >= 0) && (dy >= 0))
(x1 > x2) ? (dx = x1 - x2) : (dx = x2 - x1);
(y1 > y2) ? (dy = y1 - y2) : (dy = y2 - y1);
if (((dx > dy) && (x1 > x2)) || ((dx <= dy) && (y1 > y2)))
{
if (dx >= dy)
x = x2; y = y2;
x2 = x1; y2 = y1;
x1 = x; y1 = y;
}
x = x1; y = y1;
if (dx > dy)
{
e = dy - dx / 2;
for (; x1 <= x2; ++x1, e += dy)
{
e = dy - dx / 2;
for (; x1 <= x2; x1++, e += dy)
{
draw_pixel(x1, y1, rgb, z_order);
if (e > 0) { y1++; e -= dx; }
}
}
else
{
e = dx - dy / 2;
for (; y1 <= y2; y1++, e += dx)
{
draw_pixel(x1, y1, rgb, z_order);
if (e > 0) { x1++; e -= dy; }
}
draw_pixel(x1, y1, rgb, z_order);
if (e > 0) { e -= dx; (y > y2) ? --y1 : ++y1; }
}
}
else if ((dx >= 0) && (dy < 0))
else
{
dy = -dy;
if (dx >= dy)
e = dx - dy / 2;
for (; y1 <= y2; ++y1, e += dx)
{
e = dy - dx / 2;
for (; x1 <= x2; x1++, e += dy)
{
draw_pixel(x1, y1, rgb, z_order);
if (e > 0) { y1--; e -= dx; }
}
}
else
{
e = dx - dy / 2;
for (; y1 >= y2; y1--, e += dx)
{
draw_pixel(x1, y1, rgb, z_order);
if (e > 0) { x1++; e -= dy; }
}
}
}
else if ((dx < 0) && (dy >= 0))
{
dx = -dx;
if (dx >= dy)
{
e = dy - dx / 2;
for (; x1 >= x2; x1--, e += dy)
{
draw_pixel(x1, y1, rgb, z_order);
if (e > 0) { y1++; e -= dx; }
}
}
else
{
e = dx - dy / 2;
for (; y1 <= y2; y1++, e += dx)
{
draw_pixel(x1, y1, rgb, z_order);
if (e > 0) { x1--; e -= dy; }
}
}
}
else if ((dx < 0) && (dy < 0))
{
dx = -dx;
dy = -dy;
if (dx >= dy)
{
e = dy - dx / 2;
for (; x1 >= x2; x1--, e += dy)
{
draw_pixel(x1, y1, rgb, z_order);
if (e > 0) { y1--; e -= dx; }
}
}
else
{
e = dx - dy / 2;
for (; y1 >= y2; y1--, e += dx)
{
draw_pixel(x1, y1, rgb, z_order);
if (e > 0) { x1--; e -= dy; }
}
draw_pixel(x1, y1, rgb, z_order);
if (e > 0) { e -= dy; (x > x2) ? --x1 : ++x1; }
}
}
}