Explorar el Código

Add Zone::getInternalBorder

smelc hace 7 años
padre
commit
d31aa0e314

+ 0 - 1
build.gradle

@@ -42,7 +42,6 @@ def gdxVersion = System.getenv("gdxVersion") ?: "1.9.6" // As in pom.xml
 // In this section you declare the dependencies for your production and test code
 dependencies {
 	compile "com.badlogicgames.gdx:gdx:$gdxVersion"
-	compile project(":RegExodus")
 
 	testCompile "com.badlogicgames.gdx:gdx-backend-lwjgl:$gdxVersion"
 	testCompile "com.badlogicgames.gdx:gdx-platform:$gdxVersion:natives-desktop"

+ 48 - 0
squidlib-util/src/main/java/squidpony/squidgrid/mapping/Rectangle.java

@@ -413,6 +413,54 @@ public interface Rectangle extends Zone {
 			return new Impl(bottomLeft.translate(x, y), width, height);
 		}
 
+		@Override
+		public List<Coord> getInternalBorder() {
+			if (width <= 1 || height <= 1)
+				return getAll();
+			final int expectedSize = width + (height - 1) + (width - 1) + (height - 2);
+			final List<Coord> result = new ArrayList<Coord>(expectedSize);
+			Coord current = Utils.getCorner(this, Direction.DOWN_LEFT);
+			for (int i = 0; i < width; i++) {
+				assert !result.contains(current);
+				result.add(current);
+				current = current.translate(Direction.RIGHT);
+			}
+			current = Utils.getCorner(this, Direction.UP_LEFT);
+			for (int i = 0; i < width; i++) {
+				assert !result.contains(current);
+				result.add(current);
+				current = current.translate(Direction.RIGHT);
+			}
+			current = Utils.getCorner(this, Direction.DOWN_LEFT);
+			/* Stopping at height - 1 to avoid doublons */
+			for (int i = 0; i < height - 1; i++) {
+				if (0 < i) {
+					/*
+					 * To avoid doublons (with the very first value of 'current'
+					 * atop this method.
+					 */
+					assert !result.contains(current);
+					result.add(current);
+				}
+				current = current.translate(Direction.UP);
+			}
+			current = Utils.getCorner(this, Direction.DOWN_RIGHT);
+			/* Stopping at height - 1 to avoid doublons */
+			for (int i = 0; i < height - 1; i++) {
+				if (0 < i) {
+					/*
+					 * To avoid doublons (with the very first value of 'current'
+					 * atop this method.
+					 */
+					assert !result.contains(current);
+					result.add(current);
+				}
+				current = current.translate(Direction.UP);
+			}
+			assert result.size() == expectedSize;
+			return result;
+		}
+
 		@Override
 		public Collection<Coord> getExternalBorder() {
 			final List<Coord> result = new ArrayList<Coord>((width + height) * 2);

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

@@ -1,5 +1,6 @@
 package squidpony.squidgrid.zone;
 
+import squidpony.squidgrid.Direction;
 import squidpony.squidgrid.mapping.DungeonUtility;
 import squidpony.squidmath.Coord;
 
@@ -127,6 +128,12 @@ public interface Zone extends Serializable, Iterable<Coord> {
 	/** @return {@code this} shifted by {@code (x,y)} */
 	Zone translate(int x, int y);
 
+	/**
+	 * @return Cells in {@code this} that are adjacent to a cell not in
+	 *         {@code this}
+	 */
+	List<Coord> getInternalBorder();
+
 	/** @return Cells adjacent to {@code this} that aren't in {@code this} */
 	Collection<Coord> getExternalBorder();
 
@@ -269,6 +276,28 @@ public interface Zone extends Serializable, Iterable<Coord> {
 			return new ListZone(shifted);
 		}
 
+		@Override
+		/* Convenience implementation, feel free to override. */
+		public List<Coord> getInternalBorder() {
+			final int sz = size();
+			if (sz <= 1)
+				return getAll();
+			final List<Coord> result = new ArrayList<Coord>(sz);
+			final List<Coord> all = getAll();
+			assert sz == all.size();
+			nextCell: for (int i = 0; i < sz; i++) {
+				final Coord c = all.get(i);
+				for (Direction out : Direction.OUTWARDS) {
+					final Coord neighbor = c.translate(out);
+					if (!contains(neighbor)) {
+						result.add(c);
+						continue nextCell;
+					}
+				}
+			}
+			return result;
+		}
+
 		@Override
 		/* Convenience implementation, feel free to override. */
 		public Collection<Coord> getExternalBorder() {