We are about to switch to a new forum software. Until then we have removed the registration on this forum.
I have a little exposure to Java and Processing. My simple-minded, self-teaching project generates a curve by linking line segments. The end points are PVectors v1 and v2. The final end point of one segment (v2) becomes the initial end point (v1) of the next. So in setup(), I define (x,y) values for v1 then, in draw(), I define a different (x,y) pair for v2 and call line() using v1 and v2. The obvious next step is to copy v2 into v1 in preparation for creating a new v2 in the next draw() loop.
But there's the problem. In the first pass through draw(), the simple statement "v1 = v2;" doesn't copy values but seems to copy the instance address from v2 into v1. I see all the methods for PVector in the Processing Reference but no ".copy()". I don't know if my ignorance is mostly Java or mostly Processing! If this were C++ I would expect a copy() operator to be defined for PVector instances.
Thanks for input to my question.
Answers
A solution could be v1.x = v2.x and v1.y = v2.y
See: http://www.processing.org/reference/PVector.html
You can use .get()
Although it may be better to just re-use the PVector as it's the same coordinate right? If you use a begin-endShape with (curve)vertex, you don't need to call them twice anyway.
(see above)
The best way would be simply to use the object references.
This may not be exactly what you are looking for but it draws a line from an array of PVectors and leaves the array elements unchanged which is useful.
Best way to transfer the values of a PVector to another 1 is via its set() method:
http://processing.org/reference/PVector_set_.html
Java, like regular C & JS, doesn't have custom operator overloading. Only few built-in 1s like
+, &, |, ^
. 8-XThe assignment operator
=
only transfers the result from right to the variable from left:http://processing.org/reference/assign.html
Its behavior can't be changed/overloaded to something else like C++ & Python can do! [-X
Some examples which rely on set() in order to avoid needless PVector instantiations:
Thanks all for the helpful responses. The little program did run as expected by separately setting the .x and .y components, but I was looking for a familiar method to assign PVectors. Thanks esp. to GoToLoop. I've glanced at those examples and one will lead to a more satisfying method.