Skip to main content

plotting - Opacity and overlapping multiple polygons


Let's say I have 100 polygons that are all different but most of them overlap. All I want to do is to draw/plot them in one single graphic with linearly adding gray values; i.e., a point with n overlapping polygons(or other graphics) should have a grayvalue of n/100.


I wish


Graphics[{Opacity[1/10], Table[Disk[{i/10, 0}], {i, 10}]}]

would work, but Opacity doesn't add up linearly, and I don't find any easy way to do what I want.


My current idea is to go via Image[Graphics[...]], ImageData and ArrayPlot, but I might lose a lot of precision that way.



Answer




I'm not very familiar with but I do not think you could get both, high precission and high performance, at once.


This is straightforward approach. With Image processing functions avalible in Mathematica. Also, I'm not going to use Opacity, I do not know how does it work inside.


First, there is a function to to create an image, you can set resulution like you need.


f = ColorConvert[Image[Graphics[#, PlotRange -> 1], 
"Bit", ImageResolution -> 96, ImageSize -> 500],
"GrayScale"] &;

Lets create some triangles:


pics = f /@ (Polygon /@ Table[{{0, -1}, RandomReal[1, 2],RandomReal[{-1, 0}, 2]}, 
{100}]);


ImageMultiply[Fold[ImageAdd, First@pics, Rest@pics], 1/100]

enter image description here


Stealing examples with Disks (generating disk images last longer but not so much):


f = ColorConvert[Image[Graphics[#, PlotRange -> {{-1, 1}, {-.5, .5}}], "Bit", 
ImageResolution -> 96, ImageSize -> 500], "GrayScale"] &;

pics = f /@ Table[Disk[{-.5 + i/100, 0}, .5], {i, 100}];


ob = ImageMultiply[Fold[ImageAdd, First@pics, Rest@pics], 1/100]
ColorNegate@ob

enter image description here


enter image description here


As you can see, everything is linear:


ListPlot[ImageData[ColorNegate@ob][[ 130]]] 

enter image description here


Comments