add java examples
This commit is contained in:
parent
624c92e934
commit
d345f67212
19 changed files with 1208 additions and 0 deletions
32
java/strategy/TransitOnEdge.java
Normal file
32
java/strategy/TransitOnEdge.java
Normal file
|
|
@ -0,0 +1,32 @@
|
|||
package patterns.strategy;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import patterns.Point;
|
||||
/**
|
||||
* Find the transit of minimal cost among the given edges.
|
||||
|
||||
Edges are given as the considered point and the sequence of neighbors points.
|
||||
|
||||
* @author pouyllau
|
||||
*/
|
||||
|
||||
|
||||
public class TransitOnEdge extends Transit {
|
||||
|
||||
public double transit(Point p, List<Point> neighbors, Map<Point, Double> costs) {
|
||||
double mincost = Double.POSITIVE_INFINITY;
|
||||
|
||||
for (Point n : neighbors) {
|
||||
if (hasCost(n, costs)) {
|
||||
double c = costs.get(n) + Point.distance(p, n);
|
||||
if (c < mincost) {
|
||||
mincost = c;
|
||||
}
|
||||
}
|
||||
}
|
||||
assert (mincost != Double.POSITIVE_INFINITY);
|
||||
return mincost;
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue