We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › Question about code in "learning processing" book.
Page Index Toggle Pages: 1
Question about code in "learning processing" book. (Read 1090 times)
Question about code in "learning processing" book.
Aug 9th, 2009, 8:46am
 
In Shiffmans book on page 187 near the top, he has a boolean intersect function with the argument of (Drop d)   The Drop is refering to the class, but what is the d refering to? I can not see anywhere where it is initalized.
Re: Question about code in "learning processing" book.
Reply #1 - Aug 9th, 2009, 9:15am
 
I guess d refers to the instance of the class? Could you give the line of code - for those who didn't get the book... yet Wink
Re: Question about code in "learning processing" book.
Reply #2 - Aug 9th, 2009, 10:40am
 
Sorry I'm on my iPhone today otherwise I would give more info.

Here is the code:

boolean intersect (drop d) {
float distance = dist(x,y,d.x,d.y);
if (distance < r + d.r) {
return true;
} else {
return false;
}
}
Re: Question about code in "learning processing" book.
Reply #3 - Aug 9th, 2009, 1:04pm
 
This is a custom function called intersect(). When you define a function, you'll see two meaningful pieces of information surrounding the function name. The item on the left of the name says "boolean." This tells us that this function will return a boolean variable. You can see that happen at the end when you return false. The item inside the parentheses is called the "argument." This function takes one argument, so when you call intersect() in your code, you need to pass in a variable of the type specified. Here, that variable type is of class "drop." The variable "d" is going to represent the drop object that is passed through once it is in the function. So you'll notice now that in the next line, Shiffman calls d's x and y members (d.x & d.y).

So in this case, "d" is actually a reference to the drop object that gets passed in. So anything you do with "d" inside of the function effects the actual object that's getting passed in when the function is called.

So to actually use this function, the code might looking something like this:
Code:

void setup(){
    size(100, 100);
    drop c = new drop();
    intersect(c);
}


In the above code we declare a drop object called "c", then we pass that object into the intersect function. Once in the function, the object will get assigned to the variable "d", and we can manipulate that however we like. Understand?
Re: Question about code in "learning processing" book.
Reply #4 - Aug 9th, 2009, 3:21pm
 
Thanks for the reply. I'm getting there. But even in the "final code" for this example 10-10 on page 187 he never defines Drop d he just uses it as an argument.

I can't even get example 10-3 to run. I suspect because Ball b is never initialized.
Re: Question about code in "learning processing" book.
Reply #5 - Aug 9th, 2009, 3:59pm
 
I haven't got the book, but the source code is downloadable from here.  

I'm not sure you've properly followed NoChinDeluxe's explanation.

Code:
boolean intersect(Drop d) {  
 //
}


is a method definition that accepts one parameter, in this case a Drop.  The 'd' is simply a label used to represent *any* drop passed to the method. A Drop 'd' isn't created in the main code, nor does it need to be.  However a Drop does need to be included in any call to the method and in fact is in this line at the bottom of example_10_10:

Code:
if (catcher.intersect(drops[i])) { 



'drops' is an array of Drop objects created at the top of example_10_10 ("Drop[] drops;       // An array of drop objects").
The above condition is in a for loop, so 'drops[i]' passes each Drop from the array as a parameter to the 'intersect' method.
Re: Question about code in "learning processing" book.
Reply #6 - Aug 10th, 2009, 6:37am
 
How about a simpler example? Let's define a function called addTwoNumbers();

Code:

//here's the function definition:
int addTwoNumbers(int a, int b){
    int c = a + b;
    return c;
}

//Here is how we would use it in code:
int x = 1;
int y = 2;
int sum = addTwoNumbers(x, y); //sum now equals 3


Notice in the above code that we never actually define ints a or b (the function arguments). They simply get assigned the value of ints x and y when we call the function. So in essence, they are just copies of the two integers we passed into the function. It will be the same with your Drop d example. There is no need to define or initialize Drop d, because it's simply an exact copy of some other object that is being passed in to the function. So if it's an exact copy of an object that's already been initialized, why initialize it again? No need, we just assign it the value of the other initialized object being passed in.
Re: Question about code in "learning processing" book.
Reply #7 - Aug 10th, 2009, 9:33am
 
Thanks everyone !!  It finally clicked..   Shocked
Page Index Toggle Pages: 1