Why my draw() don't drawing continuously?

void setup () { size (842,480);frameRate(20);}
float x = random (842);
float y = random (480);
float r = random (256);
float g = random (256);
float b = random (256);
  void draw () {loop ();fill (r,g,b);rect (x,y,x,y);}

Can someone explain me pls why ? :3

Hi @C2014,

Welcome to the forum! :wink:

As stated in the post, please format your code with the </> button. (also press Ctrl+T to format your code in the Processing editor)

In fact the draw() function is being executed multiple time per second (in this case roughly 20).

The issue is that the x, y, r… variables are declared outside of the setup() and draw() so their random value is only assigned once.

Which means that it’s drawing your rectangle repeatedly with the same parameters.

Try to think of a way to move those lines so that at each draw() call, a new random value is used :innocent:

1 Like

thank you for the answer, Can you please give me a hint, how i can modify the code to be sure that the created rectanlges may never leave the bounds of the window?

No problem!

Try to think about how you would create a rectangle starting from the top left corner that wouldn’t overflow the canvas size.

What maximum size should it have? In width, height?

The process can be the following:

  1. Pick a random point on the canvas
  2. Compute random dimensions (width and height) for the rectangle based on the window size
  3. Display that rectangle

But if the starting point of rect is for example in the middle of the screen, then, despite the restrictions in X or Y, it will still go beyond the window

No, think about it. If the corner is in the middle of the screen, it can still be inside in the window.

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

void draw() {
  rect(width / 2, height / 2, 90, 80);
  noLoop();
}

image

Then what would be it’s maximum width and height from that point?

Sorry, I’m not really catching up with your idea, Imagine that the rectangle will be near the maximum width and due to random () scale, it will still go beyond

That’s why you need to constraint the random value to be in a certain range [0, maximumWidth] (same for height).

You can do that with random(low, high).

You need to compute that maximumWidth value geometrically, make a drawing to see what it looks like :wink:

Okay, thank you very much, apparently it’s too complicated for me, or I just don’t understand your idea completely

What would you like to do in this case? You can.
Avoid creating this rect?
Adapt the size to never go beyond canvas?
Draw it mirrored?
You can.
:slight_smile:

All right here is a visual representation of what you are trying to achieve:

  • (cx, cy) are the top left coordinates of the rectangle
  • The canvas has width and height dimensions

Then ideally if you randomly picked this point on the canvas, then what is the maximum size of the rectangle so that it doesn’t overflow the canvas?

You want to compute its max width (maximum width) and max height (maximum height) so that the random value will not be greater than that.

A pseudo code:

// Pick a random point on the canvas
float cx = random(width);
float cy = random(height);

float maxWidth = ???;
float maxHeight = ???;

float rectWidth = random(0, maxWidth);
float rectHeight = random(0, maxHeight);

// And here you have it!!
2 Likes

One other approach is to use a conditional if/else statement.
In pseudo: If rect x position & y position is greater than width & height then reset x position & y position to within width and height dimensions.

EDIT: But as @josephh has outlined, you will need to add width & height for the rectangles into your conditions.

:nerd_face:

1 Like

Yes this is one possible solution of course! :wink:

Another one is:

  • Compute random width and height
  • If it overflows the canvas
  • Generate it again
  • Do that until the rectangle does not overflow the canvas

This solution might be inexpensive because of additional loop iterations (if it did overflow). But it’s straightforward to implement and doesn’t require any “math”.

1 Like

Touche @josephh!!
(I have to write more characters for this to post. So there).
:nerd_face:

1 Like

I can just reduce the scale to width/2 and height/2, to minimize the change of overgrowth

reduce the chance != avoid

What’s the solution to the ??? marks above?
How can you say: my max is the distance from c point to canvas right/bottom corner?
It’s an easy calculation :wink:

1 Like