11
$\begingroup$

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,

$\endgroup$
  • $\begingroup$ 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? $\endgroup$ – ercegovac Oct 10 '17 at 10:24
  • $\begingroup$ seems better indeed, however I work with the .svg export which shows the same thing.. $\endgroup$ – Ruud3.1415 Oct 10 '17 at 10:25
  • $\begingroup$ So it is a bug? $\endgroup$ – Ruud3.1415 Oct 10 '17 at 10:29
  • 1
    $\begingroup$ 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. $\endgroup$ – ercegovac Oct 10 '17 at 10:33
  • $\begingroup$ 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). $\endgroup$ – Szabolcs Oct 10 '17 at 11:01
6
$\begingroup$

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

$\endgroup$
8
$\begingroup$

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

$\endgroup$
6
$\begingroup$

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.

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