-
Notifications
You must be signed in to change notification settings - Fork 13
/
PathIterator.java
executable file
·56 lines (50 loc) · 2.06 KB
/
PathIterator.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
52
53
54
55
56
/**
* An Iterator class that returns points that are steps between points on a Path.
*
* @author Will Franzenr
* @version 1.0.0
*/
public class PathIterator
{
private Path path; // The path to follow
private Point target; // The point currently working towards
private int targetIndex; // The index of the target
private Point last; // The last point returned
// Constructor, accepts a path to follow
public PathIterator(Path path) {
this.path = path; // Set the path
this.targetIndex = 0; // Set the target index
this.target = path.getPoints()[targetIndex]; // Set the target to the first point
this.last = path.getPoints()[targetIndex]; // Set the last point to be the first point
}
// Get the next point on the path
public Point next() {
if(last.equals(target)) { // If the last one returned was the target
targetIndex++; // Increment the targetIndex
if(targetIndex == path.getPoints().length) // Check if it was the final point
return null;
target = path.getPoints()[targetIndex]; // Set the new target
}
int x = last.getX(); // Get X from the last point
int y = last.getY(); // Get Y from the last point
// Add or subtract one to bring the point a step closer
if(target.getX() > last.getX())
x++;
if(target.getX() < last.getX())
x--;
if(target.getY() > last.getY())
y++;
if(target.getY() < last.getY())
y--;
return (last = new Point(x, y)); // Return a point with the new cooridnates
}
// Return a duplicate of itself
public PathIterator clone() {
PathIterator n = new PathIterator(path); // Create a new PathIterator
n.path = path; // Set path
n.target = target; // Set target
n.targetIndex = targetIndex; // Set targetIndex
n.last = last; // Set last
return n; // return it
}
}