如何绘制如下图所示的四个正方形

如何绘制如下图所示的四个正方形

我知道如何单独绘制每个正方形,但我无法像图中所示的那样将它们放在一起。 在此处输入图片描述

答案1

确实不需要pics,但无论如何,基本思想是利用库name path中的密钥intersections,因此只需指定一次路径,然后进行一堆剪辑......

\documentclass[tikz,border=5]{standalone}
\usetikzlibrary{intersections}
\tikzset{use path/.code={
  \pgfextra{\csname tikz@intersect@path@name@#1\endcsname}%
}, 
square/.pic={code={
  \path  [pic actions, name path=square #1] 
    (-#1/2,-#1/2) rectangle ++(#1,#1);
}}}
\begin{document}
\begin{tikzpicture}[x=1em,y=1em]
\pic [fill=gray,  rotate=45]  at (0,0)   {square=11};
\pic [fill=black, rotate=-30] at (7,0)   {square=9};
\pic [fill=gray,  rotate=10]  at (14,-2) {square=7};
\pic [fill=black, rotate=30]  at (18,-5) {square=5};

\foreach \l/\r in {11/9, 9/7, 7/5}{
\begin{scope}
  \clip [use path=square \l]; \clip [use path=square \r];
  \fill [use path=square \l, white];
\end{scope}}
\foreach \s in {5, 7, 9, 11} \draw [use path=square \s];
\end{tikzpicture}
\end{document}

在此处输入图片描述

答案2

这是一个可能的解决方案,即绘制灰色矩形,然后绘制黑色矩形,并在内部重新绘制(通过裁剪path picture)前两个矩形为白色。

\documentclass[tikz,border=7mm]{standalone}
\usetikzlibrary{calc}
\begin{document}
  \begin{tikzpicture}
    % the two gray rectangles
    \node[draw, fill=gray, minimum size=4cm, rotate=45] (A) at (0,0) {};
    \node[draw, fill=gray, minimum size=2cm, rotate=20] (C) at (5,.5) {};
    % the two black rectangles and the white rectangles clipped inside
    \node[draw, fill=black, minimum size=3cm, rotate=-20,
      path picture={
        \node[draw, fill=white, minimum size=4cm, rotate=45] at (A) {};
        \node[draw, fill=white, minimum size=2cm, rotate=20] at (C) {};
      }] at (3,1){};
    \node[draw, fill=black, minimum size=1cm, rotate=30,
      path picture={
        \node[draw, fill=white, minimum size=2cm, rotate=20] at (C) {};
      }] (D) at (6.2,0) {};
  \end{tikzpicture}
\end{document}

在此处输入图片描述

笔记:下次请在您的问题中添加 MWE。

答案3

一种低复杂度的方法是使用两条路径来裁剪黑色矩形中的白色区域。在这里,我们只需使用参数构造四个矩形 (A)、(B)、(C)、(D):。[minimum size=#1,rotate=#2,fill=#3]然后,我们设置一条与以下相同的路径node(A)

\path[clip] (A.45)--(A.135)--(A.-135)--(A.-45)--cycle;

因为节点不能用于裁剪。这用于构造矩形(B)中的白色区域。接下来,我们设置一条路径,如下所示node(C)

\path[clip] (C.45)--(C.135)--(C.-135)--(C.-45)--cycle;

并用它来剪切矩形 (B) 和 (D) 中剩余的两个白色区域。

\documentclass[border=1pt,tikz]{standalone}
\begin{document}

\begin{tikzpicture}[blk/.style args={#1,#2,#3}{draw,minimum size=#1,rotate=#2,fill=#3}] 

\node(A)[blk={4cm,45,gray}]at(0,0){};
\node(B)[blk={3cm,70,black}]at(3,1){};
\node(C)[blk={2cm,20,gray}]at(5,.5){};
\node(D)[blk={1cm,30,black}]at(6.2,0){};

\begin{scope}
\path[clip] (A.45)--(A.135)--(A.-135)--(A.-45)--cycle;
\node[blk={3cm,70,white}]at(3,1){};
\end{scope}

\begin{scope}
\path[clip] (C.45)--(C.135)--(C.-135)--(C.-45)--cycle;
\node[blk={3cm,70,white}]at(3,1){};
\node[blk={1cm,30,white}]at(6.2,0){};
\end{scope}

\end{tikzpicture}

\end{document}

在此处输入图片描述

答案4

另一种方法是元帖子

prologues := 3;
outputtemplate := "%j%c.eps";

beginfig(1);

u := 1.6mm;
defaultfont := "phvr8r";
path A, B, C, D;

A = unitsquare scaled  5u rotated  30;
B = unitsquare scaled  7u rotated  20 shifted (-6u,1u);
C = unitsquare scaled  9u rotated  80 shifted (-7u,1u);
D = unitsquare scaled 11u rotated  50 shifted (-18u,-3u);


fill A withcolor .3 white; 
fill B withcolor .8 white;
fill C withcolor .3 white;
fill D withcolor .8 white;

unfill buildcycle(A,B);
unfill buildcycle(B,C);
unfill buildcycle(C,D);

draw A; draw B; draw C; draw D;

label.urt("5", point 1.5 of A);
label.bot("7", point 0.3 of B);
label.top("9", point 1.5 of C);
label.ulft("11", point 2.5 of D);

endfig;
end.

在此处输入图片描述

请注意,如果任何一个方块的起点(point 0 of ...)落在任何其他方块内,则buildcycle无法找到正确的重叠,原因已在我的回答中解释这个问题

相关内容