Advertisement
Guest User

func_line_algorithm

a guest
Mar 23rd, 2015
359
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.01 KB | None | 0 0
  1. /*
  2.     File: ffa_func_line_algorithm.sqf
  3.     Author: Wikipedia
  4.     Used  Bresenham's line algorithm.
  5.     Edit: Dimon UA
  6.    
  7.     Description:
  8.     Creating line
  9.  
  10.     Parameter(s):
  11.     _this select 0: position (Array)
  12.     _this select 1: position (Array)
  13.  
  14.     Returns:
  15.     Array - format Position2D
  16. */
  17. ffa_func_line_algorithm = {
  18.     line_algorithm = {
  19.         private ["_pos", "_this", "_end", "_angle", "_xstart", "_ystart", "_xend", "_yend", "_dx", "_dy", "_ind", "_incx",
  20.         "_incy", "_pdx", "_pdy", "_es", "_el", "_x", "_y", "_err", "_arr"];
  21.         _pos = (_this select 0) select 0;
  22.         _end = (_this select 0) select 1;
  23.         _step = _this select 1;
  24.         _ind = 0;
  25.         _angle =[_pos, _end] call BIS_fnc_dirTo;
  26.         _xstart = _pos select 0;
  27.         _ystart = _pos select 1;
  28.  
  29.         _xend  = _end select 0;
  30.         _yend = _end select 1;
  31.  
  32.         _dx = _xend - _xstart;//проекция на ось икс
  33.         _dy = _yend - _ystart;//проекция на ось игрек
  34.  
  35.         if (_dx < 0) then {
  36.             _incx =-1;
  37.         }else{
  38.             _incx =1;
  39.         };
  40.         /*
  41.          * Определяем, в какую сторону нужно будет сдвигаться. Если dx < 0, т.е. отрезок идёт
  42.          * справа налево по иксу, то incx будет равен -1.
  43.          * Это будет использоваться в цикле постороения.
  44.          */
  45.         if (_dy < 0) then {
  46.             _incy =-1;
  47.         }else{
  48.             _incy =1;
  49.         };
  50.         /*
  51.          * Аналогично. Если рисуем отрезок снизу вверх -
  52.          * это будет отрицательный сдвиг для y (иначе - положительный).
  53.          */
  54.  
  55.         _dx = abs _dx;
  56.         _dy = abs _dy;
  57.         if (_dx > _dy) then
  58.         //определяем наклон отрезка:
  59.         {
  60.          /*
  61.           * Если dx > dy, то значит отрезок "вытянут" вдоль оси икс, т.е. он скорее длинный, чем высокий.
  62.           * Значит в цикле нужно будет идти по икс (строчка el = dx;), значит "протягивать" прямую по иксу
  63.           * надо в соответствии с тем, слева направо и справа налево она идёт (pdx = incx;), при этом
  64.           * по y сдвиг такой отсутствует.
  65.           */
  66.             _pdx = _incx;   _pdy = 0;
  67.             _es = _dy;  _el = _dx;
  68.         }
  69.         else//случай, когда прямая скорее "высокая", чем длинная, т.е. вытянута по оси y
  70.         {
  71.             _pdx = 0;   _pdy = _incy;
  72.             _es = _dx;  _el = _dy;//тогда в цикле будем двигаться по y
  73.         };
  74.      
  75.         _x = _xstart;
  76.         _y = _ystart;
  77.         _err = _el/2;
  78.         _arr=[];
  79.         for "_i" from 0 to (_el-1) do
  80.         {
  81.             _err =_err - _es;
  82.             if (_err < 0) then
  83.             {
  84.                 _err =_err + _el;
  85.                 _x=_x + _incx;//сдвинуть прямую (сместить вверх или вниз, если цикл проходит по иксам)
  86.                 _y=_y + _incy;//или сместить влево-вправо, если цикл проходит по y
  87.             }
  88.             else
  89.             {
  90.                 _x =_x + _pdx;//продолжить тянуть прямую дальше, т.е. сдвинуть влево или вправо, если
  91.                 _y =_y + _pdy;//цикл идёт по иксу; сдвинуть вверх или вниз, если по y
  92.             };
  93.             _ind =_ind +1;
  94.             if (_ind == _step) then {
  95.             _arr set [count _arr,[_x,_y,_angle]]; _ind = 0;};
  96.         };     
  97.         _arr
  98.     };
  99.     private ["_arr1"];
  100.     _arr1 = [];
  101.     {
  102.         if (_forEachIndex != (count (_this select 0) - 1)) then {
  103.             _arr1 set [count _arr1,([[_x, (_this select 0) select (_forEachIndex +1)],(_this select 1)] call line_algorithm)];         
  104.         };
  105.     } foreach (_this select 0);
  106.     _arr1  
  107. };
  108.  
  109. private ["_pos", "_step", "_dir", "_name", "_color", "_icon", "_type", "_local", "_array", "_index", "_m", "_x"];
  110. _pos = _this select 0;
  111. _step = _this select 1;
  112. if (!isnil {_this select 2}) then {_dir =_this select 2;}else{_dir =0;};
  113. if (!isnil {_this select 3}) then {_name =_this select 3;}else{_name ="markername";};
  114. if (!isnil {_this select 4}) then {_color =_this select 4;}else{_color ="Colorblue";};
  115. if (!isnil {_this select 5}) then {_icon =_this select 5;}else{_icon ="ICON";};
  116. if (!isnil {_this select 6}) then {_type =_this select 6;}else{_type ="group_11";};
  117. if (!isnil {_this select 7}) then {_local =_this select 7;}else{_local =false;};
  118.  
  119. _array = [_pos,_step] call ffa_func_line_algorithm;
  120.  
  121. if (!isnil "_local" && {_local}) then {
  122.     {
  123.         _index = str _forEachIndex;
  124.         {
  125.             _m = createMarkerlocal[_name +_index+ (str _forEachIndex), [ _x select 0,_x select 1]];
  126.             _m setMarkerShapelocal _icon;
  127.             _m setMarkerTypelocal _type;
  128.             _m setMarkerDirlocal ((_x select 2) +_dir);
  129.             _m setmarkercolorlocal _color;
  130.         } foreach _x;
  131.     } foreach _array;
  132. }else{
  133.     {
  134.         _index = str _forEachIndex;
  135.         {
  136.             _m = createMarker[_name +_index+ (str _forEachIndex), [ _x select 0,_x select 1]];
  137.             _m setMarkerShape _icon;
  138.             _m setMarkerType _type;
  139.             _m setMarkerDir ((_x select 2) +_dir);
  140.             _m setmarkercolor _color;
  141.         } foreach _x;
  142.     } foreach _array;
  143. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement