Parcourir la source

Allow cardinal directions in Rectangle.Util.getCorner(Rectangle, Direction) + add convenience implementations of size() and contains(int, int) in Zone.Skeleton + document how to proceed to implement a Zone correctly

smelc il y a 7 ans
Parent
commit
7fe090df18

+ 10 - 9
squidlib-util/src/main/java/squidpony/squidgrid/mapping/Rectangle.java

@@ -189,13 +189,12 @@ public interface Rectangle extends Zone {
 
 
 		/**
 		/**
 		 * @param r
 		 * @param r
-         * @param diagonal A diagonal direction.
-		 * @return The coord at the corner identified by {@code diagonal} in
+		 * @param dir
+		 * @return The coord at the corner identified by {@code dir} in
 		 *         {@code r}.
 		 *         {@code r}.
 		 */
 		 */
-		public static Coord getCorner(Rectangle r, Direction diagonal) {
-			assert diagonal.isDiagonal();
-			switch (diagonal) {
+		public static Coord getCorner(Rectangle r, Direction dir) {
+			switch (dir) {
 			case DOWN_LEFT:
 			case DOWN_LEFT:
 				return r.getBottomLeft();
 				return r.getBottomLeft();
 			case DOWN_RIGHT:
 			case DOWN_RIGHT:
@@ -206,15 +205,17 @@ public interface Rectangle extends Zone {
 			case UP_RIGHT:
 			case UP_RIGHT:
 				/* -y because in SquidLib higher y is smaller */
 				/* -y because in SquidLib higher y is smaller */
 				return r.getBottomLeft().translate(r.getWidth() - 1, -(r.getHeight() - 1));
 				return r.getBottomLeft().translate(r.getWidth() - 1, -(r.getHeight() - 1));
+			case NONE:
+				return r.getCenter();
 			case DOWN:
 			case DOWN:
 			case LEFT:
 			case LEFT:
-			case NONE:
 			case RIGHT:
 			case RIGHT:
 			case UP:
 			case UP:
-				throw new IllegalStateException(
-						"Expected a diagonal direction in Rectangle.Utils::getCorner. Received: " + diagonal);
+				final Coord c1 = getCorner(r, dir.clockwise());
+				final Coord c2 = getCorner(r, dir.counterClockwise());
+				return Coord.get((c1.x + c2.x) / 2, (c1.y + c2.y) / 2);
 			}
 			}
-			throw new IllegalStateException("Unmatched direction in Rectangle.Utils::getCorner: " + diagonal);
+			throw new IllegalStateException("Unmatched direction in Rectangle.Utils::getCorner: " + dir);
 		}
 		}
 
 
 		/**
 		/**

+ 26 - 0
squidlib-util/src/main/java/squidpony/squidgrid/zone/Zone.java

@@ -37,6 +37,16 @@ import java.util.List;
  * planned), GreasedRegion is mutable for performance reasons, and may need copies
  * planned), GreasedRegion is mutable for performance reasons, and may need copies
  * to be created if you want to keep around older GreasedRegions.
  * to be created if you want to keep around older GreasedRegions.
  * </p>
  * </p>
+ * 
+ * <p>
+ * The correct method to implement a {@link Zone} efficiently is to first try
+ * implementing the interface directly, looking at each method and thinking
+ * whether you can do something smart for it. Once you've inspected all methods,
+ * then extend {@link Zone.Skeleton} (instead of Object in the first place) so
+ * that it'll fill for you the methods for which you cannot provide a smart
+ * implementation.
+ * </p>
+ * 
  * @author smelC
  * @author smelC
  * @see squidpony.squidmath.CoordPacker
  * @see squidpony.squidmath.CoordPacker
  * @see squidpony.squidmath.GreasedRegion
  * @see squidpony.squidmath.GreasedRegion
@@ -159,6 +169,22 @@ public interface Zone extends Serializable, Iterable<Coord> {
 
 
 		private static final long serialVersionUID = 4436698111716212256L;
 		private static final long serialVersionUID = 4436698111716212256L;
 
 
+		@Override
+		/* Convenience implementation, feel free to override */
+		public int size() {
+			return getAll().size();
+		}
+
+		@Override
+		/* Convenience implementation, feel free to override */
+		public boolean contains(int x, int y) {
+			for (Coord in : this) {
+				if (in.x == x && in.y == y)
+					return true;
+			}
+			return false;
+		}
+
 		@Override
 		@Override
 		/* Convenience implementation, feel free to override */
 		/* Convenience implementation, feel free to override */
 		public boolean contains(Coord c) {
 		public boolean contains(Coord c) {