isgoy
Joined: 29 Mar 2012 Posts: 13 Location: Spain
|
glcd_bar correct function |
Posted: Thu Mar 29, 2012 4:22 am |
|
|
I have found a bug in the "glcd_bar" function. For some angles it doesn't work well. This code runs correctly.
Code: |
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - //
// Purpose: Draw a bar (wide line) on a graphic LCD
// Inputs: (x1, y1) - the start coordinate
// (x2, y2) - the end coordinate
// width - The number of pixels wide
// color - ON or OFF
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - //
void glcd_bar(int x1, int y1, int x2, int y2, int width, int1 color)
{
signed int x, y, addx, addy, j;
signed long P, dx, dy;
int i;
dx = abs((signed int)(x2 - x1));
dy = abs((signed int)(y2 - y1));
x = x1;
y = y1;
if(x1 > x2)
{
addx = -1;
}
else
addx = 1;
if(y1 > y2)
{
addy = -1;
}
else
addy = 1;
if(dx >= dy)
{
P = 2*dy - dx;
for(i=0; i<=dx; ++i)
{
for(j=-(width/2); j<width/2+width%2; ++j)
{
glcd_pixel(x, y+j, color);
}
if(P < 0)
{
P += 2*dy;
x += addx;
}
else
{
P += 2*dy - 2*dx;
x += addx;
y += addy;
}
}
}
else
{
P = 2*dx - dy;
for(i=0; i<=dy; ++i)
{
if(P < 0)
{
P += 2*dx;
y += addy;
}
else
{
P += 2*dx - 2*dy;
x += addx;
y += addy;
}
for(j=-(width/2); j<width/2+width%2; ++j)
{
glcd_pixel(x+j, y, color);
}
}
}
} |
|
|