3
$\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?

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$

1 Answer 1

2
$\begingroup$

GridGraph belongs to the graph theory part of Mathematica and is neither an image nor a graphics. This causes many vicious problems.

Overlay can, in fact, overlay multiple graphics; but, the result is neither an image nor a graphics. This causes many vicious problems.

Show can overlay graphics properly. It is handy when combining plots, which graphics. The result is a graphic.

Graphics overlay earlier subexpressions with the later subexpressions. Therefore, the overlay operation can be done there also.

I leave as a reader's exercise to disasemble the following:

picture = Image[Graphics[
    {{LightRed, Translate[Rotate[Rectangle @@ (ConstantArray[5.*Sqrt[1/2], 
           {2, 2}]*{-1, 1}), 45*Degree], {5, 5}]}, 
     {Darker[Blue], (Line[{{0, #1}, {10, #1}}] & ) /@ Range[0, 10], 
      (Line[{{#1, 0}, {#1, 10}}] & ) /@ Range[0, 10], FaceForm[Gray], 
      EdgeForm[Darker[Blue]], Table[Disk[{x + 0.015, y - 0.015}, 0.08], 
       {x, 0, 10}, {y, 0, 10}]}, Text[Style["Text", Green, Bold, 72, 
       FontFamily -> "Times"], {5, 5}, {0, 0}]}, ImageSize -> {200, 200}]]
       
mask = Image[Graphics[{White, Translate[
      Rotate[Rectangle @@ (ConstantArray[5.*Sqrt[1/2], {2, 2}]*{-1, 1}), 
       45*Degree], {5, 5}]}, ImageSize -> {200, 200}, Background -> Black]]
       
ImageApply[If[#2 == {0, 0, 0}, {1, 1, 1}, #1] & , {picture, mask}]

Here are the three images.

Of course, I may have missed something.

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