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

Draw tiles using line tool #6

Open
yuripourre opened this issue Sep 23, 2015 · 0 comments
Open

Draw tiles using line tool #6

yuripourre opened this issue Sep 23, 2015 · 0 comments

Comments

@yuripourre
Copy link
Member

Let user draw more tiles per click

Hint: Use Bresenham Algorithm as in http://eugen.dedu.free.fr/projects/bresenham/

// use Bresenham-like algorithm to print a line from (y1,x1) to (y2,x2)
// The difference with Bresenham is that ALL the points of the line are
//   printed, not only one per x coordinate.
// Principles of the Bresenham's algorithm (heavily modified) were taken from:
//   http://www.intranet.ca/~sshah/waste/art7.html
void useVisionLine (int y1, int x1, int y2, int x2)
{
  int i;               // loop counter
  int ystep, xstep;    // the step on y and x axis
  int error;           // the error accumulated during the increment
  int errorprev;       // *vision the previous value of the error variable
  int y = y1, x = x1;  // the line points
  int ddy, ddx;        // compulsory variables: the double values of dy and dx
  int dx = x2 - x1; //(dx/=tile.w)
  int dy = y2 - y1; //(dy/=tile.h)
  POINT (x1, y1);  // first point
  // NB the last point can't be here, because of its previous point (which has to be verified)
  if (dy < 0) {
    ystep = -1;
    dy = -dy;
  } else {
    ystep = 1;
  }
  if (dx < 0) {
    xstep = -1;
    dx = -dx;
  } else {
    xstep = 1;
  }

  ddy = 2 * dy;  // work with double values for full precision
  ddx = 2 * dx;
  if (ddx >= ddy){  // first octant (0 <= slope <= 1)
    // compulsory initialization (even for errorprev, needed when dx==dy)
    errorprev = error = dx;  // start in the middle of the square
    for (i=0 ; i < dx ; i++){  // do not use the first point (already done)
      x += xstep;
      error += ddy;
      if (error > ddx){  // increment y if AFTER the middle ( > )
        y += ystep;
        error -= ddx;
        // three cases (octant == right->right-top for directions below):
        if (error + errorprev < ddx)  // bottom square also
          POINT (x, y-ystep);
        else if (error + errorprev > ddx)  // left square also
          POINT (x-xstep, y);
        else{  // corner: bottom and left squares also
          POINT (x, y-ystep);
          POINT (x-xstep, y);
        }
      }
      POINT (x, y);
      errorprev = error;
    }
  }else{  // the same as above
    errorprev = error = dy;
    for (i=0 ; i < dy ; i++){
      y += ystep;
      error += ddx;
      if (error > ddy){
        x += xstep;
        error -= ddy;
        if (error + errorprev < ddy)
          POINT (x-xstep, y);
        else if (error + errorprev > ddy)
          POINT (x, y-ystep);
        else{
          POINT (x-xstep, y);
          POINT (x, y-ystep);
        }
      }
      POINT (x, y);
      errorprev = error;
    }
  }
  // assert ((y == y2) && (x == x2));  // the last point (y2,x2) has to be the same with the last point of the algorithm
} 
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant