module bend_pattern(width, height, materialThickness, maxPolyLength, maxPolygHeight){
    //calculate the exact length by calculating how many polygons fit into the square and then dividing the base width with this number.
    polyLengthCount = ceil(width/(maxPolyLength*1.25))-1;
    exactPolyLength = (width/polyLengthCount)*0.8;
    exactPolyDistance = (width/polyLengthCount)*0.1;
    // calculate the vertical density of the lines in a similar way
    polyHeightCount = ceil(height/((polyDistance+maxPolygHeight)))-1;
    exactHeight = height/polyHeightCount;
    exactPolygonHeight = exactHeight-polyDistance;
    difference()
    {
        // create base square
        square([ width,height ]);
        
        // generate alternating lines of polygons
        for(loopHeightCount = [0:polyHeightCount-1])
        {
            for (loopCount = [0:polyLengthCount-1])
            {
                
                polygon(points =    [  
                                        [loopCount*(exactPolyLength+exactPolyDistance*2)+exactPolyDistance,
                                            loopHeightCount*(polyDistance+exactPolygonHeight)+polyDistance/2+exactPolygonHeight/2],
                                        [loopCount*(exactPolyLength+exactPolyDistance*2)+exactPolyDistance+exactPolyLength/2,
                                            loopHeightCount*(polyDistance+exactPolygonHeight)+polyDistance/2+exactPolygonHeight],
                                        [loopCount*(exactPolyLength+exactPolyDistance*2)+exactPolyDistance+exactPolyLength/2,
                                            loopHeightCount*(polyDistance+exactPolygonHeight)+polyDistance/2],
                                        [loopCount*(exactPolyLength+exactPolyDistance*2)+exactPolyDistance+exactPolyLength,
                                            loopHeightCount*(polyDistance+exactPolygonHeight)+polyDistance/2+exactPolygonHeight/2]
                                    ],
                                        paths = 
                                        [
                                            [0, 1, 3, 2]
                                        ]
                                    );
            }
            if(loopHeightCount<=polyHeightCount-2)
            {
                translate([-(exactPolyLength+exactPolyDistance*2)/2,polyDistance/2+exactPolygonHeight/2])
                {
                    for (loopCount = [0:polyLengthCount])
                    {
                        polygon(points =    [  
                                                [loopCount*(exactPolyLength+exactPolyDistance*2)+exactPolyDistance,
                                                    loopHeightCount*(polyDistance+exactPolygonHeight)+polyDistance/2+exactPolygonHeight/2],
                                                [loopCount*(exactPolyLength+exactPolyDistance*2)+exactPolyDistance+exactPolyLength/2,
                                                    loopHeightCount*(polyDistance+exactPolygonHeight)+polyDistance/2+exactPolygonHeight],
                                                [loopCount*(exactPolyLength+exactPolyDistance*2)+exactPolyDistance+exactPolyLength/2,
                                                    loopHeightCount*(polyDistance+exactPolygonHeight)+polyDistance/2],
                                                [loopCount*(exactPolyLength+exactPolyDistance*2)+exactPolyDistance+exactPolyLength,
                                                    loopHeightCount*(polyDistance+exactPolygonHeight)+polyDistance/2+exactPolygonHeight/2]
                                            ],
                                                paths = 
                                                [
                                                    [0, 1, 3, 2]
                                                ]
                                            );
                    }
                }
            }
        }
    };
}
            
        
         
            
            
baseLength = 273;
baseWidth = 343;
materialThickness = 3;
boxLength = 67;
boxWidth = 46;
boxHeight = 20; 
widthFittingCount = (ceil(baseWidth/(boxWidth+materialThickness))-1);
lengthFittingCount = (ceil(baseLength/(boxLength+materialThickness))-1);
widthEdge = (baseWidth - (widthFittingCount*(boxWidth+materialThickness)))/2;
lengthEdge = (baseLength - (lengthFittingCount*(boxLength+materialThickness)))/2;
echo(lengthFittingCount);
if(widthEdge>=(materialThickness/2))
{
    difference(){    
        square([baseWidth,baseLength]);
        for (lengthCount = [0:lengthFittingCount-1])
        {
            for (widthCount = [0:widthFittingCount])
            {
                translate([
                widthEdge-0.5*materialThickness+widthCount*(materialThickness+boxWidth),
                (lengthEdge+0.5*materialThickness+lengthCount*(materialThickness+boxLength))+boxLength*0.25,
                0])
                {square([materialThickness,boxLength*0.5]);}; 
            }
		}
		for (lengthCount = [0:lengthFittingCount])
		{
			for (widthCount = [0:widthFittingCount-1])
			{
                translate([
                (widthEdge+0.5*materialThickness+widthCount*(materialThickness+boxWidth))+boxWidth*0.25,
                lengthEdge-0.5*materialThickness+lengthCount*(materialThickness+boxLength),
                0])
                {square([boxWidth*0.5,materialThickness]);};
			}        	
		}
	}
    for (pieceCount = [0:widthFittingCount])
    {
        translate([baseWidth+1+pieceCount*(boxHeight+materialThickness+1),0,0])
        {
            difference()
            {
                union()
                {
                    for (gap = [0:lengthFittingCount-1])
                    {
                        translate([
                        boxHeight,
                        materialThickness+0.25*boxLength+gap*(materialThickness+boxLength),
                        0])
                        {square([materialThickness,boxLength*0.5]);};
                    }
                    square([boxHeight,(materialThickness+boxLength)*lengthFittingCount+materialThickness]);
                }
                for (gap = [0:lengthFittingCount])
                    {
                    translate([
                    0,
                    gap*(materialThickness+boxLength),
                    0])
                    {square([boxHeight*0.5,materialThickness]);};
                }
            }
        };
    }
    
    for (pieceCount = [0:lengthFittingCount])
    {
        translate([baseWidth+1+(widthFittingCount+1)*(boxHeight+materialThickness+1)+pieceCount*(boxHeight+materialThickness+1),0,0])
        {
            difference()
            {
                union()
                {
                    for (gap = [0:widthFittingCount-1])
                    {
                        translate([
                        boxHeight,
                        materialThickness+0.25*boxWidth+gap*(materialThickness+boxWidth),
                        0])
                        {square([materialThickness,boxWidth*0.5]);};
                    }
                    square([boxHeight,(materialThickness+boxWidth)*widthFittingCount+materialThickness]);
                }
                for (gap = [0:widthFittingCount])
                    {
                    translate([
                    boxHeight*0.5,
                    gap*(materialThickness+boxWidth),
                    0])
                    {square([boxHeight*0.5,materialThickness]);};
                }
            }
        };
    }
}
            
        
        