# How to store last 20 mouse positions in an array?

**URL:** <https://discourse.processing.org/t/how-to-store-last-20-mouse-positions-in-an-array/5626>\
**Category:** Coding Questions\
**Created:** [November 17, 2018, 1:59pm UTC](https://discourse.processing.org/t/how-to-store-last-20-mouse-positions-in-an-array/5626 "2018-11-17T13:59:15Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![fmS3lect](https://avatars.discourse-cdn.com/v4/letter/f/0ea827/32.png) [@fmS3lect](https://discourse.processing.org/u/fmS3lect)\
**Post date:** [November 17, 2018, 1:59pm UTC](https://discourse.processing.org/t/how-to-store-last-20-mouse-positions-in-an-array/5626/1 "2018-11-17T13:59:15Z")

</div>

Hey all together,

i am Sebastian, i am very new to programming and i think i need some help 😃

i wrote this code snippet to store the last 20 mouse positions but it only stores the same 20times… i cannot figure out why…

thank your for your help in advance

```auto
int num = 20;
float[] lastMouseX = new float[num];
float[] lastMouseY = new float[num];

void setup() {
size(500, 500);
background(255);
}

void draw() {
  for(int i = 0; i < num; i = i+1 ) {
    lastMouseX[i] = mouseX;
    lastMouseY[i] = mouseY;
  }
  
  
  printArray(lastMouseX);

}

```

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [November 17, 2018, 2:04pm UTC](https://discourse.processing.org/t/how-to-store-last-20-mouse-positions-in-an-array/5626/2 "2018-11-17T14:04:01Z")

</div>

`http://Studio.ProcessingTogether.com/sp/pad/export/ro.90vdKMfkiO$zf`

---

<div class="post-metadata">

**Author:** ![fmS3lect](https://avatars.discourse-cdn.com/v4/letter/f/0ea827/32.png) [@fmS3lect](https://discourse.processing.org/u/fmS3lect)\
**Post date:** [November 17, 2018, 2:14pm UTC](https://discourse.processing.org/t/how-to-store-last-20-mouse-positions-in-an-array/5626/3 "2018-11-17T14:14:46Z")

</div>

thank your for your fast help!

but i dont quite understand whats happening at the code you send…  
i am not that good at programming…

---

<div class="post-metadata">

**Author:** ![josephh](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/josephh/32/210_2.png) [@josephh](https://discourse.processing.org/u/josephh)\
**Post date:** [November 17, 2018, 2:30pm UTC](https://discourse.processing.org/t/how-to-store-last-20-mouse-positions-in-an-array/5626/4 "2018-11-17T14:30:52Z")

</div>

Basically it uses the class `PVector` ([https://processing.org/reference/PVector.html](https://processing.org/reference/PVector.html)) which in this case, stores the `mouseX` and `mouseY` position.

The idea behind this, is that you have an array of `PVector` objects and at the beginning, you keep filling the array until the length becomes 20 and then you continue adding one element at a time and you remove the first element so the length stays at 20.

An example with `ArrayLists` ([https://processing.org/reference/ArrayList.html](https://processing.org/reference/ArrayList.html)) :

```auto
ArrayList<PVector> list_positions;
int length_limit = 20;

void setup() {
  size(400, 400);
  stroke(0);
  strokeWeight(4);

  list_positions = new ArrayList<PVector>();
}

void draw() {
  background(255);
  //Adding a new position
  list_positions.add(new PVector(mouseX, mouseY));
  if (list_positions.size() > length_limit) {
    //Removing the first element
    list_positions.remove(0);
  }

  //Drawing a line
  beginShape();
  for (PVector p : list_positions) {
    vertex(p.x, p.y);
  }
  endShape();
}

```

---

<div class="post-metadata">

**Author:** ![fmS3lect](https://avatars.discourse-cdn.com/v4/letter/f/0ea827/32.png) [@fmS3lect](https://discourse.processing.org/u/fmS3lect)\
**Post date:** [November 17, 2018, 3:08pm UTC](https://discourse.processing.org/t/how-to-store-last-20-mouse-positions-in-an-array/5626/5 "2018-11-17T15:08:06Z")

</div>

Thank you very much!  
that is exactly what i want, i did not know about PVector but this is very usefull!

---

<div class="post-metadata">

**Author:** ![TfGuy44](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/tfguy44/32/41_2.png) [@TfGuy44](https://discourse.processing.org/u/TfGuy44)\
**Post date:** [November 17, 2018, 10:48pm UTC](https://discourse.processing.org/t/how-to-store-last-20-mouse-positions-in-an-array/5626/6 "2018-11-17T22:48:02Z")

</div>

A PVector is just a convenient way to store a group of two or three floats in a single variable. Plus it has some other handy functions related to vector math.
