We are about to switch to a new forum software. Until then we have removed the registration on this forum.
The following is a function that is supposed to draw 500 randomly placed blades of grass. For simplicities sake, I made these blades of grass triangles. I set up a while loop to limit the amount of times a triangle while be drawn. Upon execution, triangles are drawn to the constraints specified, the problem is that the function is supposed to stop drawing triangles after the 501 triangle is drawn.
For some reason, triangles are drawn at random positions (within my constraints) infinitely regardless of the counter reaching its limit. Does anyone have any idea why this is?
void grass() {
int grasscounter=0;
float g1x=random(0, 700);
float g1y=random(400, 500);
float g2x=constrain(random(0, 700), g1x-2, g1x-3);
float g2y=constrain(random(400, 500), g1y+2, g1y+3);
float g3x=constrain(random(0, 700), g1x+2, g1x+3);
float g3y=constrain(random(400, 500), g1y+2, g1y+3);
while (grasscounter<500)
{
println(grasscounter);
int p1x=int(g1x); // The following is supposed to convert the randomly generated triangle positions into int values. I thought it might fix the loop problem.
int p1y=int(g1y);
int p2x=int(g2x);
int p2y=int(g2y);
int p3x=int(g3x);
int p3y=int(g3y);
fill(0, 255, 0);
triangle(p1x, p1y, p2x, p2y, p3x, p3y);
grasscounter=grasscounter+1;
}
}
Answers
Hi @raycharles -- heads up -- Folks who want to help will need to see your code formatted and since it's a question involving draw, you might want to include the setup and draw functions, but especially the draw(). Write your code line for line and then highlight it and click on the C button. Good luck.
I have formatted the code for you so we can see what is happening( @clair shows you how to do it).
The problem is not with the loop since I ran the method and it printed numbers up to 499 as expected. One problem I did get was that it only displayed one triangle and looking at your code the variables p1x, p1y etc. never change.
I have modified your code to this and it displayed 500 triangles. I suspect that the probllem was caused by you including the line
int grasscounter=0;
inside the while look so it could never reach 500
I see. So far as the formatting goes, I'll be sure to do that next time and I apologize about the confusion. That aside, I can't believe it was something so simple. Did the outputted triangles appear all at once, or did they continue to multiply in the same area ?
I ask because the size of my triangles are extremely small. While my counter should stop any more than 500 triangles from being drawn, I feel as though a infinite amount of triangles are being drawn over each other.
The code you provided did NOT create an infinite numbers of triangles. It drew 500 triangles all in the same place so it looked like just one triangle.
A much more simplified example: ;))
@quark I thought as much, I just wasn't quite sure. Thank you again for your input.
@GotoLoop I see where I was going wrong a bit better now. Your solution is much more concise than my own, though it has everything I was going for within my own code.
Question answered.