Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

I've written a mathematia code which arranges $n$ unit squares in columns of length $m$ (of course, when $n$ is not divisible to $m$, then the last column is not full). This is the code:

n = 60;
m = 1;
l = {};
For[i = 0, i <= n - 1, 
 l = Append[l, 
   Rectangle[{Quotient[i, m], Mod[i, m]}, {Quotient[i, m] + .9, 
     Mod[i, m] + .9}]]; i++]
Graphics[{Red, l}]

Here's my problem: For a fixed value for $n$ (say, $60$), if you change $m$ then you get squares of smaller or larger sizes, as shown below. I want all the squares for different $m$'s be of the same size. What should I do?

Thanks in advance.

for $n = 60$ and $m = 1$:

enter image description here

for $n = 60$ and $m = 13$:

enter image description here

share|improve this question

4 Answers 4

up vote 3 down vote accepted

I've kept your For loop but you should ween yourself off that.

n = 60;
m = 1;
unit = 20;
l = {};
For[i = 0, i <= n - 1, 
 l = Append[l, 
   Rectangle[{Quotient[i, m], Mod[i, m]}, {Quotient[i, m] + .9, 
     Mod[i, m] + .9}]]; i++]
Graphics[{Red, l}, ImagePadding -> 0, PlotRangePadding -> 0, 
 ImageSize -> {unit*Ceiling[n/m], unit*m}]

enter image description here

m=60;n=4;

enter image description here

share|improve this answer
    
Thank you very much. But what don't you like about the "for"? Do you suggest a better alternative? –  Behzad Nov 19 '13 at 21:31
1  
More precisely Ceiling[n/m] + w - 1 where w=0.9 is the size of squares. –  ybeltukov Nov 19 '13 at 21:34
1  
e.g. Rectangle[{Quotient[#, m], Mod[#, m]}, {Quotient[#, m] + .9, Mod[#, m] + .9}] & /@ Range[0, n - 1] but there are probably several others –  Mike Honeychurch Nov 19 '13 at 21:34
    
@MikeHoneychurch Wow! Your last comment makes it possible to use Manipulate, which makes using the code much easier. Thanks. –  Behzad Nov 19 '13 at 22:15
1  
@Behzad there are lots of ways to address this as per other answers -- my answer deals solely with the problem you encountered. I'd recommend learning more about functional programming and study some of the other answers. –  Mike Honeychurch Nov 19 '13 at 22:24

Mike Honeychurch has addressed the real issue, ImageSize, but for fun here is another way to create the grid of rectangles:

Graphics @ {Red, 
 Translate[Rectangle[{0, 0}, {0.9, 0.9}], IntegerDigits[Range[n] - 1, m, 2]]}

enter image description here

share|improve this answer

An alternative approach

n = 60;
m = 13;
Grid[Reverse@Transpose@Partition[ConstantArray["■", n], m, m, 1, ""], 
 Spacings -> {0, 0}, ItemStyle -> Red]

enter image description here


Yet another approach

ArrayPlot[MapAt[White &, #, {1 ;; -1 - Mod[n, m, 1], -1}] &@
   ConstantArray[Red, {m, Ceiling[n/m]}], Mesh -> True, MeshStyle -> White,
      PixelConstrained -> 10]

enter image description here

share|improve this answer

Here is a relatively nasty way to generate the coordinates of the rectangles, but I figured I'd share it anyway:

Starting with a list of length n, partition it into lists of length m putting any excess elements (in case m does not divide n) in their own shorter list.

n = 7; m = 3;
Partition[ConstantArray[1, {n}], m, m, 1, {}]
(* {{1, 1, 1}, {1, 1, 1}, {1}} *)

If we map over this list on the second level we get something quite nice:

MapIndexed[
 #2 &, (* #2 because the second argument is the position of the element *)
 Partition[ConstantArray[1, {n}], m, m, 1, {}],
 {2} (* Map on second level *)
]
(*{
   {{1, 1}, {1, 2}, {1, 3}},
   {{2, 1}, {2, 2}, {2, 3}},
   {{3, 1}}} *)

Now put this together and make some rectangles:

n = 60; m = 13;
Graphics[{
  EdgeForm[White], (* Edges to look like padding between rectangles *)
  FaceForm[Red],
  MapIndexed[
    Rectangle[#2, #2 + {1, 1}] &,
    Partition[ConstantArray[1, {n}], m, m, 1, {}],
    {2}]
  }]

enter image description here

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.