12
$\begingroup$

How can I draw an half or a third filled rectangle? I want to make one rectangle frame with a unity width and then filled it according to a value between 0 and 1.

$\endgroup$
3
  • 1
    $\begingroup$ Take a look at EdgeForm and FaceForm and draw two rectangles. $\endgroup$
    – Kuba
    Commented Jul 15, 2016 at 9:51
  • $\begingroup$ @Kuba is there a ColorFunction analogue for this case (can be used as Graphics primitive)? $\endgroup$
    – vapor
    Commented Jul 15, 2016 at 10:00
  • $\begingroup$ @happyfish there is an option VertexColors for a Polygon. $\endgroup$
    – Kuba
    Commented Jul 15, 2016 at 10:02

2 Answers 2

14
$\begingroup$
Graphics[{EdgeForm[Thick], White, Rectangle[], Black, Rectangle[{0, 0}, {1, 0.5}]}]

Mathematica graphics

Or indeed, if you want to be able to change the filling dynamically, you can wrap a manipulate around it.

Manipulate[
 Graphics[{EdgeForm[Thick], White, Rectangle[], Black, 
   Rectangle[{0, 0}, {1, a}]}], {a, 0, 1, 0.05}]
$\endgroup$
1
  • $\begingroup$ Thanks! That's exactly what I need. $\endgroup$
    – Miguel
    Commented Jul 15, 2016 at 10:35
9
$\begingroup$

For fun

Example

DynamicModule[
 {fill},
 fill = .5;
 Panel @ Column[{
    Slider[Dynamic @ fill, {0, 1}],
    Graphics[{
      {FaceForm @ White, EdgeForm @ {Thick, Black}, Rectangle[{0, 0}]},
      {FaceForm @ Black, Rectangle[{0, 0}, {1, Dynamic @ fill}]}
      }]
    },
   Alignment -> Center]
 ]

Output

output

$\endgroup$
1
  • $\begingroup$ +1 because it is a less taxing dynamic routine, and I agree that Mathematica is fun. $\endgroup$
    – Feyre
    Commented Jul 15, 2016 at 10:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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