|
如图所示,画的直线在第2点开始会向左和向下各偏移一个像素。
下面的感觉更好用。
void LCD_DrawLine(uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2, uint16_t color)
{
uint16_t t;
int xerr = 0, yerr = 0, delta_x, delta_y, distance;
int incx, incy, uRow, uCol;
delta_x = x2 - x1; //计算坐标增量
delta_y = y2 - y1;
uRow = x1;
uCol = y1;
if (delta_x > 0)
{
incx = 1; //设置单步方向
}
else if (delta_x == 0)
{
incx = 0; //垂直线
}
else
{
incx = -1;
delta_x = -delta_x;
}
if (delta_y > 0)
{
incy = 1;
}
else if (delta_y == 0)
{
incy = 0; //水平线
}
else
{
incy = -1;
delta_y = -delta_y;
}
//选取基本增量坐标轴
if ( delta_x > delta_y)
{
distance = delta_x;
}
else
{
distance = delta_y;
}
for (t = 0; t <= distance + 1; t++)
{
LCD_SetPixel(uRow, uCol, color); //画点
xerr += delta_x;
yerr += delta_y;
if (xerr > distance)
{
xerr -= distance;
uRow += incx;
}
if (yerr > distance)
{
yerr -= distance;
uCol += incy;
}
}
}
|
|