Split Rectangle

Split rectangle by specifying number of divisions, minimum and maximum area
rctg1 = Rectangle.ByWidthLength(10,10);
//Number of Lines
cnt;
//Minimum Area of Polygon
min;
//Maximum Area of Polygon
max;
//Max attempts
atm = 500;
[Imperative]
{
surf1 = rctg1.Patch();
lins1 = [];
c = 0;
t = [];
sed = 0;
test1 = true;
sed = sed + 1;
while(test1 && sed<atm)
{
lins1 = lins(rctg1,cnt,sed);
surf1 = spl(rctg1,lins1);
for (s in surf1)
{
if (s.Area<min || s.Area>max)
{
t[c] = true;
c = c + 1;
}
else
{
t[c] = false;
c = c + 1;
}
}
sed = sed + 1;
test1 = List.Contains(t,true);
}
return surf1;
};
//Functions
def lins(rec,cnt,sed)
{
edgs1 = rec.Explode();
parm1 = List.TakeItems(List.Shuffle(0.1..1..#(Math.Random(sed)*50)),cnt/2);
parm2 = List.TakeItems(List.Shuffle(0.1..1..#(Math.Random(sed+1)*50)),cnt/2);
parm3 = List.TakeItems(List.Shuffle(0.1..1..#(Math.Random(sed+2)*50)),cnt/2);
parm4 = List.TakeItems(List.Shuffle(0.1..1..#(Math.Random(sed+3)*50)),cnt/2);
pnts1 = edgs1[0].PointAtParameter(parm1);
pnts2 = edgs1[1].PointAtParameter(parm2);
pnts3 = edgs1[2].PointAtParameter(parm3);
pnts4 = edgs1[3].PointAtParameter(parm4);
lins1 = List.TakeItems(List.Flatten(Line.ByStartPointEndPoint([pnts1,pnts2],[pnts3,pnts4]),-1),cnt);
return lins1;
};
def spl(rect,lines:var[]..[])
{
//Cutting Tool
tool = Solid.ByUnion(List.Flatten([rect,lines],-1).Extrude(Vector.ZAxis()).Thicken(0.4));
//Divisions
divisions = List.Clean(PolyCurve.ByJoinedCurves(rect.Patch().Split(tool).PerimeterCurves()),false).Offset(0.2,false).Patch();
return divisions;
};