fix: use 8-connected instead of 4-connected bresenham

This commit is contained in:
2021-07-28 22:40:48 +02:00
parent c6f56547c6
commit 14d30e14e4
2 changed files with 37 additions and 3 deletions

View File

@ -1174,7 +1174,7 @@ public class GameLogic {
return result;
}
ArrayList<IntVector2> path = GameLogic.Bresenham4Connected(thanos.getPosition(), picked);
ArrayList<IntVector2> path = GameLogic.Bresenham8Connected(thanos.getPosition(), picked);
int mp = thanos.mp.getMax();
@ -1478,4 +1478,38 @@ public class GameLogic {
return result;
}
//https://www.programmersought.com/article/43476640523/
public static ArrayList<IntVector2> Bresenham8Connected(IntVector2 a, IntVector2 b) {
ArrayList<IntVector2> result = new ArrayList<>();
int x0 = a.getX();
int y0 = a.getY();
int x1 = b.getX();
int y1 = b.getY();
int dx = Math.abs(x1 - x0);
int dy = Math.abs(y1 - y0);
int ix = x0 < x1 ? 1 : -1;
int iy = y0 < y1 ? 1 : -1;
int e = dx - dy;
while(x0 != x1 || y0 != y1) {
result.add(new IntVector2(x0, y0));
int e2 = e << 1;
if(e2 > -dy) {
e -= dy;
x0 += ix;
}
if(e2 < dx) {
e += dx;
y0 += iy;
}
}
result.add(b);
return result;
}
}