We are about to switch to a new forum software. Until then we have removed the registration on this forum.
PVector[] dots = new PVector[3];
void setup() {
dots[0] = new PVector(100, 10);
dots[1] = new PVector(10, 10);
dots[2] = new PVector(10, 50);
size(200, 200);
fill(255);
}
void draw() {
background(0);
ellipse(dots[0].x, dots[0].y, 10, 10);
ellipse(dots[1].x, dots[1].y, 10, 10);
ellipse(dots[2].x, dots[2].y, 10, 10);
printArray(dots);
println(getAcute(dots[0], dots[1], dots[2]));
}
float getAcute(PVector a, PVector b, PVector c) {
return degrees(PVector.angleBetween(a.sub(b), c.sub(b))); //Problematic line.
}
When using this basic function of finding angle using 3 points, the values of 'dots[0]' and 'dots[2]' are reduced by 'dots[1]' every time the getAcute function is called. I don't understand why it does this because my understanding is that the function creates 3 temporary variables, "a, b, c", and is restricted to that function. How do I make it so the dots[] values aren't changed at all? I could make new variables and set them to equal dots[] every time, but I feel like there is a simpler solution and my code should be working.
Answers
https://Processing.org/reference/PVector_sub_.html
So, is this a call by reference or a call by value?
float getAcute(PVector a, PVector b, PVector c) {
@Chrisir You are working with references. If the reference is changed inside the function, say a is assigned a new PVector using new, then a new memory address is created and assigned to a. However, the original object used to call by a is not affected. Also notice that the new created object assigned to a inside the function will not be available after the function ends. It will be lost. Does this answer your question?
Kf