# Funcionamiento de PVector o constructores... No lo entiendo

**URL:** <https://discourse.processing.org/t/funcionamiento-de-pvector-o-constructores-no-lo-entiendo/14687>\
**Category:** Español\
**Created:** [October 14, 2019, 11:55am UTC](https://discourse.processing.org/t/funcionamiento-de-pvector-o-constructores-no-lo-entiendo/14687 "2019-10-14T11:55:48Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Waboqueox](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/waboqueox/32/5643_2.png) [@Waboqueox](https://discourse.processing.org/u/Waboqueox)\
**Post date:** [October 14, 2019, 11:55am UTC](https://discourse.processing.org/t/funcionamiento-de-pvector-o-constructores-no-lo-entiendo/14687/1 "2019-10-14T11:55:48Z")

</div>

Hola, esto me está amargando 😅, a ver si alguien me puede ayudar :

Algo extraño me pasa al crear un objeto, le paso dos PVector al constructor y un flotante, y al crear el objeto, cuando trato de sumar el float a el valor x de un PVector, se actualiza el valor de otro PVector… Mejor ver el ejemplo:

> **Codigo principal:**
>
> ```auto
> Menu menu;
> PVector a,b;
> float c = 150*4;
> 
> void setup() {
> size(displayWidth, displayHeight, P3D);
> a = new PVector(200,height-175);
> b = new PVector(600,height);
> menu = new Menu (a,b,c);
> 
> }
> 
> void draw() {
> background(200,200,0);
> menu.display();  
> }
> 
> ```

> **La clase:**
>
> ```auto
> 
> class Menu{
> PVector esquina1, esquina2, limiteY;
>   
> Menu(PVector a, PVector b, float c){
> esquina1 = a;
> esquina2 = b;
> println("Me llega: \n a: " + a + "\n b: " +b + "\n c: " + c);
> limiteY = a;
> println("Aqui, las cosas valen: \n esquina1: " + esquina1 + "\n esqina2: "
> +esquina2 + "\n limiteY: " + limiteY);
>                 
>                 
> println("Opero: \n limiteY.x: " + limiteY.x + " + c: "
> + c + " = " + (limiteY.x+c));
> limiteY.x = (limiteY.x+c);
> println("Y por último: \n esquina1: " + esquina1 + "\n esqina2: "
> +esquina2 + "\n limiteY: " + limiteY);
> }
>   
> void display(){
> rectMode(CORNERS);
> rect(esquina1.x,esquina1.y,esquina2.x,esquina2.y);
> }  
> }
> 
> ```

Pero bueno, lo interesante está aquí, la parte que no entiendo:

```auto
Menu(PVector a, PVector b, float c){
    esquina1 = a;
    esquina2 = b;
    limiteY = a;
    limiteY.x = (limiteY.x+c);
  }

```

Y obtengo estos valores:

> Me llega:
> 
> - a: [200.0, 849.0, 0.0]
> - b: [600.0, 1024.0, 0.0]
> - c: 600.0  
> Aqui, las cosas valen:
> - esquina1: [200.0, 849.0, 0.0]
> - esqina2: [600.0, 1024.0, 0.0]
> - limiteY: [200.0, 849.0, 0.0]  
> Opero:
> - limiteY.x: 200.0 + c: 600.0 = 800.0  
> Y por último:
> - esquina1: [800.0, 849.0, 0.0]
> - esqina2: [600.0, 1024.0, 0.0]
> - limiteY: [800.0, 849.0, 0.0]

My pregunta es ¿por qué la variable esquina1 actualiza su valor cuando le sumo un valor a la variable limiteY?

Gracias por leer y ayudar 😃

---

<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:** [October 14, 2019, 5:35pm UTC](https://discourse.processing.org/t/funcionamiento-de-pvector-o-constructores-no-lo-entiendo/14687/2 "2019-10-14T17:35:33Z")

</div>

> [@Waboqueox](#):
>
> `limiteY = a;`

[ProcessingJS.org/reference/PVector\_get\_/](http://ProcessingJS.org/reference/PVector_get_/)  
`limiteY = a.get();`

---

<div class="post-metadata">

**Author:** ![Waboqueox](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/waboqueox/32/5643_2.png) [@Waboqueox](https://discourse.processing.org/u/Waboqueox)\
**Post date:** [October 15, 2019, 7:10am UTC](https://discourse.processing.org/t/funcionamiento-de-pvector-o-constructores-no-lo-entiendo/14687/3 "2019-10-15T07:10:26Z")

</div>

Gracias por contestar @GoToLoop, pero mi duda es principalmente ¿Por qué la variable esquina1 cambia su valor cuando le sumo a OTRA variable ( PVector limiteY ) otra variable ( float c )?

```
 limiteY = a; 

```

Funciona en mi caso sin necesidad de usar " a.get(); "

Y mirando la referencia ya he conseguido sumarle la variable usando:

```
 limiteY.add(0,c);

```

Y por curiosidad, ¿ qué diferencia hay entre [esta referencia (processing.org)](https://processing.org/reference/) y [esta referencia (processingjs.org)](http://processingjs.org/reference/) ?

Graciaaaas 🤗

---

<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:** [October 15, 2019, 8:06am UTC](https://discourse.processing.org/t/funcionamiento-de-pvector-o-constructores-no-lo-entiendo/14687/4 "2019-10-15T08:06:08Z")

</div>

> [@Waboqueox](#):
>
> ¿Por qué la variable esquina1 cambia su valor cuando le sumo a OTRA variable ( PVector limiteY ) otra variable ( float c )?

You mean this statement here, right? `limiteY.x = (limiteY.x+c);`

BtW, the statement above can be shortened like this: `limiteY.x = limiteY.x + c;`

An even shorter version of it: `limiteY.x += c;`

As you can see, you are not mutating the field _limiteY_ itself, but the field _x_ from the PVector object which _limiteY_ points to.

Which btW is the very same PVector object field _esquina1_ also points to:  
`esquina1 = a;` & `limiteY = a;`

Which is originally the PVector object created inside **setup()**, assigned to the sketch’s global field _a_:  
`a = new PVector(200, height - 175);`

And then passed as argument for the Menu’s constructor:  
`menu = new Menu(a, b, c);`

In short fields _a_, _esquina1_ and _limiteY_ are all alias to the same PVector object.

And therefore, mutating its fields _x_, _y_ and _z_ via either of them will reflect on the other 2 fields as well.

If alias are undesirable, we need to assign a clone of the object to a field instead of its original object.

For PVector objects, we have methods **get()** & **copy()**, which return a clone of the object that invokes it:

> **[copy() / Reference](https://processing.org/reference/PVector_copy_.html)**
>
> Copies the components of the vector and returns the result as a PVector.

> [@Waboqueox](#):
>
> Y por curiosidad, ¿ qué diferencia hay entre [esta referencia (processiing.org)](https://processing.org/reference/) y [esta referencia (processingjs.org)](http://processingjs.org/reference/) ?

The former is for the Processing’s default Java Mode, while the latter is for the old Processing’s JS flavor called ProcessingJS (Pjs).
