perf(astar): calculate distance on the fly instead of on init

This commit is contained in:
Paul Alnet 2024-05-24 14:57:29 +02:00
parent a75f05b9a3
commit 386a227978

View file

@ -8,22 +8,30 @@ import org.insa.graphs.model.RoadInformation;
public class LabelStar extends Label {
private double distanceToDestination;
private int MaximumSpeed;
private Node destination;
public LabelStar(Node node, int MaximumSpeed, Node destination) {
super(node);
this.MaximumSpeed = MaximumSpeed;
this.distanceToDestination = -1;
this.destination = destination;
}
private void initializeDistance() {
// precision was never an answer
if (this.MaximumSpeed < 0) {
distanceToDestination = (double) Point.distance(node.getPoint(), destination.getPoint());
distanceToDestination = (double) Point.distance(node.getPoint(), this.destination.getPoint());
}
else {
distanceToDestination = (double) Point.distance(node.getPoint(), destination.getPoint()) / (1000 * MaximumSpeed);
distanceToDestination = (double) Point.distance(node.getPoint(), this.destination.getPoint()) / (1000 * MaximumSpeed);
}
}
@Override
public double getTotalCost() {
return this.getCost() + distanceToDestination;
if (this.distanceToDestination < 0)
this.initializeDistance();
return this.getCost() + this.distanceToDestination;
}
@Override