2

my task is to draw a grid of 10 x 10 wheels. Each should be a random color and have lots of spokes like a real wheel. I have to do this using nested for loops.

I have got to the code below but for some reason its drawing a 10 x infinite grid. I don't understand why as the outer loop should only run 10 times.

float XPos = 25;
float YPos = 25;
float Radius = 20;

void setup() {
    size(500, 500);
}

void draw() {
    for (int h = 0; h < 10; h++) {
        for (int i = 0; i < 10; i++) {
            float RanR = random(250);
            float RanG = random(250);
            float RanB = random(250);

            stroke(RanR, RanG, RanB);
            ellipse(XPos, YPos, Radius * 2, Radius * 2);

            for (int j = 0; j < 360; j += 10) {
                stroke(RanR, RanG, RanB);
                line(XPos, 
                     YPos, 
                     XPos + Radius * sin(radians(j)), 
                     YPos + Radius * cos(radians(j))
                    );
            }

            XPos += Radius * 2;
        }

        XPos = 25;
        YPos += Radius * 2;
    }
}
5
  • 2
    You have to show what does these methods stroke and line do? Also you have three nested fors so in a simple math you have 10*10*36 (360/10) Commented Oct 13, 2015 at 16:40
  • yes stroke determines the color of the lines. line draws the line. the inner for loop repeats 36 times to draw the spokes and when its finished it will move on to the next cirlce Commented Oct 13, 2015 at 16:42
  • I can't see why is printing infinite circles, maybe you should post all the code, but be careful, post only the code that you think be important Commented Oct 13, 2015 at 16:52
  • 1
    @JorgeCampos This is Processing, not strict Java. Those are not user-defined methods, but methods in the Processing API. Commented Oct 13, 2015 at 16:54
  • that is all the code, turns out the draw void was being done over and over which was making infinite rows Commented Oct 13, 2015 at 16:56

2 Answers 2

2

The draw() function is automatically called 60 times per second. You're saving your XPos and YPos variables outside of the draw() function, so any changes you make to them inside your draw() function will persist the next time draw() is called.

Instead of saving your XPos and YPos variables at the top-level of the sketch, you could create them inside your for loop and base them off of h and i:

float Radius = 20;

void setup() {
  size(500, 500);
}

void draw() {
  for (int h = 0; h < 10; h++) {
    for (int i = 0; i < 10; i++) {

      float XPos = 25+40*h;
      float YPos = 25+40*i;
      float RanR = random(250);
      float RanG = random(250);
      float RanB = random(250);

      stroke(RanR, RanG, RanB);
      ellipse(XPos, YPos, Radius * 2, Radius * 2);

      for (int j = 0; j < 360; j += 10) {
        stroke(RanR, RanG, RanB);
        line(XPos, 
        YPos, 
        XPos + Radius * sin(radians(j)), 
        YPos + Radius * cos(radians(j))
          );
      }
    }
  }
}

Notice that this will cause your circles to be redrawn in random colors 60 times per second. You could either use the randomSeed() function to get the same random numbers each time, or you could call noLoop() at the end of draw() so that it's not automatically called again.

Sign up to request clarification or add additional context in comments.

2 Comments

thanks that works too and is alot better. I would just have to remove the draw void so the code doesnt repeat
@Will That's probably not a very good idea. Use noLoop() instead.
0

Your code looks well, except that draw must be reentrant. That is, upon being called a second time it should start drawing at the same position.

So (apart from the Radius), the fields should be kept local to the draw method.

void draw() {
    float XPos = 25;
    float YPos = 25;
    float Radius = 20;

In real GUIs the draw or paint method is called whenever the screen must be updated.

My guess: draw is called more than once.

BTW. Java convention is to use an initial small letter for fields, local variables, and methods: xPos, yPos, radius.

4 Comments

how is it called multiple times? because of line or ellipse function calls?
thanks u got it, i took out the void setup and void draw so the code only runs once and it gave me a 10 x 10 grid. thank alot
"Called directly after setup(), the draw() function continuously executes the lines of code contained inside its block until the program is stopped or noLoop() is called. " – processing.org/reference/draw_.html
This is not true. The draw() function does not have to be reentrant. It is one possible solution in this particular case, but Processing's draw() function does not have to be reentrant at all.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.