-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathPath.pde
81 lines (68 loc) · 2.01 KB
/
Path.pde
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
// Polygonal Line Path
// ===================
// Path Class
// ----------
//
// Represents a polygonal line path. Informs registered listeners about
// any changes.
static class Path implements Iterable<PVector>
{
// List of path points
private ArrayList<PVector> points = new ArrayList<PVector>();
// List of registered listeners
private ArrayList<IPathListener> listeners = new ArrayList<IPathListener>();
// Gets the number of points in the path
public int length() {
return points.size();
}
// True if this path has no points
public boolean isEmpty() {
return points.size() == 0;
}
// Gets the `i`th point
public PVector point(int i) {
return points.get(i);
}
// Gets the last point
public PVector lastPoint() {
return points.get(points.size() - 1);
}
// Adds new point at the end of the path
public void addPoint(PVector p) {
points.add(p);
for (IPathListener listener : listeners)
listener.onAddPoint(this, p);
}
// Adds new point at the end of the path
public void addPoint(float x, float y) {
addPoint(new PVector(x, y));
}
// Removes all points from the path
public void clear() {
points.clear();
for (IPathListener listener : listeners)
listener.onClear(this);
}
// Registers new listener. It will from now on be informed about any
// modifications to this path. The listeners will be informed in the same
// order they were registered.
public void addListener(IPathListener listener) {
listeners.add(listener);
}
// Gets the iterator over all points
public Iterator<PVector> iterator() {
return points.iterator();
}
}
// IPathListener Interface
// -----------------------
//
// Path modification callbacks. Client must implement this interface to be
// able to be informed about modifications of a `Path`
interface IPathListener
{
// Invoked when point `p` is added to the end of path `sender`
void onAddPoint(Path sender, PVector p);
// Invoked when `sender` is cleared
void onClear(Path sender);
}