box packing 2D maps

I wanted a way to remove overlaps from some random rectangles.

each box getting pushed out, its a bunch of packed primitives with a vector attribute for width and height

Here is the vex code – run once in a detail wrangle it loops a number of times over the points and pushes them outside while checking for overlaps

int relax_iter=chi("iter");

vector pp[];

for(int i=0;i<npoints(0);i++){
    vector p1=point(0,"P",i);
    push(pp,p1);
}


for(int q=0;q<relax_iter;q++){
    vector newP={0,0,0};
    int in=0;
    for(int i=0;i<npoints(0);i++){
        for(int j=0;j<npoints(0);j++){
        
                vector min1 = pp[i]-point(0,"dimen",i);
                vector max1 = pp[i]+point(0,"dimen",i);
                vector p1 = pp[i];
                
        
                float x1min = min1.x;
                float x1max = max1.x;
                float z1min = min1.z;
                float z1max = max1.z;
                
                vector min2 = pp[j]-point(0,"dimen",j);
                vector max2 = pp[j]+point(0,"dimen",j);
                vector p2 = pp[j];
                
                float x2min = min2.x;
                float x2max = max2.x;
                float z2min = min2.z;
                float z2max = max2.z;
                
                newP = p1+rint(normalize(p1-p2)*4);
                
                in = x1min <= x2max && x2min <= x1max && z1min <= z2max && z2min <= z1max? 1:0;
                if(in){
                    pp[i]=newP;
                
            }
        }  
    }
}
        

for(int i=0;i<npoints(0);i++){
    setpointattrib(0, "P", i, pp[i], "set");
}

    
I also wanted to build a connected structure from this – so I used prim’s algorithm to connect a tree, I disconnected some of the small boxes to keep more primary paths, since the small islands gets connected any way when everything is converted to right angles

Leave a Reply

Your email address will not be published. Required fields are marked *