Rectangle should grow then stop (resolved)

hello, i’m a beginner on processing and I want to make something simple but I don’t really understand why it don’t work.

here’s my code :

void setup()
{
  size(600, 600);
  background(255);
}

void draw()
{
  for (int a = 0; a<width; a+=1)
  {
    rect(100, 100, a, 100);
    if (a>450)
    {
      a+=0;
    }
  }
}

what i want it to do is draw a rectangle that get larger (fro left to right) and when this rectangle get
close to the width of the window it stop growing and it stay in this position.
actually i managed to make it growing but when i put the “if” and what’s in it start already grown untill outside and it don’t work.
thanks for your help.

1 Like

The image your sketch is drawing is only updated and shown to the user when the draw() function is finished. Your entire loop happens in each frame, and at the end of that loop, the rectangle is already long.

What you need to do is this:

int a = 0;

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

void draw() {
  background(255);
  if (a<450) {
    a++;
  }
  rect(100, 100, a, 100);
}

Notice that this will cause a small rectangle to be drawn the first time draw() finishes, and a larger one the next time. And so on.

2 Likes

thank you it’s working perfectly as I wanted !