3
$\begingroup$

I'm new to Mathematica and I am trying to generate an array of rectangles which stays perfectly aligned and scaled correctly and able to control the opacity of each one. Could anyone point me on to the right direction?

Red Blocks


Many thanks to all for showing potential ways of solving the problem. I really appreciate!

$\endgroup$
3
$\begingroup$

Does something like this work for you?

Graphics[{
  Red,
  Rectangle[{0, 0}, {1, -4}],
  Opacity[0.8],
  Rectangle[{1.1, 0}, {2.1, -2}],
  Opacity[0.6],
  Rectangle[{2.2, 0}, {3.2, -2}],
  Opacity[0.4],
  Rectangle[{3.3, 0}, {4.3, -2}],
  Opacity[0.2],
  Rectangle[{4.4, 0}, {5.4, -2}],
  Black,
  Opacity[1],
  Table[Style[
    Text[{"B0", "B1", "B2", "B3", "B4"}[[i + 1]], {0.5 + 1.1 i, 
      If[i == 0, -4.2, -2.2]}], 16], {i, 0, 4}]
  }]

I've hard-coded all of the rectangles and opacities just to show you the order in which things can be done, but you could easily combine them into a Table and generate them programmatically like I've done for the labels.

Graphic with rectangles of different opacities.

$\endgroup$
  • $\begingroup$ Many thanks for pointing me out a direction possible ways of doing it. Many thanks! $\endgroup$ – antrofite_ Jan 12 at 11:02
2
$\begingroup$

You can start with a single rectangle and Translate and Scale it:

gap = 1/10; 
Graphics[{Red, {Opacity[1/(1 + #)], 
  Translate[Scale[Rectangle[{0, 1}, {1, 5}], {1, 1-Unitize[#]/2}], {(1 + gap) #, Unitize@#}],
  Opacity[1], Black, 
  Text[Style["B" <> ToString[#], 16], {1/2 + (1 + gap) #, 2/3 + 2 Unitize[#]}]} & /@
    Range[0, 5]}, 
 ImageSize -> 600]

enter image description here

$\endgroup$

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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