Advertisement
Ulabael

BresenhamModified

Sep 23rd, 2022 (edited)
261
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.59 KB | None | 0 0
  1. function scrBresenhamModified(x1, y1, x2, y2, grid){
  2. // Источник : http://eugen.dedu.free.fr/projects/bresenham/
  3. var ystep, xstep;
  4. var error;
  5. var errorprev;
  6. var _y = y1, _x = x1;
  7. var ddy, ddx;
  8. var dx = x2 - x1;
  9. var dy = y2 - y1;
  10. var arr = [];
  11. var width = ds_grid_width(grid);
  12. var height = ds_grid_height(grid);
  13.  
  14. array_push(arr, [x1, y1]);
  15. if (dy < 0){
  16. ystep = -1;
  17. dy = -dy;
  18. }
  19. else {
  20. ystep = 1;
  21. }
  22. if (dx < 0){
  23. xstep = -1;
  24. dx = -dx;
  25. }
  26. else {
  27. xstep = 1;
  28. }
  29.  
  30. ddy = 2 * dy; // Для точности - используем двойные значения
  31. ddx = 2 * dx;
  32. if (ddx >= ddy){
  33. errorprev = dx;
  34. error = dx; // Стартуем в центре
  35. for (i=0 ; i < dx ; i++){ // Не используя первую точку, т.к. уже включена
  36. if _y >= 0 and _y < height
  37. and _x >= 0 and _x < width
  38. and ds_grid_get(grid, _x, _y) > 0 {
  39. _x += xstep;
  40. error += ddy;
  41. if (error > ddx){
  42. _y += ystep;
  43. error -= ddx;
  44. //
  45. if (error + errorprev < ddx)
  46. and _x >= 0 and _x < width
  47. and _y-ystep >= 0 and _y - ystep < height
  48. and ds_grid_get(grid, _x, _y-ystep) > 0
  49. { // bottom square also
  50. array_push(arr, [_x, _y-ystep]);
  51. }
  52. else if (error + errorprev > ddx)
  53. and _y >= 0 and _y < height
  54. and _x-xstep >= 0 and _x - xstep < width
  55. and ds_grid_get(grid, _x-xstep, _y) > 0
  56. { // left square also
  57. array_push(arr, [_x-xstep, _y]);
  58. }
  59. else{ // corner: bottom and left squares also
  60. if _y-ystep >= 0 and _y - ystep < height
  61. and _y >= 0 and _y < height
  62. and ds_grid_get(grid, _x-xstep, _y) > 0
  63.  
  64. {
  65. array_push(arr, [_x-xstep, _y]);
  66. }
  67. if _y-ystep >= 0 and _y - ystep < height
  68. and _x >= 0 and _x < width
  69. and ds_grid_get(grid, _x, _y-ystep) > 0
  70. {
  71. array_push(arr, [_x, _y-ystep]);
  72. }
  73. }
  74. }
  75. if _y >= 0 and _y < height
  76. and _x >= 0 and _x < width
  77. and ds_grid_get(grid, _x, _y) > 0
  78. {
  79. array_push(arr, [_x, _y])
  80. }
  81. errorprev = error;
  82. }
  83. }
  84. }
  85. else{ // Тоже самое
  86. errorprev = dy;
  87. error = dy;
  88. for (i=0 ; i < dy ; i++){
  89. if _y >= 0 and _y < height
  90. and _x >= 0 and _x < width
  91. and ds_grid_get(grid, _x, _y) > 0 {
  92. _y += ystep;
  93. error += ddx;
  94. if (error > ddy){
  95. _x += xstep;
  96. error -= ddy;
  97. if (error + errorprev < ddy)
  98. and _x-xstep >= 0 and _x - xstep < width
  99. and _y >= 0 and _y < height
  100. and ds_grid_get(grid, _x-xstep, _y) > 0
  101. {
  102. array_push(arr, [_x-xstep, _y]);
  103. }
  104. else if (error + errorprev > ddy)
  105. and _y-ystep >= 0 and _y - ystep < height
  106. and _x >= 0 and _x < width
  107. and ds_grid_get(grid, _x, _y-ystep) > 0
  108. {
  109. array_push(arr, [_x, _y-ystep]);
  110. }
  111. else{
  112. if _x-xstep >= 0 and _x - xstep < width
  113. and _y >= 0 and _y < height
  114. and ds_grid_get(grid, _x-xstep, _y) > 0
  115. {
  116. array_push(arr, [_x-xstep, _y]);
  117. }
  118. if _x >= 0 and _x < width
  119. and _y-ystep >= 0 and _y - ystep < height
  120. and ds_grid_get(grid, _x, _y-ystep) > 0
  121. {
  122. array_push(arr, [_x, _y-ystep]);
  123. }
  124. }
  125. }
  126. if _y >= 0 and _y < height
  127. and _x >= 0 and _x < width
  128. and ds_grid_get(grid, _x, _y) > 0
  129. {
  130. array_push(arr, [_x, _y])
  131. }
  132. errorprev = error;
  133. }
  134. }
  135. }
  136. return arr
  137. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement