Merge pull request #64 from Geolykt/patch-1

Added distanceSquared where applicable and changed BlockPosition distance calculation
This commit is contained in:
TheMode 2020-11-05 15:44:35 +01:00 committed by GitHub
commit d31c6e8c5f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
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 * @param blockPosition the block position to check the distance
* @return the distance between 'this' and {@code blockPosition} * @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()) + return Math.abs(getX() - blockPosition.getX()) +
Math.abs(getY() - blockPosition.getY()) + Math.abs(getY() - blockPosition.getY()) +
Math.abs(getZ() - blockPosition.getZ()); 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. * Copies this block position.
* *

View File

@ -72,6 +72,8 @@ public class Position {
/** /**
* Gets the distance between 2 positions. * 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 * @param position the second position
* @return the distance between {@code this} and {@code position} * @return the distance between {@code this} and {@code position}
@ -82,6 +84,18 @@ public class Position {
MathUtils.square(position.getZ() - getZ())); 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 * Gets a unit-vector pointing in the direction that this Location is
* facing. * 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)); 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 * Performs scalar multiplication, multiplying all components with a
* scalar. * scalar.