1
$\begingroup$

Is it possible to initialize Graphics Directives when constructing Graphics? (=when writing a code for a Graphics?)

To make you understand here is an imaginary code :

Graphics[{Opacity[0.5], Red, Disk[], Green, Disk[{1, 0}, 1],
  Directive["Initialize"], Blue, Disk[{2, 0}, 1]}]

Directive["Initialize"] is an imaginary code. I hope it act like Opacity[1.]

I upload a screenshot and codes :

enter image description here

Graphics[{Opacity[0.5], Red, Disk[], Green, Disk[{1, 0}, 1],
  Blue, Disk[{2, 0}, 1]}]

Show[Graphics[{Opacity[0.5], Red, Disk[], Green, Disk[{1, 0}, 1]}],
 Graphics[{Blue, Disk[{2, 0}, 1]}]]

Graphics[{Opacity[0.5], Red, Disk[], Green, Disk[{1, 0}, 1],
  Directive["Initialize"], Blue, Disk[{2, 0}, 1]}]

Note that there is a built-in Directive. Find ref/Directive in the MMA document.

$\endgroup$
1
  • 4
    $\begingroup$ You can use {...} to apply directives only to some of the primitives: try {{Opacity[0.5],...},Blue,...}, which will make sure that the "effect" off Opacity[0.5] "stops" before Blue $\endgroup$
    – Lukas Lang
    Jun 12, 2023 at 5:17

1 Answer 1

2
$\begingroup$

Making Lukas's suggestion in comments into a symbol with UpValues that make it behave like your Directive["Initialize"]:

resetDirectives /: {a___, resetDirectives[], b___} := {{a}, b}

Graphics[{AbsolutePointSize[15], Purple, Point[{-2, 0}], Opacity[0.5],
   Red, Disk[], Green, Disk[{1, 0}, 1], Blue, AbsoluteThickness[5], 
  Disk[{2, 0}, 1], Point[{4, 0}], 
  Line[Prepend[Last@#]@# &@CirclePoints[{4, 0}, 1, 3]]}]

enter image description here

Graphics[{AbsolutePointSize[15], Purple, Point[{-2, 0}], Opacity[0.5],
   Red, Disk[], Green, Disk[{1, 0}, 1], 
  resetDirectives[], 
  Blue, AbsoluteThickness[5], Disk[{2, 0}, 1], 
  Point[{4, 0}], Line[Prepend[Last@#]@# &@CirclePoints[{4, 0}, 1, 3]]}]

enter image description here

Graphics[{AbsolutePointSize[15], Purple, Point[{-2, 0}], Opacity[0.5],
   Red, Disk[], Green, Disk[{1, 0}, 1], 
  resetDirectives[], 
  Blue, AbsoluteThickness[5], Disk[{2, 0}, 1], 
  resetDirectives[], 
  Point[{4, 0}], Line[Prepend[Last@#]@# &@CirclePoints[{4, 0}, 1, 3]]}]

enter image description here

$\endgroup$
2
  • $\begingroup$ Works perfectly! and it is outstanding example of UpValues! Very impressive! $\endgroup$
    – imida k
    Jun 12, 2023 at 10:33
  • $\begingroup$ For users : note that the definition of resetDirectives [] has an inductive structure. Note that there is { } for only a in {{a}, b}. $\endgroup$
    – imida k
    Jun 12, 2023 at 10:41

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.