8

How to draw, for example, four rectangles on the circulant shapes which hold down another separately? See my figure. I can't draw the purple rectangle which is behind the red rectangle on the above left corner. If you know a method, tell me please.

And I want to shade the shape in ball color, so I can't just draw a little rectangle on above left corner then set it color purple.

Jake
  • 232,450

3 Answers3

14

With help of Paul Gaborit's invclip it's possible to write

\documentclass[tikz,border=2mm]{standalone}
\begin{document}

\tikzset{invclip/.style={clip,insert path={{[reset cm]
      (-\maxdimen,-\maxdimen) rectangle (\maxdimen,\maxdimen)
    }}}}

\begin{tikzpicture}

\fill[red] (-1,-1) rectangle (1,1);
\fill[yellow, shift={(1.5,1.5)}] (-1,-1) rectangle (1,1);
\fill[blue, shift={(3,0)}] (-1,-1) rectangle (1,1);
\begin{pgfinterruptboundingbox}
\path[invclip] (-1,-1)-|(1,1);
\end{pgfinterruptboundingbox}
\fill[purple, shift={(1.5,-1.5)}] (-1,-1) rectangle (1,1);
\end{tikzpicture}
\end{document}

enter image description here

Ignasi
  • 136,588
5

Done with MetaPost here, also through clipping and restoring the intersection part. To be run with LuaLaTeX.

I used the shading facilities provided by the Metafun format of MetaPost. I chose a linear shading, since it seems more appropriate for rectangles.

\documentclass{standalone}
\usepackage{luamplib}
    \mplibsetformat{metafun}
\begin{document}
\begin{mplibcode}
input mpcolornames;

vardef myshade(expr debut, fin, mycolor) =
    define_linear_shade(debut, fin, .1[white, mycolor], mycolor)
enddef;

def fillit_withshade(expr pat, mycolor) =
    fill pat withshade myshade(ulcorner pat, lrcorner pat, mycolor);
enddef;

beginfig(1);
    numeric a, b; a = 2.75cm; b = 3cm; 
    picture intersect; intersect = nullpicture;
    path rect[], fullrect, bb_intersect; fullrect = fullsquare xyscaled (a, b); 
    rect1 = fullrect xshifted .75a; rect2 = fullrect yshifted .75b;
    rect3 = fullrect xshifted -.75a; rect4 = fullrect yshifted -.75b;
    bb_intersect = buildcycle(rect4, subpath (3, 5) of rect1) 
        leftenlarged .5bp bottomenlarged .5bp;
        % extends the intersection to its lower-left boundary (.5bp is the default pen width)

    fillit_withshade(rect4, Purple);
    clip currentpicture to bb_intersect; addto intersect also currentpicture;
    fillit_withshade(rect4, Purple); fillit_withshade(rect3, red);
    fillit_withshade(rect2, yellow); fillit_withshade(rect1, blue);
    draw intersect;
endfig;
\end{mplibcode}
\end{document}

enter image description here

Franck Pastor
  • 18,756
4

You can simply draw part of the rectangle again by using clip.

\documentclass[tikz]{standalone}
\begin{document}
\begin{tikzpicture}
\shade[ball color=red] (-1,-1) rectangle (1,1);
\shade[ball color=yellow] (0.5,0.5) rectangle (2.5,2.5);
\shade[ball color=blue] (2,-1) rectangle (4,1);
\shade[ball color=purple] (0.5,-2.5) rectangle (2.5,-0.5);
\clip (-1,-1) rectangle (1,0);  %clip the the top half away
\shade[ball color=red] (-1,-1) rectangle (1,1); %and draw the red rectangle again
\end{tikzpicture}
\end{document}

shaded rectangles

  • +1, though I would suggest some different shadings than "ball", which doesn't look nice applied to squares. – Claudio Fiandrino Mar 13 '15 at 14:08
  • @Claudio Fiandrino: I agree, but it is my understanding that that is what OP wants. – hpekristiansen Mar 13 '15 at 14:10
  • I understand, but no one prevents you to present a better alternative as second example. Remember that answers are not just meant to solve OP's problem, but are especially useful for future visitors, which may appreciate a better looking. – Claudio Fiandrino Mar 13 '15 at 14:47