Draw rectangles inside resizable frame


#1

anyone remembers a good algorithm to smart fit multiple rectangles inside a resizable frame?

like I do here, but not finite.

here is a dummy code i wrote to film the above video

I remember doing this again using modulo % but I can’t remember how on earth I did it…
it’s one of those days , i can’t think clearly

needless to say
I tried google but didn’t stumble upon a anything relevant yet… :disappointed_relieved:
I am probably using wrong key words, there must be a smart solution for this

if anyone can help it would be greatly appreciated

int withPerItm = 400;
    int lastM=0;
    int lastM2=0;
    int lastM3=0;
    int lastM4=0;

    int limitWidth= (w-withPerItm);
    for (int i=0; i<myrects.size(); i++) {
       
        if(x+(i*withPerItm)<limitWidth){
        myrects[i].draw(x+(i*withPerItm), y, withPerItm, withPerItm);
            lastM=i+1;
        }else{
            int new_i = i-lastM;
            int stepDown=withPerItm;
            if(x+(new_i*withPerItm)<limitWidth){
            myrects[i].draw(x+(new_i*withPerItm), y+stepDown, withPerItm, withPerItm);
            lastM2 = i+1;
            }else{
                 new_i = i-lastM2;
                 stepDown = withPerItm*2;
                if(x+(new_i*withPerItm)<limitWidth){
                myrects[i].draw(x+(new_i*withPerItm), y+stepDown, withPerItm, withPerItm);
                lastM3=i+1;
                }else{
                    new_i = i-lastM3;
                    stepDown = withPerItm*3;
                    if(x+(new_i*withPerItm)<limitWidth){
                    myrects[i].draw(x+(new_i*withPerItm), y+stepDown, withPerItm, withPerItm);
                    }else{
                        new_i = i-lastM4;
                        stepDown = withPerItm*4;
                        if(x+(new_i*withPerItm)<limitWidth){
                            myrects[i].draw(x+(new_i*withPerItm), y+stepDown, withPerItm, withPerItm);
                        }else{
                            
                        }
                        
                    }
                }

            }
        }
    }

#2

(5 minutes later)

sorry about that… I figured it out… :coffee:

   int limitWidth= (w-withPerItm);
    int widthmodl = w/withPerItm;
    int ysteps =-1;


    for (int i=0; i<myrects.size(); i++) {      
        int widht = i%widthmodl;       
        if(widht==0)ysteps++;        
        myrects[i].draw(x+(widht*withPerItm), y+(withPerItm*ysteps), withPerItm, withPerItm);
       }