16
$\begingroup$

I know there is a Rectangle[] function, but it is always filled with some color. What I need is a rectangle, which is empty inside (only it's boundaries visible). I just need to overlay it on plotted function to indicate some of it's area.

$\endgroup$
  • 2
    $\begingroup$ Did you try a Line yet? $\endgroup$ – Yves Klett Sep 11 '12 at 19:57
  • $\begingroup$ Was just about to start doing it, but now I see Sjoerd's answer is exactly what I need. $\endgroup$ – alex Sep 11 '12 at 20:17
29
$\begingroup$

Use FaceForm[] to define the polygon's filling as empty. You set the polygon's outline color with EdgeForm[color]

Graphics[{EdgeForm[{Thick, Blue}], FaceForm[], Rectangle[]}]

or slightly more complex:

Graphics[
   {
     EdgeForm[{Thick, Hue[Random[]]}], 
     FaceForm[], 
     Rectangle[#, # + {4, 4}]
   } & /@ RandomReal[{-10, 10}, {30, 2}]
]

Mathematica graphics

$\endgroup$
5
$\begingroup$

Just for completeness, it is hardly difficult to do this manually:

box[{x_, y_}, {X_, Y_}] := 
  Line[{{x, y}, {x, Y}, {X, Y}, {X, y}, {x, y}}];

Graphics[{
  {Red, Rectangle[{0, 0}, {1, 3}]},
  {Blue, Rectangle[{2, 1}, {4, 2}]},
  {Thick, box[{0, 0}, {1, 3}]}
 }]

Mathematica graphics

Manual control allows for customizations:

box2[{x_, y_}, {X_, Y_}] :=
 MapIndexed[
  {Arrowheads[Sqrt[#2]/30`], Arrow@#} &,
  Partition[{{x, y}, {x, Y}, {X, Y}, {X, y}, {x, y}}, 2, 1]
 ]

Graphics[{
  {Red, Rectangle[{0, 0}, {1, 3}]},
  {Blue, Rectangle[{2, 1}, {4, 2}]},
  {Thick, box2[{0, 0}, {1, 3}]}
 }, ImagePadding -> 10]

Mathematica graphics

$\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.