0
$\begingroup$

I am using this code

pic1 = GridGraph[{19, 10}, PlotRange -> {-10, 10}];
pic2 = Graphics[ {{Inset[Style["text", Black, FontSize -> Scaled[0.1], FontFamily -> "Times"], {5, 5}] } } ]  ;
pic3 = Show[{pic1, pic2}] ;
pic4 = Graphics[ {LightRed, Rotate[Rectangle[{30, 30}, {50, 10}], 45 Degree]} ,ImageSize -> 200 ] ;

Overlay[{pic4, pic3}] 

and the result is $Fig1$. I have two questions:

Q1. How can I ask Mathematica to crop only the red area and give a result like Fig2? or, alternatively, is it possible to manually erase/remove the unwanted area in Fig1 and then export it as a PDF file?

Q2. Is it possible to control the position of the red square in Fig1 (the corresponding code pic4)? For example, moving it a bit to the right?

enter image description here

$\endgroup$
2
  • 2
    $\begingroup$ Look for RegionFunction $\endgroup$ Commented Mar 9, 2023 at 13:16
  • $\begingroup$ How to use RegionFunction with Overlay? What RegionFunction should I use to describe the rectangular area I am interested in? $\endgroup$
    – Martha97
    Commented Mar 9, 2023 at 15:00

1 Answer 1

1
$\begingroup$

Unfortunately RegionFunction doesn't work with Graphics or Show, sorry for my misleading hint in my comment.

As a workaround you might use a "passepartout" and mask your plot (see helpful answer from @Domen RegionFunction inside Show )

your modified plot(without masking, I changed Overlay to Show):

pic1 = GridGraph[{11, 10} ];
pic2 = Graphics[{{Text(*Inset*)[Style["text", Black, FontSize ->Scaled[0.1],FontFamily -> "Times"], {5, 5}]}}];
pic = {pic1, pic2} ;

reg = TransformedRegion[ Rectangle[{5, 3 }, {10, 8}] ,RotationTransform[Pi/4, {5, 3}]];
pic4 = Graphics[{LightRed, reg} ];

Show[{pic4, pic  } ]

enter image description here

final picture with passepartout:

frame = Rectangle [ {0, 0}, {11, 12}]; (* not to small*)
mask = BoundaryDiscretizeRegion[RegionDifference[frame, reg], 
MaxCellMeasure -> 1/30 , MeshCellStyle -> {1 -> None, 2 -> White}];

Show[{pic4, pic, mask  }  ]

enter image description here

The final picture might be clipped using PlotRange

Hope it helps!

$\endgroup$

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.