@Override public final boolean contains(ShortPoint2D position) { return contains(position.x, position.y); }
@Override public CoordinateStream stream() { return stream(centerX, centerY, radius); }
public CoordinateStream streamBorder() { return streamBorder(centerX, centerY, radius); }
public MapCircleIterator(MapCircle circle) { this.circle = circle; radius = circle.getRadius(); currentY = -(int) (radius / MapCircle.Y_SCALE); currentLineHalfWidth = circle.getHalfLineWidth(currentY); currentX = -currentLineHalfWidth; centerX = circle.getCenterX(); centerY = circle.getCenterY(); }
public static CoordinateStream streamBorder(int centerX, int centerY, float radius) { return stream(centerX, centerY, radius).filter(getBorderPredicate(centerX, centerY, radius)); }
/** * Occupies the given area for the given playerId if it is not already occupied by towers of an enemy. * * @param playerId * The id of the occupying player. * @param influencingArea * The area affected by the tower. * @param groundArea * The ground area of the tower. */ public void addTowerAndOccupyArea(byte playerId, MapCircle influencingArea, FreeMapArea groundArea) { IMapArea filteredArea = new MapShapeFilter(influencingArea, width, height); PartitionOccupyingTower tower = new PartitionOccupyingTower(playerId, influencingArea.getCenter(), groundArea, filteredArea, influencingArea.getBorders(), (int) influencingArea.getRadius()); occupyAreaOfTower(tower); }
@Test public void testBorderStream() { for (int i = 1; i < 40; i++) { MapCircle circle = new MapCircle(new ShortPoint2D(100, 100), i); MapCircle oneSmallerCircle = new MapCircle(new ShortPoint2D(100, 100), i - 1); circle.streamBorder().forEach((x, y) -> { assertTrue(circle.contains(x, y)); assertTrue(!oneSmallerCircle.contains(x, y)); }); int maxCount = circle.stream().count() - oneSmallerCircle.stream().count(); int actualCount = circle.streamBorder().count(); assertTrue(actualCount <= maxCount); } } }
@Test public void testTraversing1() { final MapCircle c1 = new MapCircle(100, 100, 50); final MapCircle c2 = new MapCircle(120, 100, 20); final MapCircle c3 = new MapCircle(120, 100, 10); final IContainingProvider containingProvider = (x, y) -> c1.contains(x, y) && !c2.contains(x, y) || c3.contains(x, y); assertTrue(c1.contains(x, y) && !c2.contains(x, y)); // checks if the position is in the area area.add(new ShortPoint2D(x, y)); int idx = x + y * WIDTH; }; boolean result = AreaTraversingAlgorithm.traverseArea(containingProvider, visitor, c1.iterator().next(), WIDTH, HEIGHT); assertTrue(result); if (!c2.contains(curr)) { assertTrue(mapArea.contains(curr));
@Test public void testStream() { for (int i = 0; i < 40; i++) { MapCircle circle = new MapCircle(new ShortPoint2D(100, 100), i); MapCircleIterator iterator = circle.iterator(); circle.stream().forEach((x, y) -> { assertTrue(iterator.hasNext()); ShortPoint2D expected = iterator.next(); assertEquals(expected, new ShortPoint2D(x, y)); }); assertFalse(iterator.hasNext()); } }
public CachedViewCircle(int radius) { radius -= FogOfWar.PADDING / 2; MapCircle circle = new MapCircle(0, 0, radius + FogOfWar.PADDING); size = countElements(circle); x = new short[size]; y = new short[size]; sight = new byte[size]; MapCircleIterator iter = circle.iterator(); final float squaredViewDistance = radius * radius; int i = 0; while (iter.hasNext()) { int y = iter.nextY(); int x = iter.nextX(); this.x[i] = (short) x; this.y[i] = (short) y; double squaredDistance = MapCircle.getSquaredDistance(x, y); byte newSight; if (squaredDistance < squaredViewDistance) { newSight = CommonConstants.FOG_OF_WAR_VISIBLE; } else { newSight = (byte) (CommonConstants.FOG_OF_WAR_VISIBLE - (Math.sqrt(squaredDistance) - radius) / FogOfWar.PADDING * CommonConstants.FOG_OF_WAR_VISIBLE); } sight[i] = newSight; i++; } }
private void drawBuildingCircle(int x, int y, BuildingMapDataObject buildingObject) { byte player = buildingObject.getPlayerId(); EBuildingType type = buildingObject.getType(); if (type == EBuildingType.TOWER || type == EBuildingType.BIG_TOWER || type == EBuildingType.CASTLE) { MapCircle circle = new MapCircle(x, y, CommonConstants.TOWER_RADIUS); drawCircle(player, circle); } }
@Test public void testGetBorders() { for (int i = 1; i < 40; i++) { MapCircle circle = new MapCircle(new ShortPoint2D(100, 100), i); SRectangle borders = circle.getBorders(); for (ShortPoint2D curr : circle) { if (!borders.contains(curr)) { fail("position: " + curr + " is not in the border " + borders + " radius: " + i); } } } }
private int countElements(MapCircle circle) { int counter = 0; MapCircleIterator iter = circle.iterator(); while (iter.hasNext()) { // count the elements in the circle to create array iter.nextX(); counter++; } return counter; }
/** * Gets the distance of map coordinates to the center. * * @param x * The x coordinate. * @param y * The y coordinate * @return The distance to the center of this circle, so that the tiles around the center all have distance 1. */ public final float squaredDistanceToCenter(int x, int y) { return getSquaredDistance(x - centerX, y - centerY); }
public static final float getDistance(int x1, int y1, int x2, int y2) { float squared = getDistanceSquared(x1, y1, x2, y2); return (float) Math.sqrt(squared); }
/** * Test method for {@link jsettlers.common.map.shapes.MapCircle#contains(jsettlers.common.position.ShortPoint2D)} . */ @Test public void testContainsShortPoint2D() { short cx = 0; short cy = 0; MapCircle circle = new MapCircle(cx, cy, 1); ShortPoint2D[] truePoints = new ShortPoint2D[] { new ShortPoint2D(cx - 1, cy - 1), new ShortPoint2D(cx - 1, cy), new ShortPoint2D(cx, cy - 1), new ShortPoint2D(cx, cy), new ShortPoint2D(cx + 1, cy), new ShortPoint2D(cx, cy + 1), new ShortPoint2D(cx + 1, cy + 1) }; for (ShortPoint2D point : truePoints) { assertTrue("Point should be inside", circle.contains(point)); } ShortPoint2D[] falsePoints = new ShortPoint2D[] { new ShortPoint2D(cx - 2, cy - 1), new ShortPoint2D(cx, cy + 3), new ShortPoint2D(cx - 1, cy + 1), }; for (ShortPoint2D point : falsePoints) { assertFalse("Point should not be inside: " + point, circle.contains(point)); } }
private void occupyAreaIfNeeded() { if (!occupiedArea) { MapCircle occupying = new MapCircle(super.pos, CommonConstants.TOWER_RADIUS); super.grid.occupyAreaByTower(super.getPlayer(), occupying, getGroundArea()); occupiedArea = true; } }
public static final float getDistanceSquared(int x1, int y1, int x2, int y2) { int dx = x1 - x2; int dy = y1 - y2; return getSquaredDistance(dx, dy); }
/** * Returns the {@link PartitionOccupyingTower} objects with areas that intersect the area specified by the given position and radius. * * @param center * Position of the tower defining the center of it's area. * @param radius * Radius of it's area. * @param towerPredicate * Predicate specifying the towers that should be checked * @return */ public List<Tuple<Integer, PartitionOccupyingTower>> getTowersInRange(ShortPoint2D center, int radius, Predicate<PartitionOccupyingTower> towerPredicate) { return StreamSupport.stream(this).filter(towerPredicate).map(tower -> { int sqDist = (int) MapCircle.getDistanceSquared(center.x, center.y, tower.position.x, tower.position.y); return new Tuple<>(sqDist, tower); }).filter(towerWithDistance -> { int maxDist = radius + towerWithDistance.e2.radius; return towerWithDistance.e1 <= (maxDist * maxDist); }).collect(Collectors.toList()); } }