My suggestion would be to simplify things
So the PVector stores already x and y, so you don’t need a new class
Find my example below. Everytime you click on the canvas, the mouse coordinates are added to the record. Once you have clicked 10 times (in this case), all previous records will be printed
ArrayList<PVector> recorded = new ArrayList();
void setup() {
size(100, 100);
}
void draw() {
}
void mousePressed() {
recorded.add(new PVector(mouseX, mouseY)); //Add new coordinates to ArrayList
int idxLastRecord = recorded.size()-1; //Get the last added position in the array
println("Add to record: " + recorded.get(idxLastRecord).x + ", " + recorded.get(idxLastRecord).y); //Print the coordinates
if (recorded.size() == 10) { //If the size is ten, print all
println("I am the one! PRINTING THEM ALL! ");
for (int i = 0; i < recorded.size(); i++) {
println("X = " + recorded.get(i).x + ", Y = " + recorded.get(i).y);
}
println("--------------------------");
}
}
ArrayList<PVector> recorded = new ArrayList();
void setup() {
size(1110, 400);
background(0);
}
void draw() {
background(0);
for (PVector pv : recorded) // short form of for-loop
ellipse(pv.x, pv.y, 7, 7);
}
void mousePressed() {
//Add new coordinates to ArrayList
recorded.add(new PVector(mouseX, mouseY));
//Get the last added position in the array
PVector pvTest = recorded.get(recorded.size()-1);
println("Add to record: "
+pvToString(pvTest)); //Print the coordinates
if (recorded.size() == 10) { //If the size is ten, print all
println("I am the one! PRINTING THEM ALL! ");
for (PVector pv : recorded)
println(pvToString(pv));
println("--------------------------");
}
}
// function to get String from PVector
String pvToString (PVector pv) {
return
"X = " + pv.x
+ ", Y = " + pv.y;
}
Thanks @Chrisir, I’ve got another question related to this. To animate an object (a red circle in this particular example) along the points drawn; I’m using the code below in draw() but the red circle ends up at the end of the path (at the last vertex in the array).
I thought for loop in Processing was iterating each time a new frame is created in draw() function, relative to the frame rate. But this one happens all at once. I searched the forum and the answers suggested using lerp(), which wouldn’t work in my case because the time between two point is set to 1 frame- irrespective of the distance between them. Should I use millis or pushMatrix, popMatrix? Keep in mind this is a 120fps power-intensive sketch, so I’m really confused.
Here’s the loop:
if (readyToAnimate && mouseX > 1000) {
for (int i = 0; i < recorded.size() - 1; i++) {
background(48);
fill(255, 0,0);
ellipse((recorded.get(i).p1.x), recorded.get(i).p1.y, 30, 30);
}
}
draw() updates the screen only one time at its end.
Therefore you cannot see any animation done with a for loop.
Get rid of the for loop and then use the fact that draw is looping in itself.
So make i a global variable and increase it with i++;
When i is >= size() of the list say i = 0; to restart
The lerp() command
Later you can use lerp() too. It would help to calculate and show the minor points between each or two points in the chain. So the animation gets more smooth. You would need i2 to do this and reset i2 every time you reach a point (which you received by i).
Normally we use a fixed number of steps (amt parameter in the reference for lerp()) between two points. To have always the same speed of the animation, we could calculate the number of steps based on the distance of the two current points.