To obtain Line from a square grid

I need to find all the squares in the grid which include a part of the line drawn in a grid. Infact I found what I need to do exactly in the below link.

http://lifc.univ-fcomte.fr/~ededu/projects/bresenham/
(I am definitely not looking for Bresenham's algorithm)

There is an algorithm described which I find really confusing (all variables being of int type). I am not sure if its only for grid with squares of width 1 or any size.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
// 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; 
  int dy = y2 - y1; 
  POINT (y1, x1);  // 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 (y-ystep, x); 
        else if (error + errorprev > ddx)  // left square also 
          POINT (y, x-xstep); 
        else{  // corner: bottom and left squares also 
          POINT (y-ystep, x); 
          POINT (y, x-xstep); 
        } 
      } 
      POINT (y, x); 
      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 (y, x-xstep); 
        else if (error + errorprev > ddy) 
          POINT (y-ystep, x); 
        else{ 
          POINT (y, x-xstep); 
          POINT (y-ystep, x); 
        } 
      } 
      POINT (y, x); 
      errorprev = error; 
    } 
  } 
  // assert ((y == y2) && (x == x2));  // the last point (y2,x2) has to be the same with the last point of the algorithm 
} 



Can somebody help me plzz?
1
2
for (m=dy/dx, y=y1, t=round(x1); t<=x2; t++, y+=m) 
    glVertex2i(t,round(y));
This is the base of DDA (Digital Differential Analyzer), where x1,x2,y1,y2 are floats. You need to know which pixel need to be painted, so you rounded.
Bressenham use only integer arithmetic, because it multiplies every number by dx ( so now m=dy/dx*dx=dy) and 2 (avoid round)
So to know which pixel paint, it consults a decision variable (in this case error).

I am not sure if its only for grid with squares of width 1 or any size
Just width 1. (so change what the algorithm thinks is 1)
Topic archived. No new replies allowed.