Create helper class for distance calculation
This commit is contained in:
@@ -0,0 +1,74 @@
|
||||
package com.bartlomiejpluta.base.util.math;
|
||||
|
||||
import org.joml.Vector2dc;
|
||||
import org.joml.Vector2fc;
|
||||
import org.joml.Vector2ic;
|
||||
|
||||
import static java.lang.Math.*;
|
||||
|
||||
public class Distance {
|
||||
|
||||
public static int chebyshev(int ax, int ay, int bx, int by) {
|
||||
return max(abs(ax - bx), abs(ay - by));
|
||||
}
|
||||
|
||||
public static float chebyshev(float ax, float ay, float bx, float by) {
|
||||
return max(abs(ax - bx), abs(ay - by));
|
||||
}
|
||||
|
||||
public static double chebyshev(double ax, double ay, double bx, double by) {
|
||||
return max(abs(ax - bx), abs(ay - by));
|
||||
}
|
||||
|
||||
public static int chebyshev(Vector2ic a, Vector2ic b) {
|
||||
return max(abs(a.x() - b.x()), abs(a.y() - b.y()));
|
||||
}
|
||||
|
||||
public static float chebyshev(Vector2fc a, Vector2fc b) {
|
||||
return max(abs(a.x() - b.x()), abs(a.y() - b.y()));
|
||||
}
|
||||
|
||||
public static double chebyshev(Vector2dc a, Vector2dc b) {
|
||||
return max(abs(a.x() - b.x()), abs(a.y() - b.y()));
|
||||
}
|
||||
|
||||
public static int manhattan(int ax, int ay, int bx, int by) {
|
||||
return abs(ax - bx) + abs(ay - by);
|
||||
}
|
||||
|
||||
public static float manhattan(float ax, float ay, float bx, float by) {
|
||||
return abs(ax - bx) + abs(ay - by);
|
||||
}
|
||||
|
||||
public static double manhattan(double ax, double ay, double bx, double by) {
|
||||
return abs(ax - bx) + abs(ay - by);
|
||||
}
|
||||
|
||||
public static int manhattan(Vector2ic a, Vector2ic b) {
|
||||
return abs(a.x() - b.x()) + abs(a.y() - b.y());
|
||||
}
|
||||
|
||||
public static float manhattan(Vector2fc a, Vector2fc b) {
|
||||
return abs(a.x() - b.x()) + abs(a.y() - b.y());
|
||||
}
|
||||
|
||||
public static double manhattan(Vector2dc a, Vector2dc b) {
|
||||
return abs(a.x() - b.x()) + abs(a.y() - b.y());
|
||||
}
|
||||
|
||||
public static double euclidean(float ax, float ay, float bx, float by) {
|
||||
return sqrt((ax - bx) * (ax - bx) + (ay - by) * (ay - by));
|
||||
}
|
||||
|
||||
public static double euclidean(double ax, double ay, double bx, double by) {
|
||||
return sqrt((ax - bx) * (ax - bx) + (ay - by) * (ay - by));
|
||||
}
|
||||
|
||||
public static double euclidean(Vector2fc a, Vector2fc b) {
|
||||
return sqrt((a.x() - b.x()) * (a.x() - b.x()) + (a.y() - b.y()) * (a.y() - b.y()));
|
||||
}
|
||||
|
||||
public static double euclidean(Vector2dc a, Vector2dc b) {
|
||||
return sqrt((a.x() - b.x()) * (a.x() - b.x()) + (a.y() - b.y()) * (a.y() - b.y()));
|
||||
}
|
||||
}
|
||||
@@ -6,6 +6,7 @@ import com.bartlomiejpluta.base.engine.core.gl.object.material.Material;
|
||||
import com.bartlomiejpluta.base.engine.core.gl.object.mesh.Mesh;
|
||||
import com.bartlomiejpluta.base.engine.world.animation.model.AnimatedSprite;
|
||||
import com.bartlomiejpluta.base.internal.logic.Updatable;
|
||||
import com.bartlomiejpluta.base.util.math.Distance;
|
||||
import com.bartlomiejpluta.base.util.math.MathUtil;
|
||||
import lombok.EqualsAndHashCode;
|
||||
import lombok.Getter;
|
||||
@@ -164,11 +165,11 @@ public abstract class MovableSprite extends AnimatedSprite implements Movable, U
|
||||
|
||||
@Override
|
||||
public int chebyshevDistance(Vector2ic coordinates) {
|
||||
return max(abs(this.coordinates.x - coordinates.x()), abs(this.coordinates.y - coordinates.y()));
|
||||
return Distance.chebyshev(this.coordinates, coordinates);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int manhattanDistance(Vector2ic coordinates) {
|
||||
return abs(this.coordinates.x - coordinates.x()) + abs(this.coordinates.y - coordinates.y());
|
||||
return Distance.manhattan(this.coordinates, coordinates);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user