Skip to content

Instantly share code, notes, and snippets.

@GoToLoop
Last active August 10, 2020 03:43
Show Gist options
  • Save GoToLoop/571eaa4797963ffbacdd706542c30637 to your computer and use it in GitHub Desktop.
Save GoToLoop/571eaa4797963ffbacdd706542c30637 to your computer and use it in GitHub Desktop.
Trailing Deque
height: 600
scrolling: no
border: no
license: cc-by-4.0
/**
* ArrayQueue (v1.1.1)
* GoToLoop (2020/Apr/20)
* https://Discourse.Processing.org/t/linkedlist-vs-arraylist/19946/14
*/
//package java.util; // Uncomment this line if this is a ".java" file
import java.util.Collection;
import java.util.Queue;
import java.util.List;
import java.util.ArrayList;
import java.util.Iterator;
import java.io.Serializable;
static // Comment this out if this is a ".java" file
public class ArrayQueue<E> implements Queue<E>, Cloneable, Serializable {
//static final long serialVersionUID = 8_779_387_929_858_209_947L;
static final long serialVersionUID = 779_387_929;
protected final List<E> list;
public ArrayQueue() {
list = new ArrayList<E>();
}
public ArrayQueue(final int initialCapacity) { // Ignored by Pjs
list = new ArrayList<E>(initialCapacity);
}
public ArrayQueue(final Collection<? extends E> c) {
list = new ArrayList<E>(c);
}
public void clear() {
list.clear();
}
public int size() {
return list.size();
}
public boolean isEmpty() {
return list.isEmpty();
}
public boolean offer(final E e) {
return add(e);
}
public boolean add(final E e) { // No return (void) on Pjs
return list.add(e);
}
public E peek() {
return isEmpty()? null : element();
}
public E element() {
return list.get(0);
}
public E poll() {
return isEmpty()? null : remove();
}
public E remove() {
return list.remove(0);
}
public boolean remove(final Object o) {
return list.remove(o);
}
public boolean removeAll(final Collection<?> c) {
return list.removeAll(c);
}
public boolean addAll(final Collection<? extends E> c) { // void on Pjs
return list.addAll(c);
}
public boolean retainAll(final Collection<?> c) { // Undefined on Pjs
return list.retainAll(c);
}
public boolean containsAll(final Collection<?> c) { // Undefined on Pjs
return list.containsAll(c);
}
public boolean contains(final Object o) {
return list.contains(o);
}
public Object[] toArray() {
return list.toArray();
}
public <T> T[] toArray(final T[] a) { // Parameter ignored by Pjs
return list.toArray(a);
}
public Iterator<E> iterator() {
return list.iterator();
}
public ArrayQueue<E> clone() {
return new ArrayQueue<E>(list);
}
public String toString() { // Undefined on Pjs
return list.toString();
}
}
<script defer src=https://Unpkg.com/processing-js></script>
<canvas data-processing-sources="Trailing_Deque.pde PointXY.pde ArrayQueue.pde"></canvas>
final class PointXY {
static final color FG = #0000FF;
static final short BOLD = 3;
int x, y;
PointXY(final int px, final int py) {
setXY(px, py);
}
PointXY setXY(final int px, final int py) {
x = px;
y = py;
return this;
}
PointXY lineXY(final PointXY other) {
line(other.x, other.y, x, y);
return this;
}
String toString() {
return "[ " + x + ", " + y + " ]";
}
}
/**
* Trailing Deque (v4.0.0)
* by Quarks (2013/Feb)
* for Rialobran
* mod GoToLoop
*
* https://Forum.Processing.org/two/discussion/3864/
* how-to-draw-multiple-user-defined-lines#Item_3
*
* https://www.OpenProcessing.org/sketch/878488
* https://Bl.ocks.org/GoToLoop/571eaa4797963ffbacdd706542c30637
*/
import java.util.Queue;
import java.util.ArrayDeque; // not needed anymore
static final boolean JAVA = 1/2 != 1/2.;
static final byte MAX_TRAIL_POINTS = 5;
final Queue<PointXY> trails = new ArrayQueue<PointXY>(MAX_TRAIL_POINTS);
//final Queue<PointXY> trails = new ArrayDeque<PointXY>(MAX_TRAIL_POINTS);
static final color BG = 0350;
static final short FPS = 30;
static final String GFX = JAVA2D; // use JAVA2D, FX2D or P2D
void setup() {
size(800, 600, GFX);
frameRate(FPS);
noLoop();
stroke(PointXY.FG);
strokeWeight(PointXY.BOLD);
}
void draw() {
background(BG);
PointXY tt = trails.peek();
for (final PointXY t : trails) tt = t.lineXY(tt);
if (JAVA) println(trails);
else { // Pjs doesn't implement ArrayList::toString()
for (final PointXY xy : trails) print(xy);
println("\n");
}
}
void mousePressed() {
trails.add( trails.size() == MAX_TRAIL_POINTS
? trails.remove().setXY(mouseX, mouseY)
: new PointXY(mouseX, mouseY) );
redraw();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment