6

Is there a function that gives you dimensions of Graphics? I mean x and y dimensions of rectangular region that the content of Graphics would fit into.

Graphics[{Table[
   Circle[RandomReal[{-10, 10}, 2], RandomReal[{0, 10}, 2], 
    RandomReal[{0, 2 π}, 2]], {n, 100}]}]

enter image description here

Update:

PlotRange is of no use since it is far from being precise.

v = Table[
   Circle[RandomReal[{-10, 10}, 2], RandomReal[{0, 10}, 2], 
    RandomReal[{0, 2 π}, 2]], {n, 100}];
Graphics[{v}];
Graphics[{v, {Transparent, EdgeForm[Red], 
   Rectangle @@ Transpose[PlotRange[%]]}}]

enter image description here

Also BoundingRegionis of no use for the same reason - being imprecise.

Carl Woll
  • 130,679
  • 6
  • 243
  • 355
azerbajdzan
  • 15,863
  • 1
  • 16
  • 48

1 Answers1

8

You can use the ResourceFunction "GraphicsInformation" for this purpose:

SeedRandom[1];
g = Graphics[{
    Table[
        Circle[RandomReal[{-10,10},2],RandomReal[{0,10},2],RandomReal[{0,2 π},2]],
        {n, 100}
    ]

enter image description here

Using the "GraphicsInformation" resource function:

pr = Lookup[ResourceFunction["GraphicsInformation"][g], "PlotRange"]

{{-17.8295, 17.0196}, {-19.7501, 19.9222}}

Check:

Show[
    g, 
    Epilog -> {
        EdgeForm[Red],
        FaceForm[None],
        Rectangle[{pr[[1,1]], pr[[2,1]]}, {pr[[1,2]], pr[[2,2]]}]
    }
]

enter image description here

"GraphicsInformation" also returns other pieces of useful information:

Keys @ ResourceFunction["GraphicsInformation"][g]

{"ImagePadding", "ImageSize", "PlotRangeSize", "ImagePaddingSize",
"PlotRange", "BasePlotRange"}

Carl Woll
  • 130,679
  • 6
  • 243
  • 355