From 3be560512627b1c7f5a11d4d754d1bb3c673e45c Mon Sep 17 00:00:00 2001 From: Geolykt Date: Wed, 4 Nov 2020 20:31:29 +0100 Subject: [PATCH] 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. --- .../minestom/server/utils/BlockPosition.java | 28 +++++++++++++++++-- .../net/minestom/server/utils/Position.java | 14 ++++++++++ .../net/minestom/server/utils/Vector.java | 10 +++++++ 3 files changed, 50 insertions(+), 2 deletions(-) diff --git a/src/main/java/net/minestom/server/utils/BlockPosition.java b/src/main/java/net/minestom/server/utils/BlockPosition.java index b597012d8..3e819dadb 100644 --- a/src/main/java/net/minestom/server/utils/BlockPosition.java +++ b/src/main/java/net/minestom/server/utils/BlockPosition.java @@ -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. * diff --git a/src/main/java/net/minestom/server/utils/Position.java b/src/main/java/net/minestom/server/utils/Position.java index c587384a3..66b1d9978 100644 --- a/src/main/java/net/minestom/server/utils/Position.java +++ b/src/main/java/net/minestom/server/utils/Position.java @@ -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. diff --git a/src/main/java/net/minestom/server/utils/Vector.java b/src/main/java/net/minestom/server/utils/Vector.java index e4542a379..f88382a8f 100644 --- a/src/main/java/net/minestom/server/utils/Vector.java +++ b/src/main/java/net/minestom/server/utils/Vector.java @@ -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.