Guest User

Untitled

a guest
May 27th, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.12 KB | None | 0 0
  1. // Change
  2. dy = Math.abs(y1 - y0)
  3. // to
  4. dy = -Math.abs(y1 - y0)
  5.  
  6.  
  7. // Change
  8. err = dx - dy;
  9. // to
  10. err = dx + dy
  11.  
  12. // Change
  13. if (e2 > -dy) {
  14. err -= dy;
  15. x0 += sx;
  16. }
  17. // to
  18. if (e2 > dy) { // removes the - operation
  19. err += dy;
  20. x0 += sx;
  21. }
  22.  
  23. // just before the loop
  24. var column = bitmap[x0];
  25.  
  26. // then in the loop you only need to index the row
  27. if (column[y0] === 1) {
  28. return 1;
  29. }
  30. if (e2 > dy) {
  31. err += dy;
  32. x0 += sx;
  33. column = bitmap[x0]; // and update the column only when x0 changes.
  34. }
  35.  
  36. // using bitmap as a flat array with width as the pixel width
  37. // Assuming coords are ints
  38. function collision(x0, y0, x1, y1, width) {
  39. const dx = Math.abs(x1 - x0);
  40. const dy = -Math.abs(y1 - y0);
  41. const sx = x0 < x1 ? 1 : -1;
  42. const sy = y0 < y1 ? width : -width;
  43. const endIndex = x1 + y1 * width;
  44. var e2, err = dx + dy;
  45. var index = x0 + y0 * width;
  46.  
  47. // loop through line drawing
  48. while (index !== endIndex) {
  49. if (bitmap[index] === 1) { return 1 }
  50. e2 = err << 1;
  51. if (e2 > dy) {
  52. err += dy;
  53. index += sx;
  54. }
  55. if (e2 < dx) {
  56. err += dx;
  57. index += sy;
  58. }
  59. }
  60. return 0;
  61. }
Add Comment
Please, Sign In to add comment