我正在编写一个程序,用Tiva C (Tm4C123GXL)在液晶屏幕(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))(我希望速度达到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);
}发布于 2015-05-25 06:16:58
类型问题。当dy/dx小于1时,M的结果为0。因此,将它们设置为浮动以获得浮点数。
发布于 2015-05-25 03:24:57
如果这将是一个bresenham线绘制算法注意,它只工作在45度。(总是想知道为什么ppl甚至不首先看一下WP。)不确定,但因为你的姓氏有德国血统:你也会在德国的WP上找到这个。
对于其他角度,您必须交换/重新排序坐标。如果性能有问题的话,对hor/vert和对角线有单独的绘图算法可能更好(但由于使用了double,显然根本就没有)。
https://stackoverflow.com/questions/30430618
复制相似问题