Search code examples
processing

Copying PVectors in Processing


PVector a = new PVector(1, 2);
PVector b = a;
PVector c = a.copy();
PVector d = a.get();

Not using .copy() method change one of my drawing.Is there any differences between b, c, d PVectors???


Solution

  • get() and copy() are functionally identical -- each returns a deep copy of the PVector.

    PVector b = a; creates a reference b that points to the same PVector object that a points to.

    Therefore, there are differences between the b, and c & d PVectors.

    Let's use your code and change the value of a after instantiating the other variables to see how they differ:

    PVector a = new PVector(1, 2);
    
    PVector b = a;
    PVector c = a.copy();
    PVector d = a.get();
    
    a.x = 4;
    
    println(a.x);
    println(b.x);
    println(c.x);
    println(d.x);
    

    Output:

    4.0
    4.0
    1.0
    1.0