Skip to main content

graphics - Filling a rectangle with an image


Say I have some rectangles of different sizes drawn on the screen. How can I fill them all with the same image? Ideally, the image would be resized to completely fill each rectangle. I've been trying to do this by wrapping each rectangle in graphics and adding the image wrapped in an inset in the epilog for each one, something like this


Example:


Show[Graphics[{Rectangle[{0, 0}, {0.5, 0.3}]}, 

Epilog -> Inset[pic, {0, 0}, {0, 0}]],
Graphics[{Rectangle[{0.5, 0.3}, {0.7, 0.4}]},
Epilog -> Inset[pic, {0, 0}, {0, 0}]]]

but it's not giving me the result I want.


Is there anything like Fill[Rectangle[],pic] that would fill the rectangle with the image pic? I would also be fine with using something other than rectangles. Maybe there's something like a frame I can work with instead and just draw the image into those?



Answer



Code:


(*Sample data*)  
data = Table[

ColorData["Rainbow", t] /. RGBColor -> List, {t, 0, 1, 1/100}];

(*Visualize*)
Graphics[{Texture[data],
Polygon[{{0, 0}, {1, 0}, {1, 1}, {0, 1}},
VertexTextureCoordinates -> {{0}, {0}, {1}, {1}}]}]

Output:


example output


Note:



In the above picture data could be any picture you wish to use as a Texture[]. Please see references below to find more useful information.


Reference:


Texture
VertexTextureCoordinates


Comments