We are about to switch to a new forum software. Until then we have removed the registration on this forum.
I have two different shapes that I'm trying to generate on a grid, but I would also like for the code to be flexible to the chosen size of the canvas. I didn't initially approach it with an array because I'm not as confident in my ability to properly utilize an array, and I'm still not sure that's the right way to go anyway.
What I ended up doing (or attempting to do, anyway) is break the canvas into different "points", one per 100 pixels. The goal from there was to draw one of the two shapes centered at each point randomly.
Here's a rough paraphrase of the code I'm using:
int csize = 500;
This is to make the canvas size flexible (or at least flexible in that I only have to change one line of code to alter the whole thing and keep it functional)
int cd100 = (csize / 100);
"cd100" meaning canvas divided by 100
float xpos100 = random(cd100);
float ypos100 = random(cd100);
Now the idea here, that I've checked and seen is broken in a separate debug sketch, is to generate a random whole number between 0 and 5 (in this case). This could probably be solved by limiting the selected random number to a whole number (as allowing decimals removes the function of my weird workaround all together), is there a way to do this?
float xpos = (xpos10*100);
float ypos = (ypos10*100);
Then multiply it back up to fit on the full canvas size. In this example, it would ideally limit the options to 0, 100, 200, 300, 400, and 500.
So the basic question to solve my current version is this: is there a way to limit the options selected by random() to whole numbers?
EDIT: Figured it out myself. Should have dug a little deeper in the reference section. I'm leaving this up though, because if anybody has any suggestions about whether I should be using an array I'd love to hear it
But also, as any Processing veteran could probably see, my solution is pretty rudimentary and heavy-handed. If anybody has another, more sensible or efficient method of doing what I'm trying to accomplish I'd love to hear it. Thanks for you time.
Answers
"This is to make the canvas size flexible (or at least flexible in that I only have to change one line of code to alter the whole thing and keep it functional)"
A better (more idiomatic) way to do this in Processing is just to set this size in the size() call (this size is used when you export, for example), then to use the built-in variables width and height. No need to duplicate them...
Whole random number:
The map() function might be useful to you, too.
Another more Java-ish way for integer picked numbers using (int) cast operator: (~~)
Or use round() when you want to have some chance to get 5 too: 8-X
yes, round() is a bad solution, destroys the uniform distribution of random() - 0 and 5 will occur only half as often as the others.