Skip to main content
1 of 4
Dr. belisarius
  • 116.2k
  • 13
  • 205
  • 456

I thought it could be easier using IntegerPartitions[], but the code got too convoluted.

part[r_] := Module[{d},
              d = Prepend[#,0]&/@ (IntegerPartitions[#,All,Range@3,1][[1]] & /@ r[[3;; 4]]); 
              Flatten /@ Tuples[Transpose /@
                     ({r[[#]] + Most@Accumulate@d[[#]], Rest@d[[#]]} & /@ {1, 2})] /. 
                      {x_Integer, w_, y_, h_} -> {x, y, w, h}]

part[{2, 3, 6, 5}]
(*
{{2, 3, 3, 3}, {2, 6, 3, 2}, {5, 3, 3, 3}, {5, 6, 3, 2}}
*)
part[{2, 3, 7, 9}]
(*
{{2, 3, 3, 3}, {2, 6, 3, 3}, {2, 9, 3, 3}, {5, 3, 3, 3}, 
 {5, 6, 3, 3}, {5, 9, 3, 3}, {8, 3, 1, 3}, {8, 6, 1, 3}, {8, 9, 1, 3}}
*)
Dr. belisarius
  • 116.2k
  • 13
  • 205
  • 456