Skip to content

Instantly share code, notes, and snippets.

@busti
Last active September 27, 2017 00:05
Show Gist options
  • Save busti/7b57037fe3ebde162c5bd7e6a7ff3da2 to your computer and use it in GitHub Desktop.
Save busti/7b57037fe3ebde162c5bd7e6a7ff3da2 to your computer and use it in GitHub Desktop.
Reference implementation of a "collection" where elements are accessible by a positional offset.
public class Path {
private List<Element> elements = new ArrayList<>();
public class Element {
public double lenght;
}
public void addElement(Element element) {
elements.add(element);
}
public Element getElementAtOffset(double offset) {
double pos = 0;
for (Element element: elements) {
pos += element.lenght;
if (pos >= offset)
return element;
}
return null;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment