12

I am trying to make a figure in Mathematica using some graphic primitives:

rod[l_, r_] :=   GraphicsGroup[{Opacity[.1], {Disk[{-l/2 + r, 0}, 
 r, {\[Pi]/2 , 3 \[Pi]/2}]}, {Disk[{l/2 - r, 0}, 
 r, {-\[Pi]/2, \[Pi]/2}]},     Rectangle[{-l/2 + r, -r}, {l/2 - r, r}]}]

when I run:

Graphics[rod[3, .5]]

I get:

enter image description here

where there is a clear missallignment with the rectangle and hemicircles, what can I do to allign them in a proper way?

cheers,

Alexey Popkov
  • 61,809
  • 7
  • 149
  • 368
Ruud3.1415
  • 842
  • 4
  • 20
  • I think it is a problem of rendering. If I export it in jpg i get a good result. See here, is this what you expected? – ercegovac Oct 10 '17 at 10:24
  • seems better indeed, however I work with the .svg export which shows the same thing.. – Ruud3.1415 Oct 10 '17 at 10:25
  • So it is a bug? – Ruud3.1415 Oct 10 '17 at 10:29
  • 1
    I'm not sure at this point is it a bug. I've had my share of problems with Export function and few turned out to be a bug. However, you can try increasing the resolution of the output with Export["graphics.svg", Graphics[rod[3, .5]], ImageSize -> 2000]. This works on my machine. – ercegovac Oct 10 '17 at 10:33
  • Bug or not? It's definitely not "by design" as it is undesirable, but I suspect that this is difficult enough to fix that any fix is unlikely (even Adobe Reader suffers from it). – Szabolcs Oct 10 '17 at 11:01
  • @ercegovac But SVG is a vector format, so it should not affect rasterization and pixel-rounding, no? What you see when viewing the SVG depends on the SVG viewer you use. Changing the image size does clearly affect the rounding on my computer. Sometimes it disappears, sometimes it re-appears. It's the same with setting the notebook magnification or using Magnify. But it does not fix the problem for good. – Szabolcs Oct 10 '17 at 12:40
  • Something like this can make it less noticeable, but won't completely fix it: ImageResize[ Rasterize[ Graphics[rod[3, .5]], "Image", ImageResolution -> 20*72 ], Scaled[1/20] ]. – Szabolcs Oct 10 '17 at 12:40
  • @Szabolcs Well, I am not sure why but it does have effect. I used Illustrator to view the generated image. The objects are better alligned when I generated with increased image size. Though, the problem is not solved, just merely less severe. – ercegovac Oct 10 '17 at 12:54
  • @Szabolcs, but then we are moving to rasters again. I would prefer vector graphics with higher precision paths – Ruud3.1415 Oct 10 '17 at 12:54
  • @Szabolcs I just tested again, you are right, changing image size affects rounding. – ercegovac Oct 10 '17 at 12:58
  • @Ruud3.1415 What do you mean by "vector graphics with higher precision"? Mathematica's vector representation is exact. If you export to common formats like SVG, PDF, EPS, then some precision will be lost in principle (due to converting everything to floating point), but the precision will still be much higher than the resolution of your screen. What you see (the artefacts) has nothing to do with the representation of the graphics in vector formats. It is a property of the renderer that converts the vector representation to pixels. Each renderer will behave differently. – Szabolcs Oct 10 '17 at 12:59
  • If all you want is to produce gapless renders, you can consider using a different renderer. E.g. export to PDF and rasterize that with whatever viewer works best. If you want to produce vector graphics that look good on everyone's screen, then the best option is to make the shape a single polygon. Otherwise it will look bad on at least some people's computers. Just try to export a ContourPlot to PDF and see what it looks like. – Szabolcs Oct 10 '17 at 13:02
  • @Szabolcs, you're right it was the viewer I used that produced some problem – Ruud3.1415 Oct 10 '17 at 13:10

3 Answers3

8

As the only workaround seems to be to turn it to a Polygon, here is a way. The idea is to merge the graphics objects and to extract its boundary coordinates using existing tools:

Your rod can be simply defined as two disks + a rectangle:

rod = {Disk[{-l/2 + r, 0}, r], Disk[{l/2 - r, 0}, r], 
Rectangle[{-l/2 + r, -r}, {l/2 - r, r}]} /. {l -> 3, r -> .6};

Then this seems pretty smooth at any size / magnification :

Graphics[{Opacity[0.1],
Polygon@MeshCoordinates@BoundaryDiscretizeRegion[RegionUnion @@ rod, AccuracyGoal -> 3]}]

Blockquote

SquareOne
  • 7,575
  • 1
  • 15
  • 34
6

In practice, there isn't much you can do to stitch together multiple transparent graphics primitives with no artefacts. The better solution is to have a single object.

Looking at the coordinates, the alignment of your objects is clearly perfect. The problem is with how they are rendered on screen, not with how they are constructed. The misalignment happens due to rounding to integer pixel coordinates, and is a common problem with the display of vector graphics. Sadly, Mathematica is not very good at handling rounding in these cases, but it is by no means alone in this. I expect that if you export this figure to PDF, some PDF readers will show the artefacts and some will not. Preview.app on Mac does not show them while Adobe Reader shows very subtle lines. I also expect differences between platforms.

The best practical solution is to have a single Polygon object that approximates this shape.


One exception is when all your objects are Polygons contained within a single GraphicsComplex. These are stitchable without artefacts. On OS X, it is necessary to use the Graphics option Method -> {"TransparentPolygonMesh" -> True} to make them render correctly.

Szabolcs
  • 234,956
  • 30
  • 623
  • 1,263
6

In versions 10.2+, you can use StadiumShape to get a single primitive that gives the same shape as your rod:

rodF[l_, r_] := {Opacity[.1],StadiumShape[{{-l/2 + r,0}, {l/2 - r, 0}}, r]}

Graphics[rodF[3,.5]]

enter image description here

kglr
  • 394,356
  • 18
  • 477
  • 896