Rolpege

Faster Tilemap.line

Jul 25th, 2011
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.          /**
  2.          * Draws a line of tiles
  3.          *  
  4.          * @param   x1      The x coordinate to start
  5.          * @param   y1      The y coordinate to start
  6.          * @param   x2      The x coordinate to end
  7.          * @param   y2      The y coordinate to end
  8.          * @param   id      The tiles id to draw
  9.          *
  10.          */    
  11.         public function line(x1:int, y1:int, x2:int, y2:int, id:int):void
  12.         {
  13.             if(usePositions)
  14.             {
  15.                 x1 /= _tile.width;
  16.                 y1 /= _tile.height;
  17.                 x2 /= _tile.width;
  18.                 y2 /= _tile.height;
  19.             }
  20.            
  21.             x1 %= _columns;
  22.             y1 %= _rows;
  23.             x2 %= _columns;
  24.             y2 %= _rows;
  25.            
  26.             // get the drawing difference
  27.             var X:Number = Math.abs(x2 - x1),
  28.                 Y:Number = Math.abs(y2 - y1),
  29.                 xx:int, yy:int;
  30.            
  31.             // draw a single pixel
  32.             if (X == 0)
  33.             {
  34.                 if (Y == 0)
  35.                 {
  36.                     setTile(x1, y1, id);
  37.                     return;
  38.                 }
  39.                 // draw a straight vertical line
  40.                 yy = y2 > y1 ? 1 : -1;
  41.                 while (y1 != y2)
  42.                 {
  43.                     setTile(x1, y1, id);
  44.                     y1 += yy;
  45.                 }
  46.                 setTile(x2, y2, id);
  47.                 return;
  48.             }
  49.            
  50.             if (Y == 0)
  51.             {
  52.                 // draw a straight horizontal line
  53.                 xx = x2 > x1 ? 1 : -1;
  54.                 while (x1 != x2)
  55.                 {
  56.                     setTile(x1, y1, id);
  57.                     x1 += xx;
  58.                 }
  59.                 setTile(x2, y2, id);
  60.                 return;
  61.             }
  62.            
  63.             xx = x2 > x1 ? 1 : -1;
  64.             yy = y2 > y1 ? 1 : -1;
  65.             var c:Number = 0,
  66.                 slope:Number;
  67.            
  68.             if (X > Y)
  69.             {
  70.                 slope = Y / X;
  71.                 c = .5;
  72.                 while (x1 != x2)
  73.                 {
  74.                     setTile(x1, y1, id);
  75.                     x1 += xx;
  76.                     c += slope;
  77.                     if (c >= 1)
  78.                     {
  79.                         y1 += yy;
  80.                         c -= 1;
  81.                     }
  82.                 }
  83.                 setTile(x2, y2, id);
  84.             }
  85.             else
  86.             {
  87.                 slope = X / Y;
  88.                 c = .5;
  89.                 while (y1 != y2)
  90.                 {
  91.                     setTile(x1, y1, id);
  92.                     y1 += yy;
  93.                     c += slope;
  94.                     if (c >= 1)
  95.                     {
  96.                         x1 += xx;
  97.                         c -= 1;
  98.                     }
  99.                 }
  100.                 setTile(x2, y2, id);
  101.             }
  102.         }
Advertisement
Add Comment
Please, Sign In to add comment