Added distanceSquared where applicable

Warning: Also breaks current API-Implementation (API-specification remains the same) in a way that BlockPosition#getDistance(BlockPosition) no longer returns the manhattan distance, but the distance according to pythagoras.
This commit is contained in:
Geolykt 2020-11-04 20:31:29 +01:00
parent 2c0f0a8f9e
commit 3be5605126
3 changed files with 50 additions and 2 deletions

View File

@ -174,17 +174,41 @@ public class BlockPosition {
}
/**
* Gets the distance to another block position.
* Gets the manhattan distance to another block position.
*
* @param blockPosition the block position to check the distance
* @return the distance between 'this' and {@code blockPosition}
*/
public int getDistance(@NotNull BlockPosition blockPosition) {
public int getManhattanDistance(@NotNull BlockPosition blockPosition) {
return Math.abs(getX() - blockPosition.getX()) +
Math.abs(getY() - blockPosition.getY()) +
Math.abs(getZ() - blockPosition.getZ());
}
/**
* Gets the distance to another block position.
* In cases where performance matters, {@link #getDistanceSquared(BlockPosition)} should be used
* as it does not perform the expensive Math.sqrt method.
*
* @param blockPosition the block position to check the distance
* @return the distance between 'this' and {@code blockPosition}
*/
public double getDistance(@NotNull BlockPosition blockPosition) {
return Math.sqrt(getDistanceSquared(blockPosition));
}
/**
* Gets the square distance to another block position.
*
* @param blockPosition the block position to check the distance
* @return the distance between 'this' and {@code blockPosition}
*/
public int getDistanceSquared(@NotNull BlockPosition blockPosition) {
return MathUtils.square(getX() - blockPosition.getX()) +
MathUtils.square(getY() - blockPosition.getY()) +
MathUtils.square(getZ() - blockPosition.getZ());
}
/**
* Copies this block position.
*

View File

@ -72,6 +72,8 @@ public class Position {
/**
* Gets the distance between 2 positions.
* In cases where performance matters, {@link #getDistanceSquared(Position)} should be used
* as it does not perform the expensive Math.sqrt method.
*
* @param position the second position
* @return the distance between {@code this} and {@code position}
@ -82,6 +84,18 @@ public class Position {
MathUtils.square(position.getZ() - getZ()));
}
/**
* Gets the square distance to another position.
*
* @param position the second position
* @return the squared distance between {@code this} and {@code position}
*/
public float getDistanceSquared(Position position) {
return MathUtils.square(getX() - position.getX()) +
MathUtils.square(getY() - position.getY()) +
MathUtils.square(getZ() - position.getZ());
}
/**
* Gets a unit-vector pointing in the direction that this Location is
* facing.

View File

@ -118,6 +118,16 @@ public class Vector {
return Math.sqrt(MathUtils.square(x - o.x) + MathUtils.square(y - o.y) + MathUtils.square(z - o.z));
}
/**
* Gets the squared distance between this vector and another.
*
* @param o The other vector
* @return the squared distance
*/
public double distanceSquared(Vector o) {
return MathUtils.square(x - o.x) + MathUtils.square(y - o.y) + MathUtils.square(z - o.z);
}
/**
* Performs scalar multiplication, multiplying all components with a
* scalar.