|
Author |
Topic: bq: fixed number vs. variable number (Read 541 times) |
|
roland_g
|
bq: fixed number vs. variable number
« on: Jul 16th, 2004, 6:40pm » |
|
warning.. beginners question: Can anybody explain to me why "height/50" doesn't work for "int hsquare", but "10" results in a correct drawing. Code: void setup() { size(500,500); } int sum; //int hsquares = height/50; //int vsquares = width/50; int hsquares = 10; int vsquares = 10; int hsize = 50; int vsize = 50; int oddcolor = 32; int evencolor = 220; void loop() { for (int i=0; i<=hsquares; i++){ for (int j=0; j<=vsquares; j++){ sum = i+j; if (sum%2 == 1) { //if the sum is odd fill(oddcolor); //we fill with oddcolor } else { fill(evencolor); //else we fill with evencolor } rect(i*hsize ,j*vsize , hsize , vsize); // now a rect... } } } |
| PS: Please feel free to correct me if I am using any terms in a wrong way.
|
|
|
|
arielm
|
Re: bq: fixed number vs. variable number
« Reply #1 on: Jul 16th, 2004, 6:49pm » |
|
it's because width & height are not initialized yet with real values (the fact that your "global variables" initialization block is located after setup() in the source-code is ignored by processing... this part always occurs before setup()...) solution: int hsquares; int vsquares; setup() { size(500, 500); hsquares = height / 50; ... }
|
Ariel Malka | www.chronotext.org
|
|
|
REAS
|
Re: bq: fixed number vs. variable number
« Reply #2 on: Jul 17th, 2004, 12:37am » |
|
I'll make a note of this in the reference. Thank you for bringing it up. Ariel, I think you put those globals wherever you like, including the bottom of the page. Please give it a test.
|
|
|
|
arielm
|
Re: bq: fixed number vs. variable number
« Reply #3 on: Jul 17th, 2004, 1:09am » |
|
yeah, of course that it's possible to put a "global" variable (i.e. a public field that belongs to the mother-applet) anywhere in the source-code _ it's just a matter of style _ but at the end of the day, when it gets compiled: it occurs before setup(), hence roland_g's problem. i think that one helpful practice for "beginners" is to use the following construct: whenever you need global variables, declare them but leave them empty: int foo; then, give them a value inside setup(): void setup() { foo = 5; } avoids a lot of potential headaches, no?
|
Ariel Malka | www.chronotext.org
|
|
|
roland_g
|
Re: bq: fixed number vs. variable number
« Reply #4 on: Jul 17th, 2004, 4:36pm » |
|
Thanks for the answer arielm, but putting the variables before the void setup() section doesn't make it work either. I am a bit confused about why width and height haven't been inialized yet, when I put it under the setup() section. I thought that the viod setup() function was the first thing that gets compiled/read (or whatever you call that). Code: int hsquares = height/50; int vsquares = width/50; //int hsquares = 10; //int vsquares = 10; void setup() { size(500,500); } int sum; int hsize = 50; int vsize = 50; int oddcolor = 32; int evencolor = 220; void loop() { for (int i=0; i<=hsquares; i++){ for (int j=0; j<=vsquares; j++){ sum = i+j; if (sum%2 == 1) { //if the sum is odd fill(oddcolor); //we fill with oddcolor } else { fill(evencolor); //else we fill with evencolor } rect(i*hsize ,j*vsize , hsize , vsize); // now a rect... } } } |
|
|
|
|
|
TomC
|
Re: bq: fixed number vs. variable number
« Reply #5 on: Jul 17th, 2004, 4:45pm » |
|
The variable declarations you are using outside of functions (including setup) are executed before anything else. As arielm has pointed out, the location of these in relation to functions is irrelevant, so it's OK to do this anywhere in the code. However, it's easiest to understand if you only do it at the top. Your specific problem is because inside setup, the call to size() initialises width and height, so you can't refer to them in a variable initialisation outside of a function. That's because the declarations outside of setup are called before the contents of setup. Looks like I might have said the same thing twice there, but I hope it's clear. The workaround is to put some variables for width and height at the top of your code, and use those for size() and for variable declarations outside of functions. That will work. Code: int WIDTH = 500; int HEIGHT = 500; int hsquares = HEIGHT/50; int vsquares = WIDTH/50; void setup() { size(WIDTH,HEIGHT); } // etc... |
|
|
« Last Edit: Jul 17th, 2004, 5:02pm by TomC » |
|
|
|
|
roland_g
|
Re: bq: fixed number vs. variable number
« Reply #6 on: Jul 17th, 2004, 7:58pm » |
|
got it. Thank you for the explanation Tom.
|
|
|
|
|