We are about to switch to a new forum software. Until then we have removed the registration on this forum.
http://www.learningprocessing.com/examples/chapter-9/example-9-8/
int[] xpos = new int[50];
int[] ypos = new int[50];
void setup() {
size(200,200);
smooth();
for (int i = 0; i < xpos.length; i ++ ) {
xpos[i] = 0;
ypos[i] = 0;
}
}
void draw() {
background(255);
// Shift array values
for (int i = 0; i < xpos.length-1; i ++ ) {
xpos[i] = xpos[i+1];
ypos[i] = ypos[i+1];
}
// New location
xpos[xpos.length-1] = mouseX;
ypos[ypos.length-1] = mouseY;
// Draw everything
for (int i = 0; i < xpos.length; i ++ ) {
// Draw an ellipse for each element in the arrays.
// Color and size are tied to the loop's counter: i.
noStroke();
fill(255-i*5);
ellipse(xpos[i],ypos[i],i,i);
}
}
I am a complete beginner in Processing & Programming in general.
Okay, here is what I understood from this code, as I'm not here to copy & paste, and expect someone to explain everything from top to bottom without making an effort to try to understand it myself:
The declaration and creation of xpos & ypos arrays, of length (size) 50 (i.e running from index 0 -> 49)
Size of display window is set to 200 by 200 (w x h)
All the elements, from xpos[0] -> xpos[49] & ypos[0] -> ypos[49] are initialised to zero. So xpos[0] = 0; xpos[30] = 0 etc
I may have understood the next part incorrectly, but basically all the elements, from index 0 -> 48 (xpos[0] -> xpos[48]), their values are shifted 1 place. So xpos[0] = xpos[1], xpos[29] = xpos[30] etc
At xpos[49] & ypos[49], mouseX and mouseY values are placed there, respectively. The rest of the elements value is 0.
This is another part I'm unsure of too. Isn't the first ellipse, drawn at ellipse(xpos[0],ypos[0],0,0)? i.e ellipse(0,0,0,0)? With a fill color of (255)? If it is, as the counter increases, until xpos[49], isn't xpos[49] = mouseX? If so, and when I click play, is the ellipse at ellipse(0,0,49,49)? As xpos[49] = mouseX, and since we haven't done anything yet, mouseX = 0? And mouseY = 0.
If that's all correctly understand, could someone explain the logic of this program, which I'm struggling to comprehend.
Basically, at the start, all the elements in the xpos & ypos arrays are set to 0, with the exception of the last element [49], set to mouseX/mouseY. Since mouseX & mouseY both equal to 0 at the start, after the 3rd for loop finishes running, we are left with ellipse(xpos[49] = 0, ypos[49] = 0, 49,49) and fill (255-495) = 10. So a black square at (0,0), of width & height of 49.
Then draw() runs again. Overall I'm struggling to understand this program.
Answers
Lines #11 to #14 are redundant! Since the
int
primitive data-type has initial value = 0!Basically, all values are left-shifted 1 index slot @ 60 FPS. Even though they start @ 0, they fill it up fast w/ mouse coordinates!
Some similar online examples for you to peruse: >:D<
studio.processingtogether.com/sp/pad/export/ro.9GTDpA6dp4tH1/latest
studio.processingtogether.com/sp/pad/export/ro.90vdKMfkiO$zf/latest
It is unnecessary to initialise the elements to zero, because by default, int primitive data type have a value of 0, without being initialized? So we don't need to initialise int data because it's already 0 by default without us doing anything?
Each single frame, which is 1 run through draw(), the values shift 1 place to the left?
Links not working, but thanks.
Either he/she simply didn't know that or wanted to highlight the importance of placing an initial value,
even though it's just a redundant
0
!That is only true for structures like array slots & for field variables.
Local variables still need an explicit initial value before their usage!
Also notice that non-primitive types start out as
null
! So pay special attention to them! :PThat's the idea! :>
I believe it was temporarily offline! It's online right now though!
Either way, since they're not big, I'm gonna post those 2 codes here: (*)
Thanks, will try out those examples, although look quite beyond me at this stage!
Any site for a beginner, where I can practice with Arrays & processing examples/exercises?
Currently I am working on http://www.learningprocessing.com/examples/ and exercises.
Any others?
What is the best way to learn Processing? Maybe the method I'm using isn't the best! Currently I'm reading through the Learning Processing book (http://www.learningprocessing.com/buy-the-book/), chapter by chapter, and then doing the examples and exercises on the site.
Any suggestions would be appreciated!
What about YouTube again?
Preferably something that I can interact with. Like http://www.w3schools.com/ , which I used for html/css/javascript.
http://www.funprogramming.org/
Thanks.