3
$\begingroup$

This code

Graphics[{Red, Polygon[{{0, 0}, {0, 10}, {1, 10}, {1, 0}}]}];
Graphics[{Blue, Polygon[{{1, 0}, {1, 9}, {2, 9}, {2, 0}}]}];
Graphics[{Green, Polygon[{{2, 0}, {2, 8}, {3, 8}, {3, 0}}]}];
Show[%, %%, %%%]

nicely creates what I need:

enter image description here

But, I need ten of them and it is tedious to do it one line at a time. For the corners, I tried NestList like this

NestList[{{# + 1, 0}, {# + 1, # - 1}, {# + 1, # - 1}, {# + 1, 
0}} &, {{0, 0}, {0, 10}, {1, 10}, {1, 0}}, 3]

but that did not work.

Should have hung in a moment longer. Found this to work

NestList[{{#[[1, 1]] + 1, 
0}, {#[[2, 1]] + 1, #[[2, 2]] - 1}, {#[[3, 1]] + 1, #[[3, 2]] - 
 1}, {#[[4, 1]] + 1, 0}} &, {{0, 0}, {0, 10}, {1, 10}, {1, 0}}, 3]

Still interested in better ways, though.

$\endgroup$

3 Answers 3

4
$\begingroup$

While others offer more compact code, here I favour strategies to keep a clean kernel, using scoping like Block to avoid leaving lingering definitions and which also gives you a single place to edit the relevant parameters like width, stepsize, etc...

Block[
{
  width = 5,
  stepsize = 1,
  nBars = 10,
  initialheight = 40,
  height,
  xposition 
},
Graphics[
  Table[
  height=initialheight-stepsize*k;
  xposition = width*k;
  {
    Directive[EdgeForm[Thin],Hue[0.9*k/(nBars-1)]],
    Polygon[{
      {xposition,0},
      {xposition+width,0},
      {xposition+width,height},
      {xposition,height}
    }]
   }
   ,{k,0,nBars-1}]
  ]
]

enter image description here

$\endgroup$
1
$\begingroup$
cols = ColorData[97][#] & /@ Range[10]
polys = Table[Rectangle[{i, 0}, {1 + i, 10 - i}], {i, 0, 9}]
Graphics[{
  Transpose[{cols, polys}]
  }]

enter image description here

The color list can be changed as required.

$\endgroup$
1
$\begingroup$
step = Table[
   Graphics[{Red, 
     Polygon[{{x1, 0}, {x1, 10 - x1}, {x1 + 1, 10 - x1}, {x1 + 1, 
        0}}]}], {x1, 0, 10}];

Show@Table[step[[ii]], {ii, 1, 11}]

steps

Edit: to get different colors

polygons = 
  Table[Polygon[{{x1, 0}, {x1, 10 - x1}, {x1 + 1, 10 - x1}, {x1 + 1, 
      0}}], {x1, 0, 10}];
colors = # & /@ RandomColor[11];
Graphics@(Thread@{colors, polygons})

3

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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