繁体   English   中英

c绘制功能无法正确绘制

[英]c draw function not drawing properly

我正在尝试编写一个程序,该程序使用Tiva C(Tm4C123GXL)将数字汽车speedo写入LCD屏幕(ST7735)。 附带的代码是画线功能,应在两个距离之间画一条直线。 如果我将(speed_x1,speed_y1,80,60,ST7735_WHITE)放入函数中,直到45度,绘制的线都是水平的,而不是应该成角度的。 在45度直至90度后,绘图是好的,然后在90度后再次断裂。

speed_x1 = 80 - 55 * cos((PI / 180) * (speed * 1.8))
speed_y1 = 60 - 55 * sin((PI / 180) * (speed * 1.8))

(我希望speedo最高达到100,因此速度* 1.8是1.8降/公里/小时)

在这里解决我的问题的任何帮助将不胜感激。 谢谢 :)

 void ST7735_DrawLine(short x1, short y1, short x2, short y2, unsigned short color) {
        // unsigned char hi = color >> 8, lo = color;
        //int x=x1;
        //int y=y1;

        int dy = y2 - y1;
        int dx = x2 - x1;
        double m = dy / dx;
        double c = y1 - m * x1;
        if ((x1 >= _width) || (y1 >= _height) || (x2 >= _width) || (y2 >= _height) ) return; 
        setAddrWindow(x1, y1, x1 + x2 - 1, y2);
        while(x1 <= x2)
        {
            if (m <= 1)
            {
                x1 = x1 + 1;
                y1 = m * x1 + c;
                ST7735_DrawPixel(x1,y1,color);
            }
            else
            {
                y1 = y1 + 1;
                x1 = (y1 - c) / m;
                ST7735_DrawPixel(x1,y1,color);
            }
        } 
    }

void ST7735_DrawPixel(short x, short y, unsigned short color) {
    if ((x < 0) || (x >= _width) || (y < 0) || (y >= _height))
        return;
    setAddrWindow(x,y,x+1,y+1);
    pushColor(color);
}

类型转换问题。 当dy / dx小于1时,M的结果为0。 将它们转换为浮点数,以得到浮点数。

如果那是布雷森汉姆(Bresenham)线描算法,请注意它仅在45度范围内有效。 (总是想知道为什么ppl甚至不首先查看WP。)不确定,但是由于您的姓氏是德国血统:您也会在WP上以德语找到它。

对于其他角度,您将不得不交换/重新排列坐标。 如果性能是一个问题,最好对hor / vert和对角线使用单独的绘制算法可能更好(但是当您使用double时,显然根本没有)。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM