Advertisement
Ulabael

scrBresenham

Sep 23rd, 2022
225
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.91 KB | None | 0 0
  1. function scrBresenham(x1, y1, x2, y2, grid) {
  2. var w = x2-x1;
  3. var h = y2-y1;
  4. var arr = [];
  5. var dx1 = 0; var dx2 = 0; var dy1 = 0; var dy2 = 0;
  6. if (w<0) {dx1 = -1} else if (w > 0) {dx1 = 1};
  7. if (w<0) {dx2 = -1} else if (w > 0) {dx2 = 1};
  8. if (h<0) {dy1 = -1} else if (h > 0) {dy1 = 1};
  9. var longest = abs(w);
  10. var shortest = abs(h);
  11. if (!(longest > shortest)) {
  12. longest = abs(h);
  13. shortest = abs(w);
  14. if (h < 0) {dy2 = -1} else if (h > 0) {dy2 = 1}
  15. dx2 = 0;
  16. }
  17. var numerator = longest >> 1;
  18. for (var i = 0; i <= longest; i++) {
  19. if (x1 > 0 and y1 > 0 and x1 < ds_grid_width(grid) and y1 < ds_grid_height(grid)) {
  20. if (ds_grid_get(grid, x1, y1) > 0) {
  21. array_push(arr, [x1, y1]);
  22. numerator += shortest;
  23. if (!(numerator<longest)){
  24. numerator -= longest;
  25. x1 += dx1;
  26. y1 += dy1;
  27. } else {
  28. x1 += dx2;
  29. y1 += dy2;
  30. }
  31. }
  32. }
  33. }
  34. return arr;
  35. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement