We are about to switch to a new forum software. Until then we have removed the registration on this forum.
class Particle {
float x, y;
float r;
Particle() {
x = random(width);
y = random(height);
r= random (4,30);
}
Particle(float tempX, float tempY, float tempR) {
x=tempX;
y=tempY;
r=tempR;
}
boolean overlaps(Particle other) {
float d = dist(x,y,other.x,other.y);
if (d< r+ other.r){
return true;
} else {
return false;
}
}
void display() {
stroke(255);
strokeWeight(4);
noFill();
ellipse(x,y,r*2,r*2);
}
In the code above, I don't get the logic behind
boolean overlaps(Particle other) {
float d= dist(x,y,other.x,other.y);
if(d<r+other.r){
return true;
}else{
return false;
}
}
For me it seems like, newly defined function 'overlaps' would take two inputs one of which is Particle and the other is other. And it will execute with the variable float d which represents distance between x&y coordinates of Particle and x&y coordinates of other. if the distance value is smaller than the sum of radius of Particle and other, it will yield the 'true' value.
However,
specifically, for "overlaps(Particle other)", I think there must be a comma between Particle and other as 'Particle' also requires commas among each entity going into its round bracket. like Particle (float tempX, float tempY,float tempZ).
I think I totally don't understand the part from boolean. can you explain what the part below is meaning?
boolean overlaps(Particle other) {
float d= dist(x,y,other.x,other.y);
I really don't get this part...
Answers
When you define a class, you are creating a new type of object. You use various types of objects all the time without realizing it: int is a type of object that represents a number; String is a type of object that represents a bunch of char objects in some order.
By defining the class Particle, you have created a new type of object: Particle
Now this class has a function in it, called overlaps(). This function is going to return a boolean, and it also takes one object as input: some other Particle object. You can tell all this from the definition of the function:
boolean
is the returned type.overlaps()
is the function name And the input to this function is aParticle
calledother
There is no need for a comma between Particle and other, because what that is saying is that there is one input. The type of the input is Particle. And the name of the input is other.
@TfGuy44 Thank you so much. Can I ask you one more question? if "Particle other" is just a name of another Particle,
As a bonus, method Particle::overlaps(Particle) can be greatly simplified like this: :D
@TfGuy44, Java's datatype
int
isn't an object/reference/pointer but simply primitive. L-)There are exactly 8 primitive datatypes in Java.
All of the other types can be called objects, references, pointers or even memory addresses: ~O)
http://Docs.Oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html