Hi. Do I have to place Public Voids into void draw if I want to use it repeatedly ??
To answer your question: No, it’s not allowed to defined a function inside another method in Processing.
Explanation: public void
is just the header of a function you are defining. Usually followed by the name of the function and it’s parameters. In the curly brackets the content / body of your function is defined.
public
means, that it is visible to anyone else. Usually you do not need that in your own sketches.
void
means that this function is not returning anything. Return means, that a method calculates something (for example a math method f(x) = x^2
). This function is taking in a number and returning one. In Processing it would look like this:
int f(x) {
return pow(x, 2);
}
Now to call this method repeatedly inside your sketch, you can define in the global scope (outside of draw) and call it inside draw. Calling a method is done by writing its name and giving it it’s parameters:
void draw() {
int result = f(2);
println(result);
}
And just a side note, because it leads to a lot of confusion:
The method hello()
is not returning anything, even there is something written to the console. This method produces output, but no return values.
void hello() {
println("test");
}
Please don’t say voids, say functions.
void is just the return type.
So, yeah, you define the function and then call it in draw().
see programming and functions · Kango/Processing-snippets Wiki · GitHub
void setup() {
size(600, 500);
noFill();
stroke(255, 200, 120);
strokeWeight(3);
}
void draw() {
background(0);
rightAngle(260, 180);
}
void rightAngle(float startX, float startY) {
line (startX, startY, startX+100, startY);
line (startX+100, startY, startX+100, startY+100);
}
Thanks to both of you guys I appreciate it.
and of course you can (and must sometimes) call the same function multiple times from draw()
void setup() {
size(600, 500);
noFill();
stroke(255, 200, 120);
strokeWeight(3);
}
void draw() {
background(0);
rightAngle(260, 180);
rightAngle(260-30, 180-30);
rightAngle(260-30-30, 180-30-30);
}
void rightAngle(float startX, float startY) {
line (startX, startY, startX+100, startY);
line (startX+100, startY, startX+100, startY+100);
}