Bresenham‘s Line Algorithm

/******************************************************************************
* 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);
        }
    }
}
 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值