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

This commit is contained in:
punchready 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;
}
}

View File

@ -138,7 +138,7 @@ class GameLogicTest extends BaseGameLogicTest {
IntVector2 a = new IntVector2(Math.abs(randomIntegers.next() % size), Math.abs(randomIntegers.next() % size));
IntVector2 b = new IntVector2(Math.abs(randomIntegers.next() % size), Math.abs(randomIntegers.next() % size));
ArrayList<IntVector2> result = GameLogic.Bresenham4Connected(a, b);
ArrayList<IntVector2> result = GameLogic.Bresenham8Connected(a, b);
assertEquals(result.get(0), a, "Start point should be point A");
assertEquals(result.get(result.size() - 1), b, "End point should be point B");
@ -148,7 +148,7 @@ class GameLogicTest extends BaseGameLogicTest {
if(pos.equals(a)) {
continue;
}
assertEquals(1, old.distanceManhattan(pos), "Distance between every step should be 1");
assertEquals(1, old.distanceChebyshev(pos), "Distance between every step should be 1");
old = pos;
}
}