Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Draws a line of tiles
- *
- * @param x1 The x coordinate to start
- * @param y1 The y coordinate to start
- * @param x2 The x coordinate to end
- * @param y2 The y coordinate to end
- * @param id The tiles id to draw
- *
- */
- public function line(x1:int, y1:int, x2:int, y2:int, id:int):void
- {
- if(usePositions)
- {
- x1 /= _tile.width;
- y1 /= _tile.height;
- x2 /= _tile.width;
- y2 /= _tile.height;
- }
- x1 %= _columns;
- y1 %= _rows;
- x2 %= _columns;
- y2 %= _rows;
- // get the drawing difference
- var X:Number = Math.abs(x2 - x1),
- Y:Number = Math.abs(y2 - y1),
- xx:int, yy:int;
- // draw a single pixel
- if (X == 0)
- {
- if (Y == 0)
- {
- setTile(x1, y1, id);
- return;
- }
- // draw a straight vertical line
- yy = y2 > y1 ? 1 : -1;
- while (y1 != y2)
- {
- setTile(x1, y1, id);
- y1 += yy;
- }
- setTile(x2, y2, id);
- return;
- }
- if (Y == 0)
- {
- // draw a straight horizontal line
- xx = x2 > x1 ? 1 : -1;
- while (x1 != x2)
- {
- setTile(x1, y1, id);
- x1 += xx;
- }
- setTile(x2, y2, id);
- return;
- }
- xx = x2 > x1 ? 1 : -1;
- yy = y2 > y1 ? 1 : -1;
- var c:Number = 0,
- slope:Number;
- if (X > Y)
- {
- slope = Y / X;
- c = .5;
- while (x1 != x2)
- {
- setTile(x1, y1, id);
- x1 += xx;
- c += slope;
- if (c >= 1)
- {
- y1 += yy;
- c -= 1;
- }
- }
- setTile(x2, y2, id);
- }
- else
- {
- slope = X / Y;
- c = .5;
- while (y1 != y2)
- {
- setTile(x1, y1, id);
- y1 += yy;
- c += slope;
- if (c >= 1)
- {
- x1 += xx;
- c -= 1;
- }
- }
- setTile(x2, y2, id);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment