Reduce object creation during rendering of path preview

Skip sqrt when comparing distances
This commit is contained in:
johni0702
2015-06-29 18:12:38 +02:00
parent 540588125b
commit 3122c0a71e
2 changed files with 6 additions and 7 deletions

View File

@@ -82,12 +82,11 @@ public class Position {
public void setRoll(float roll) { this.roll = roll; } public void setRoll(float roll) { this.roll = roll; }
public double distanceTo(double x, double y, double z) { public double distanceSquared(double x, double y, double z) {
return distanceTo(new Position(x, y, z, 0, 0)); double dx = this.x - x;
} double dy = this.y - y;
double dz = this.z - z;
public double distanceTo(Position p2) { return dx * dx + dy * dy + dz * dz;
return Math.sqrt(Math.pow((p2.getX() - getX()), 2) + Math.pow((p2.getY() - getY()), 2) + Math.pow((p2.getZ() - getZ()), 2));
} }
@Override @Override

View File

@@ -130,7 +130,7 @@ public class PathPreviewRenderer {
@Override @Override
public int compare(PositionKeyframe o1, PositionKeyframe o2) { public int compare(PositionKeyframe o1, PositionKeyframe o2) {
return -(new Double(o1.getPosition().distanceTo(playerX, playerY, playerZ)).compareTo(o2.getPosition().distanceTo(playerX, playerY, playerZ))); return -(new Double(o1.getPosition().distanceSquared(playerX, playerY, playerZ)).compareTo(o2.getPosition().distanceSquared(playerX, playerY, playerZ)));
} }
@Override @Override