If you think the Android project acceptableLosses listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
Java Source Code
package acceptableLosses.map;
//fromwww.java2s.comimport com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.math.Vector3;
import com.stewsters.util.math.Point2i;
import java.util.LinkedList;
publicclass MapTools {
publicstatic Vector2 getDirectionVector(int x1, int y1, int x2, int y2) {
Vector2 cell1 = world2window(x1, y1);
Vector2 cell2 = world2window(x2, y2);
returnnew Vector2(cell2.x - cell1.x, cell2.y - cell1.y);
}
publicstaticint manhattanDistance(int x, int y, int x1, int y1) {
return Math.abs(x - x1) + Math.abs(y - y1);
}
publicstatic LinkedList<Point2i> getNeighbors(int x, int y, int range) {
LinkedList<Point2i> results = new LinkedList<Point2i>();
for (int x1 = x - range; x1 < x + range; x1++) {
for (int y1 = y - range; y1 < y + range; y1++) {
if (x1 == x || y1 == y)
continue;
if (manhattanDistance(x, y, x1, y1) <= range) {
results.add(new Point2i(x1, y1));
}
}
}
return results;
}
publicstatic LinkedList<Point2i> getNeighbors(int x, int y) {
return getNeighbors(x, y, 1);
}
publicstatic Point2i window2world(int x, int y, OrthographicCamera camera) {
Vector3 pos = new Vector3(x, y, 0);
camera.unproject(pos);
returnnew Point2i((int) ((pos.x) + 0.5), (int) ((pos.y) + 0.5));
}
publicstatic Vector2 world2window(float x, float y) {
returnnew Vector2(x, y);
}
publicstatic Point2i libgdx2world(float x, float y) {
returnnew Point2i((int) (x), (int) (y));
}
}