Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Change
- dy = Math.abs(y1 - y0)
- // to
- dy = -Math.abs(y1 - y0)
- // Change
- err = dx - dy;
- // to
- err = dx + dy
- // Change
- if (e2 > -dy) {
- err -= dy;
- x0 += sx;
- }
- // to
- if (e2 > dy) { // removes the - operation
- err += dy;
- x0 += sx;
- }
- // just before the loop
- var column = bitmap[x0];
- // then in the loop you only need to index the row
- if (column[y0] === 1) {
- return 1;
- }
- if (e2 > dy) {
- err += dy;
- x0 += sx;
- column = bitmap[x0]; // and update the column only when x0 changes.
- }
- // using bitmap as a flat array with width as the pixel width
- // Assuming coords are ints
- function collision(x0, y0, x1, y1, width) {
- const dx = Math.abs(x1 - x0);
- const dy = -Math.abs(y1 - y0);
- const sx = x0 < x1 ? 1 : -1;
- const sy = y0 < y1 ? width : -width;
- const endIndex = x1 + y1 * width;
- var e2, err = dx + dy;
- var index = x0 + y0 * width;
- // loop through line drawing
- while (index !== endIndex) {
- if (bitmap[index] === 1) { return 1 }
- e2 = err << 1;
- if (e2 > dy) {
- err += dy;
- index += sx;
- }
- if (e2 < dx) {
- err += dx;
- index += sy;
- }
- }
- return 0;
- }
Add Comment
Please, Sign In to add comment