Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add BlockAreac#distanceSquared #4621

Merged
merged 8 commits into from
May 26, 2022
Original file line number Diff line number Diff line change
Expand Up @@ -150,4 +150,30 @@ public void testIntersectsArea(BlockArea other, boolean intersecting) {
BlockArea area = new BlockArea(0, 0, 3, 3);
assertEquals(intersecting, area.intersectsBlockArea(other));
}

static Stream<Arguments> testDistanceSquaredArgs() {
return Stream.of(
Arguments.of(new BlockArea(0, 0, 3, 3), -3, -3, 8),
Arguments.of(new BlockArea(0, 0, 3, 3), 1, 2, 0),
Arguments.of(new BlockArea(0, 0, 3, 3), -2, -2, 2),
Arguments.of(new BlockArea(0, 0, 3, 3), 0, 5, 1),

Arguments.of(new BlockArea(-1, -1, 4, 4), 5, 5, 0),
Arguments.of(new BlockArea(-1, -1, 4, 4), 6, 6, 2),
Arguments.of(new BlockArea(-1, -1, 4, 4), 1, 2, 0),
Arguments.of(new BlockArea(-1, -1, 4, 4), 0, 6, 1)
);
}

@ParameterizedTest
@MethodSource("testDistanceSquaredArgs")
public void testDistanceSquared(BlockArea area, int x, int y, int val) {
assertEquals(val, area.distanceSquared(x, y));
}

@Test
public void testDistanceSquaredVector() {
BlockArea area = new BlockArea(0, 0, 3, 3);
assertEquals(8, area.distanceSquared(new Vector2i(-3, -3)));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ public BlockArea(BlockAreac other) {

// -- reset ------------------------------------------------------------------------------------------------------//

@SuppressWarnings("checkstyle:HiddenField")
public BlockArea set(int minX, int minY, int maxX, int maxY) {
Preconditions.checkArgument(minX <= maxX || (minX == INVALID.minX() && maxX == INVALID.maxX()));
Preconditions.checkArgument(minY <= maxY || (minY == INVALID.minY() && maxY == INVALID.maxY()));
Expand Down Expand Up @@ -359,4 +360,24 @@ public int hashCode() {
public String toString() {
return "BlockArea[(" + this.minX + ", " + this.minY + ")...(" + this.maxX + ", " + this.maxY + ")]";
}

@Override
public long distanceSquared(Vector2ic p) {
return distanceSquared(p.x(), p.y());
}

@Override
public long distanceSquared(int px, int py) {
//center = min + width / 2 = min + (max - min) / 2
// 2 * center = 2 * min + max - min = min + max
int cx2 = minX() + maxX();
int cy2 = minY() + maxY();

// dx = p - center - width / 2
// 2 * dx = 2 * px - 2 * center - width
// dx = (2 * px - 2 * center - width) / 2
long dx = Math.max(Math.abs(2 * px - cx2) - getSizeX(), 0) / 2;
long dy = Math.max(Math.abs(2 * py - cy2) - getSizeY(), 0) / 2;
return dx * dx + dy * dy;
shadowasphodel2919 marked this conversation as resolved.
Show resolved Hide resolved
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -518,6 +518,25 @@ default boolean intersectsBlockArea(BlockAreac other) {
&& this.minX() <= other.maxX() && this.minY() <= other.maxY();
}

// -- distance --------------------------------------------------------------------------------------------------//

/**
* The squared distance to a point {@code p}.
*
* @param p the coordinates of the point
* @return the squared distance between this area and the point {@code p}
*/
long distanceSquared(Vector2ic p);

/**
* The squared distance to a point {@code p} given by coordinates (x, y).
*
* @param px the x coordinate of the point
* @param py the y coordinate of the point
* @return the squared distance between this area and the point {@code p}
*/
long distanceSquared(int px, int py);

// ---------------------------------------------------------------------------------------------------------------//
boolean equals(Object obj);

Expand Down