mirror of
https://github.com/Minestom/Minestom.git
synced 2024-12-29 04:28:21 +01:00
Merge pull request #64 from Geolykt/patch-1
Added distanceSquared where applicable and changed BlockPosition distance calculation
This commit is contained in:
commit
d31c6e8c5f
@ -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.
|
||||
*
|
||||
|
@ -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.
|
||||
|
@ -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.
|
||||
|
Loading…
Reference in New Issue
Block a user