import com.badlogic.gdx.utils.Pool;
/**
- * Returns a list of {@link Vector2} instances at integer coordinates for
- * a line in 2D, using the Bresenham algorithm.<p>
+ * Returns a list of points at integer coordinates for
+ * a line on a 2D grid, using the Bresenham algorithm.<p>
*
- * Instances of this class own the returned array of vectors and the vectors
+ * Instances of this class own the returned array of points and the points
* themselves to avoid garbage collection as much as possible. Calling
- * {@link #line(Vector2, Vector2)} or {@link #line(float, float, float, float)} will
- * result in the reuse of the previously returned array and vectors.
+ * any of the methods will result in the reuse of the previously returned array and vectors, expect
* @author badlogic
*
*/
public class Bresenham2 {
- private final Array<Vector2> points = new Array<Vector2>();
- private final Pool<Vector2> pool = new Pool<Vector2>() {
+ private final Array<GridPoint2> points = new Array<GridPoint2>();
+ private final Pool<GridPoint2> pool = new Pool<GridPoint2>() {
@Override
- protected Vector2 newObject () {
- return new Vector2();
+ protected GridPoint2 newObject () {
+ return new GridPoint2();
}
};
/**
- * Returns a list of {@link Vector2} instances along the given line, at integer coordinates.
- * The input coordinates are cast to integers using a floor operation.
+ * Returns a list of {@link GridPoint2} instances along the given line, at integer coordinates.
* @param start the start of the line
* @param end the end of the line
* @return the list of points on the line at integer coordinates
*/
- public Array<Vector2> line(Vector2 start, Vector2 end) {
+ public Array<GridPoint2> line(GridPoint2 start, GridPoint2 end) {
return line(start.x, start.y, end.y, end.y);
}
/**
- * Returns a list of {@link Vector2} instances along the given line, at integer coordinates.
- * The input coordinates are cast to integers using a floor operation.
+ * Returns a list of {@link GridPoint2} instances along the given line, at integer coordinates.
* @param startX the start x coordinate of the line
* @param startY the start y coordinate of the line
* @param endX the end x coordinate of the line
* @param endY the end y coordinate of the line
* @return the list of points on the line at integer coordinates
*/
- public Array<Vector2> line(float startX, float startY, float endX, float endY) {
+ public Array<GridPoint2> line(int startX, int startY, int endX, int endY) {
pool.freeAll(points);
points.clear();
- line(startX, startY, endX, endY, pool, points);
- return points;
+ return line(startX, startY, endX, endY, pool, points);
}
/**
- * Returns a list of {@link Vector2} instances along the given line, at integer coordinates.
- * The input coordinates are cast to integers using a floor operation.
+ * Returns a list of {@link GridPoint2} instances along the given line, at integer coordinates.
* @param startX the start x coordinate of the line
* @param startY the start y coordinate of the line
* @param endX the end x coordinate of the line
* @param endY the end y coordinate of the line
- * @param pool the pool from which Vector2 instances are fetched
+ * @param pool the pool from which GridPoint2 instances are fetched
* @param output the output array, will be cleared in this method
* @return the list of points on the line at integer coordinates
*/
- public Array<Vector2> line(float startX, float startY, float endX, float endY, Pool<Vector2> pool, Array<Vector2> output) {
- int x1 = (int)startX;
- int y1 = (int)startY;
- int x2 = (int)endX;
- int y2 = (int)endY;
+ public Array<GridPoint2> line(int startX, int startY, int endX, int endY, Pool<GridPoint2> pool, Array<GridPoint2> output) {
- int w = x2 - x1;
- int h = y2 - y1;
+ int w = endX - startX;
+ int h = endY - startY;
int dx1 = 0, dy1 = 0, dx2 = 0, dy2 = 0;
if(w < 0) {
dx1 = -1;
}
int numerator = longest >> 1;
for(int i = 0; i <= longest; i++) {
- Vector2 point = pool.obtain();
- point.set(x1, y1);
+ GridPoint2 point = pool.obtain();
+ point.set(startX, startY);
output.add(point);
numerator += shortest;
if(numerator > longest) {
numerator -= longest;
- x1 += dx1;
- y1 += dy1;
+ startX += dx1;
+ startY += dy1;
} else {
- x1 += dx2;
- y1 += dy2;
+ startX += dx2;
+ startY += dy2;
}
}
return output;
import com.badlogic.gdx.graphics.Pixmap.Format;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.math.Bresenham2;
+import com.badlogic.gdx.math.GridPoint2;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.tests.utils.GdxTest;
pixmap.setColor(Color.WHITE);
Bresenham2 bresenham = new Bresenham2();
- for(Vector2 point: bresenham.line(0, 0, 512, 512)) pixmap.drawPixel((int)point.x, (int)point.y);
- for(Vector2 point: bresenham.line(512, 0, 0, 512)) pixmap.drawPixel((int)point.x, (int)point.y);
- for(Vector2 point: bresenham.line(0, 0, 512, 256)) pixmap.drawPixel((int)point.x, (int)point.y);
- for(Vector2 point: bresenham.line(512, 0, 0, 256)) pixmap.drawPixel((int)point.x, (int)point.y);
+ for(GridPoint2 point: bresenham.line(0, 0, 512, 512)) pixmap.drawPixel(point.x, point.y);
+ for(GridPoint2 point: bresenham.line(512, 0, 0, 512)) pixmap.drawPixel(point.x, point.y);
+ for(GridPoint2 point: bresenham.line(0, 0, 512, 256)) pixmap.drawPixel(point.x, point.y);
+ for(GridPoint2 point: bresenham.line(512, 0, 0, 256)) pixmap.drawPixel(point.x, point.y);
result = new Texture(pixmap);
batch = new SpriteBatch();