Using the following code, I am able to generate a grayscale Voronoi diagram:
numpts = 12;
pts = RandomReal[{0, 1}, {numpts, 2}];
mesh = VoronoiMesh[pts, {{0, 1}, {0, 1}}]
mps = MeshPrimitives[mesh, 2];
vvv = {};
For[i = 1, i <= Length[mps], i++,
col = GrayLevel[ RandomReal[{0, 1}] ] ;
graphicGrain = Graphics[{col, Polygon[mps[[ i ]] [[1]] ]} ];
AppendTo[vvv, graphicGrain];
]
img = Show[vvv, ImageSize -> 1000, "ShrinkWrap" -> True];
img = ImageCrop[img, {950}]
Export["img_q.pdf", img]
As we can see from the image below, some of the grain boundaries seem to have a white edge going through them
Is there any way to generate a Voronoi diagram in such a way that these edges are not present? I have looked at similar posts regarding this 'white edge' problem but have not been successful at removing them myself.
Answer
Your issue appears to be the related to the Thickness
of the edges of the polygons. It shows up most when adjacent colours are dark. By increasing the thickness of the edges using Thickness
and setting the colour of the edges to the colour of the polygon using EdgeForm
you can make the edges overlap slightly and remove the white lines.
I would also use the Graphics
object for the export to get a better resolution in the PDF file.
I've made a slight refactoring of the code to remove the loop so it is more in Mma style.
numpts = 12;
pts = RandomReal[{0, 1}, {numpts, 2}];
mesh = VoronoiMesh[pts, {{0, 1}, {0, 1}}];
mps = MeshPrimitives[mesh, 2];
colourDirectives = {#, EdgeForm[#]} & /@ (GrayLevel[RandomReal[{0, 1}]] & /@
Range[First@Dimensions@mps]);
img = Graphics[
{Thickness[0.02]}~Append~Flatten@Riffle[colourDirectives, mps], ImageSize -> 400]
colourDirectives
gets a list of pairs of GrayLevel
and EdgeForm
of that colour for the polygons. These are Riffle
d with the polygons in mps
and Flatten
into a single list for Graphic
. That list has the Thickness
Append
ed to it so all edges are drawn that thickness.
Hope this helps.
Comments
Post a Comment