~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~ [ freetext search ] ~ [ file search ] ~

Linux Cross Reference
Tina4/src/math/draw/bres_line.c

Version: ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  1 /* @(#)Bresenham's line-drawing algorithm - blame Jim Ivins for any
  2  * bugs. Apply func to data at integer coordinates on a straight line
  3  * between p1 and p2 inclusive.  Plot an 8-way connected line if flag is
  4  * zero; else use 4-way connections. */
  5 
  6 #include <tina/math.h>
  7 
  8 void    apply_bresenham_line(Vec2 p1, Vec2 p2, int flag,
  9                                      void (*func) (), void *data)
 10 {
 11     int     temp, delta, pos_e, neg_e, tot_e;
 12 
 13     int     x1 = (int) vec2_x(p1);
 14     int     y1 = (int) vec2_y(p1);
 15     int     x2 = (int) vec2_x(p2);
 16     int     y2 = (int) vec2_y(p2);
 17 
 18     int     dx = (x1 > x2) ? (x1 - x2) : (x2 - x1);
 19     int     dy = (y1 > y2) ? (y1 - y2) : (y2 - y1);
 20 
 21     if (dy <= dx)
 22     {
 23         tot_e = 2 * dy - dx;
 24         pos_e = 2 * dy;
 25         neg_e = 2 * (dy - dx);
 26 
 27         if (x1 > x2)
 28         {
 29             temp = x1;
 30             x1 = x2;
 31             x2 = temp;
 32             temp = y1;
 33             y1 = y2;
 34             y2 = temp;
 35         }
 36         func(x1, y1, data);
 37 
 38         delta = (y1 < y2) ? +1 : -1;
 39 
 40         while (x1++ < x2)
 41         {
 42             if (tot_e < 0)
 43                 tot_e += pos_e;
 44             else
 45             {
 46                 tot_e += neg_e;
 47                 if (flag)
 48                     func(x1, y1, data);
 49                 y1 += delta;
 50             }
 51             func(x1, y1, data);
 52         }
 53     } else
 54     {
 55         tot_e = 2 * dx - dy;
 56         pos_e = 2 * dx;
 57         neg_e = 2 * (dx - dy);
 58 
 59         if (y1 > y2)
 60         {
 61             temp = y1;
 62             y1 = y2;
 63             y2 = temp;
 64             temp = x1;
 65             x1 = x2;
 66             x2 = temp;
 67         }
 68         func(x1, y1, data);
 69 
 70         delta = (x1 < x2) ? +1 : -1;
 71 
 72         while (y1++ < y2)
 73         {
 74             if (tot_e < 0)
 75                 tot_e += pos_e;
 76             else
 77             {
 78                 tot_e += neg_e;
 79                 if (flag)
 80                     func(x1, y1, data);
 81                 x1 += delta;
 82             }
 83             func(x1, y1, data);
 84         }
 85     }
 86 }
 87 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~ [ freetext search ] ~ [ file search ] ~

This page was automatically generated by the LXR engine.
Visit the LXR main site for more information.