like here
(The small check if it’s new doesn’t work)
Explanation
Instead of PVector (the in-built type to store a point with x,y) that we used previously we define a new type “Line” that stores 2 PVectors (line from point 1 to point 2). To define a type, we made a class “Line”.
To store a line in our list we just say
listFootprints.add(new Line(new PVector(x1, y1), new PVector(x2, y2)));
As you can see we make a new line by passing 2 PVectors in it (that we make on the fly) and add the line to the list. (you have to read this from inside to outside: from x1,y1 to PVector, 2 PVectors needed to make a line, add the line to list)
- The nice thing of using a class is that we can place a function “display()” inside a class to work with one line.
Sketch
ArrayList<Line> listFootprints = new ArrayList();
void setup() {
size (700, 700); // set the size of the window
smooth();
noCursor(); // hides mouse cursor
}
void draw() {
background(255); // Draw a white background
// draw FootPrints
drawFootPrints();
// show size of the list (for testing)
text(listFootprints.size(), 17, 17);
//Draw Zoog
drawZoog(mouseX, mouseY);
}
// ---------------------------------------------------------------------------------
void drawZoog(float x, float y) {
// draw Zoog
// store formatting
push();
//Set ellipses and rect to CENTER mode
ellipseMode(CENTER);
rectMode(CENTER);
//Draw Body
stroke(0);
fill(175);
rect(x, y, 20, 100);
//Draw Head
stroke(0);
fill(255);
ellipse(x, y-30, 60, 60);
//Draw Zoog's eyes
fill(0);
ellipse(x-18, y-30, 16, 32);
ellipse(x+18, y-30, 16, 32);
// Draw legs
stroke(0);
line(x+10, y+50, x+20, y+60);
line(x-10, y+50, x-20, y+60);
// store footprints (from legs position)
if (mouseX>0 && mouseY>0) { // if mouse is there
storeFootprints(x+10, y+50, x+20, y+60);
storeFootprints(x-10, y+50, x-20, y+60);
}
// restore formatting
pop();
}
void storeFootprints( float x1, float y1,
float x2, float y2) {
// store the foot print
// The first 3 times we store the foot print
if (listFootprints.size()==0 || listFootprints.size()==1 || listFootprints.size()==2) {
listFootprints.add(new Line(new PVector(x1, y1), new PVector(x2, y2)));
return; // leave
}//if
// After the first time we check if the last position was the same,
// we store the foot print only when it's new:
// last position:
if ( listFootprints.get(listFootprints.size()-2).pv1.x != x1 || listFootprints.get(listFootprints.size()-2).pv1.y != y1 )
listFootprints.add(new Line(new PVector(x1, y1),
new PVector(x2, y2)));
} //func
void drawFootPrints() {
for (Line l1 : listFootprints) {
noFill();
l1.display();
}//for
}//func
// ============================================================================
class Line {
PVector pv1;
PVector pv2;
// constr
Line(PVector pv1_, PVector pv2_) {
pv1=pv1_.copy();
pv2=pv2_.copy();
}// constr
void display() {
line(pv1.x, pv1.y,
pv2.x, pv2.y);
}//method
//
}//class
//