feat: add 4-connected Bresenham pathfinding algorithm
This commit is contained in:
@ -13,6 +13,7 @@ import uulm.teamname.marvelous.gamelibrary.requests.RequestType;
|
||||
import java.awt.*;
|
||||
import java.awt.geom.Line2D;
|
||||
import java.awt.geom.Point2D;
|
||||
import java.sql.ResultSetMetaData;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.HashSet;
|
||||
@ -911,7 +912,7 @@ class GameLogic {
|
||||
}
|
||||
}
|
||||
}
|
||||
//
|
||||
//TODO: implement thanos ai
|
||||
|
||||
}
|
||||
|
||||
@ -1074,4 +1075,39 @@ class GameLogic {
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
//https://stackoverflow.com/a/5187612
|
||||
public static ArrayList<IntVector2> Bresenham4Connected(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 = 0;
|
||||
|
||||
for(int i = 0; i < dx + dy; i++) {
|
||||
result.add(new IntVector2(x0, y0));
|
||||
int e1 = e + dy;
|
||||
int e2 = e - dx;
|
||||
if(Math.abs(e1) < Math.abs(e2)) {
|
||||
x0 += ix;
|
||||
e = e1;
|
||||
}else {
|
||||
y0 += iy;
|
||||
e = e2;
|
||||
}
|
||||
}
|
||||
|
||||
result.add(b);
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user