/******************************************************************************
* Function Name : LCD_DrawLine
* Description : Bresenham's line algorithm
* Input : - x0:
* - y0:
* - x1:
* - y1:
* - color:
* Output : None
* Return : None
* Attention : None
*******************************************************************************/
void LCD_DrawLine(uint16_t x0, uint16_t y0, uint16_t x1, uint16_t y1, uint16_t color)
{
short dx, dy;
short temp;
if (x0 > x1)
{
temp = x1;
x1 = x0;
x0 = temp;
temp = y1;
y1 = y0;
y0 = temp;
}
dx = x1 - x0;
dy = y1 - y0;
if (dx == 0 && dy > 0)
{
do
{
LCD_SetPoint(x0, y0, color);
y0++;
} while (y1 >= y0);
return;
}
else if (dx == 0 && dy < 0)
{
do
{
LCD_SetPoint(x0, y0, color);
y0--;
} while (y0 >= y1);
}
else if (dy == 0)
{
do
{
LCD_SetPoint(x0, y0, color);
x0++;
} while (x1 >= x0);
return;
}
/* Bresenham's line algorithm */
else if (dy > 0)
{
if (dx > dy)
{
temp = 2 * dy - dx;
while (x0 != x1)
{
LCD_SetPoint(x0, y0, color);
x0++;
if (temp > 0)
{
y0++;
temp += 2 * dy - 2 * dx;
}
else
{
temp += 2 * dy;
}
}
LCD_SetPoint(x0, y0, color);
}
else
{
temp = 2 * dx - dy;
while (y0 != y1)
{
LCD_SetPoint(x0, y0, color);
y0++;
if (temp > 0)
{
x0++;
temp += 2 * dx - 2 * dy;
}
else
{
temp += 2 * dx;
}
}
LCD_SetPoint(x0, y0, color);
}
}
else if (dy < 0)
{
if (dx > -dy)
{
temp = -2 * dy - dx;
while (x0 != x1)
{
LCD_SetPoint(x0, y0, color);
x0++;
if (temp > 0)
{
y0--;
temp += -2 * dy - 2 * dx;
}
else
{
temp += -2 * dy;
}
}
LCD_SetPoint(x0, y0, color);
}
else
{
temp = 2 * dx + dy;
while (y0 != y1)
{
LCD_SetPoint(x0, y0, color);
y0--;
if (temp > 0)
{
x0++;
temp += -2 * dx - 2 * dy;
}
else
temp += -2 * dx;
}
LCD_SetPoint(x0, y0, color);
}
}
}