Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Peggy2 0.30b line() improvement #3

Open
GoogleCodeExporter opened this issue Aug 20, 2015 · 1 comment
Open

Peggy2 0.30b line() improvement #3

GoogleCodeExporter opened this issue Aug 20, 2015 · 1 comment

Comments

@GoogleCodeExporter
Copy link

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

@GoogleCodeExporter
Copy link
Author

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 comment by [email protected] on 7 Jan 2010 at 5:06

  • Changed state: Started

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant