OSDN Git Service

added GridPoint2, GridPoint3, for integer positions
authorbadlogic <badlogicgames@gmail.com>
Sat, 13 Jul 2013 11:36:16 +0000 (13:36 +0200)
committerbadlogic <badlogicgames@gmail.com>
Sat, 13 Jul 2013 11:36:16 +0000 (13:36 +0200)
gdx/src/com/badlogic/gdx.gwt.xml
gdx/src/com/badlogic/gdx/math/Bresenham2.java
gdx/src/com/badlogic/gdx/math/GridPoint2.java [new file with mode: 0644]
gdx/src/com/badlogic/gdx/math/GridPoint3.java [new file with mode: 0644]
tests/gdx-tests/src/com/badlogic/gdx/tests/Bresenham2Test.java

index d6bd316..362bc2c 100644 (file)
                <include name="math/Ellipse.java"/>
                <include name="math/FloatCounter.java"/>
                <include name="math/Frustum.java"/>
+               <include name="math/GridPoint2.java"/>
+               <include name="math/GridPoint3.java"/>
                <include name="math/Interpolation.java"/>
                <include name="math/Intersector.java"/>
                <include name="math/MathUtils.java"/>
index 17da5a3..e807054 100644 (file)
@@ -4,71 +4,62 @@ import com.badlogic.gdx.utils.Array;
 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;
@@ -88,17 +79,17 @@ public class Bresenham2 {
                }
                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;
diff --git a/gdx/src/com/badlogic/gdx/math/GridPoint2.java b/gdx/src/com/badlogic/gdx/math/GridPoint2.java
new file mode 100644 (file)
index 0000000..b2521cd
--- /dev/null
@@ -0,0 +1,36 @@
+package com.badlogic.gdx.math;
+
+/**
+ * A point in a 2D grid, with integer x and y coordinates
+ * @author badlogic
+ *
+ */
+public class GridPoint2 {
+       public int x;
+       public int y;
+       
+       public GridPoint2() {
+       }
+       
+       public GridPoint2(int x, int y) {
+               this.x = x;
+               this.y = y;
+       }
+       
+       public GridPoint2(GridPoint2 point) {
+               this.x = point.x;
+               this.y = point.y;
+       }
+       
+       public GridPoint2 set(GridPoint2 point) {
+               this.x = point.x;
+               this.y = point.y;
+               return this;
+       }
+       
+       public GridPoint2 set(int x, int y) {
+               this.x = x;
+               this.y = y;
+               return this;
+       }
+}
diff --git a/gdx/src/com/badlogic/gdx/math/GridPoint3.java b/gdx/src/com/badlogic/gdx/math/GridPoint3.java
new file mode 100644 (file)
index 0000000..b96b46a
--- /dev/null
@@ -0,0 +1,41 @@
+package com.badlogic.gdx.math;
+
+/**
+ * A point in a 3D grid, with integer x and y coordinates
+ * @author badlogic
+ *
+ */
+public class GridPoint3 {
+       public int x;
+       public int y;
+       public int z;
+       
+       public GridPoint3() {
+       }
+       
+       public GridPoint3(int x, int y, int z) {
+               this.x = x;
+               this.y = y;
+               this.z = z;
+       }
+       
+       public GridPoint3(GridPoint3 point) {
+               this.x = point.x;
+               this.y = point.y;
+               this.z = point.z;
+       }
+       
+       public GridPoint3 set(GridPoint3 point) {
+               this.x = point.x;
+               this.y = point.y;
+               this.z = point.z;
+               return this;
+       }
+       
+       public GridPoint3 set(int x, int y, int z) {
+               this.x = x;
+               this.y = y;
+               this.z = z;
+               return this;
+       }
+}
index f18c613..7963fe5 100644 (file)
@@ -8,6 +8,7 @@ import com.badlogic.gdx.graphics.Texture;
 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;
 
@@ -21,10 +22,10 @@ public class Bresenham2Test extends 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();