You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The Peggy2.cpp 0.30b line() function was returning some strange results.
For example, (10,0) to (11,24) resulted in all points from (10,0) to
(10,23), and point (11,24) being activated. To me it would be better to
have the 'split' show up in the middle - something like (10,0)-(10,12) and
(11,13)-(11,24).
A simple change in the library function made this work. See the "Count=
(dx-dy)/2" line and its counterpart in the snippet below.
Cheers,
Tim
void Peggy2::Line(int8_t x1, int8_t y1, int8_t x2, int8_t y2)
{
if ( (x1>=25 && x2>=25) || (y1>=25 && y2>=25) ) return;
int8_t dx = abs(x2 -x1);
int8_t dy = abs(y2 -y1);
int8_t p1x,p1y,p2x,p2y,i;
if (dx > dy)
{
if (x2>x1) {
p1x=x1;
p1y=y1;
p2x=x2;
p2y=y2;
}
else {
p1x=x2;
p1y=y2;
p2x=x1;
p2y=y1;
}
int8_t y = p1y;
int8_t x = p1x;
int8_t count = 0;
int8_t increment = p2y > p1y ? 1 : -1;
count=(dx-dy)/2;
for (i=0; i<=dx; i++)
{
count += dy;
if (count > dx)
{
count -= dx;
y+= increment;
}
if (y>=0 && y<25 && x>=0 && x<25)
SetPoint(x,y);
x++;
if (x>=25)
break;
}
}
else
{
if (y2>y1) {
p1x=x1;
p1y=y1;
p2x=x2;
p2y=y2;
}
else {
p1x=x2;
p1y=y2;
p2x=x1;
p2y=y1;
}
int8_t y = p1y;
int8_t x = p1x;
int8_t count = 0;
int8_t increment = p2x > p1x ? 1 : -1;
count=(dy-dx)/2;
for (i=0; i<=dy; i++)
{
count += dx;
if (count > dy)
{
count -= dy;
x+= increment;
}
if (y>=0 && y<25 && x>=0 && x<25) SetPoint(x,y);
y+=1;
if (y>=25) break;
}
}
}
Original issue reported on code.google.com by [email protected] on 5 Jan 2010 at 4:36
The text was updated successfully, but these errors were encountered:
I agree that this looks better. One improvement to this code would be to use a
bit shift instead of division-- it's
much faster on the AVR. The assignment can also be combined with declaration:
int8_t count = (dx-dy) >> 1;
Perhaps surprising, but just switching from division to a bit shift actually
saves 86 bytes of program space.
Original issue reported on code.google.com by
[email protected]
on 5 Jan 2010 at 4:36The text was updated successfully, but these errors were encountered: