0
$\begingroup$

How can I combine graphics inside a For loop? For example, the following yields a red rectangle:

Graphics[{Red, Rectangle[{0, 0}, {1, 3}]}]

But how can I get a long line of them? The following code shows what I would like to do, even if it does not work!

For[i = 1, i < 11, i++, 
 Graphics[{Red, Rectangle[{3 i, 0}, {3 i + 1, 3}]}]]
$\endgroup$
1
  • $\begingroup$ Why is this question off-topic? The template reason cites either a "simple mistake" or place in the documentation. What mistake or what place? $\endgroup$
    – ruffle
    Sep 7 '20 at 8:42
2
$\begingroup$

Oh well...having puzzled over this for about half an hour, as soon as I post the question I realise how to answer it! But I will leave this here in the hope that it helps other people.

The solution I found is to use Table, like this:

myrectangles = Table[{Red, Rectangle[{3 i, 0}, {3 i + 1, 3}]}, {i, 1, 10}];
Graphics[myrectangles]
$\endgroup$
1
  • 1
    $\begingroup$ I might try: Graphics[{Red, Rectangle[{3 #, 0}, {3 # + 1, 3}]} & /@ Range[10]] $\endgroup$
    – bill s
    Sep 1 '20 at 23:49

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