How to texturize a Disk/Circle/Rectangle?
I noticed an example in the document of Texture
which used the alpha channel. So I think a disk-shape primitive may be simulated to a limited degree by mapping the image img
, which has been set to 100% transparent outside of the circle, onto a rectangle-shape Polygon
.
My code:
img = Rasterize[
DensityPlot[Sin[x] Sin[y],
{x, -4, 4}, {y, -3, 3},
ColorFunction -> "BlueGreenYellow",
Frame -> None, ImageSize -> 100, PlotRangePadding -> 0
]];
imgdim = ImageDimensions[img]
alphamask = Array[
If[
Norm[{#1, #2} - imgdim/2] < imgdim[[1]]/2,
1,0]&,
imgdim];
alphaimg = MapThread[Append, {img // ImageData, alphamask}, 2];
Graphics[{
Polygon[{{0, 0}, {1, 0}, {1, 1}, {0, 1}} + .3],
Texture[alphaimg],
Polygon[{{0, 0}, {1, 0}, {1, 1}, {0, 1}},
VertexTextureCoordinates -> {{0, 0}, {1, 0}, {1, 1}, {0, 1}}
],
Gray, Disk[{0, 0}, .5]
}]
which gives result like this:
Like RM, I've not been able to texture a Disk
primitive. We can create a textured disk using ParametricPlot
, however.
ParametricPlot[{r*Cos[t], r*Sin[t]}, {r, 0, 1}, {t, 0, 2 Pi},
Mesh -> False, BoundaryStyle -> None, Axes -> False,
PlotStyle -> {Opacity[1],
Texture[ExampleData[{"ColorTexture", "LightCherry"}]]}]
My fallback method for the moment is the following: approximate a circle with a polygon, fill the latter with the texture and finally conceal the angular edge with an overlaid Circle
. If the whole image is small, the number of nodes of the polygon can be further reduced. One annoying sideeffect is though that the Circle
is not antialiased...
img = Rasterize@
DensityPlot[Sin@x Sin@y, {x, -4, 4}, {y, -3, 3},
ColorFunction -> "BlueGreenYellow", Frame -> None,
ImageSize -> 200, PlotRangePadding -> 0];
coord = Block[{n = 100},
Table[{Cos[2 \[Pi] k/n], Sin[2 \[Pi] k/n]}, {k, 0, n - 1}]];
Graphics[{Texture@img, EdgeForm@None,
Polygon[coord, VertexTextureCoordinates -> (coord/2 + .5)], Black,
Thick, Circle[]}, ImageSize -> 200, Background -> [email protected]]