/
PointListPath.java
51 lines (42 loc) · 1.22 KB
/
PointListPath.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package edu.virginia.cs.sde.efficiency;
import java.util.ArrayList;
import java.util.List;
public class PointListPath implements Path {
private final List<Point> points;
public PointListPath(ArrayList<Point> points) {
this.points = points;
}
public PointListPath() {
points = new ArrayList<>();
}
public PointListPath(int initialCapacity) {
points = new ArrayList<>(initialCapacity);
}
@Override
public void add(double xCoordinate, double yCoordinate) {
points.add(new Point(xCoordinate, yCoordinate));
}
@Override
public void add(Point point) {
points.add(point);
}
@Override
public Point get(int index) {
return points.get(index);
}
@Override
public int size() {
return points.size();
}
@Override
public double totalDistance() {
double totalDistance = 0.0;
//Iterate through all but the last point, getting distance to next point
for (int i = 0; i < size()-1; i++) {
Point firstPoint = points.get(i);
Point secondPoint = points.get(i + 1);
totalDistance += firstPoint.distanceTo(secondPoint);
}
return totalDistance;
}
}