如何反射和填充多边形

如何反射和填充多边形

我想反射黑色填充的多边形。使用此解决方案,我无法填充对称多边形。您有没有什么技巧可以做到这一点?

\documentclass{report}
\usepackage{tikz}
\usetikzlibrary{calc}

\tikzset{reflect at/.style args={#1--#2}{to path={%
            ($2*($(#1)!(\tikztostart)!(#2)$)-(\tikztostart)$)
            -- ($2*($(#1)!(\tikztotarget)!(#2)$)-(\tikztotarget)$)
}}}

\begin{document}
\begin{tikzpicture}[scale=1]
\draw [very thin, gray] (2,1) grid (12,9);

\node (X) at (7,2) {};
\node (Y) at (7,8) {};

\draw[color=red,very thick](X.center)--(Y.center);

\begin{scope}
\foreach \point [count=\i] in {{(4,2)},{(3,2)},{(3,4)},{(4,4)}} {
\node (A\i) at \point {};
}
\draw[fill=black] (A1.center)--(A2.center)--(A3.center)--(A4.center)--cycle;
\draw[thick,reflect at=X--Y] (A1) to (A2) (A2) to (A3) (A3) to (A4) (A4) to (A1);
\end{scope}

\begin{scope}
\foreach \point [count=\i] in {{(3,6)},{(4,6)},{(4,8)},{(3,8)}} {
    \node (A\i) at \point {};
}
\draw[fill=black] (A1.center)--(A2.center)--(A3.center)--(A4.center)--cycle;
\draw[thick,reflect at=X--Y] (A1) to (A2) (A2) to (A3) (A3) to (A4) (A4) to (A1);
\end{scope}

\begin{scope}
\foreach \point [count=\i] in {{(5,5)},{(5,7)},{(7,5)},{(7,3)}} {
    \node (A\i) at \point {};
}
\draw[fill=black] (A1.center)--(A2.center)--(A3.center)--(A4.center)--cycle;
\draw[thick,reflect at=X--Y] (A1) to (A2) (A2) to (A3) (A3) to (A4) (A4) to (A1);
\end{scope}

\begin{scope}
\foreach \point [count=\i] in {{(9,5)},{(9,7)},{(10,8)},{(10,6)}} {
    \node (A\i) at \point {};
}
\draw[fill=black] (A1.center)--(A2.center)--(A3.center)--(A4.center)--cycle;
\draw[thick,reflect at=X--Y] (A1) to (A2) (A2) to (A3) (A3) to (A4) (A4) to (A1);
\end{scope}

\begin{scope}
\foreach \point [count=\i] in {{(10,4)},{(10,6)},{(11,6)},{(11,4)}} {
    \node (A\i) at \point {};
}
\draw[fill=black] (A1.center)--(A2.center)--(A3.center)--(A4.center)--cycle;
\draw[thick,reflect at=X--Y] (A1) to (A2) (A2) to (A3) (A3) to (A4) (A4) to (A1);
\end{scope}
\end{tikzpicture}
\end{document}

谢谢

答案1

有各种各样的建议可以做到这一点。假设我们想让我们的手指/爪子远离transform canvas,最直接适用的答案之一似乎是Paul Gaborit 的解决方案

但是,我将这个问题视为挑战,以进行破解to path以获得所需的结果。为此,让我们首先回想一下为什么上面的代码不允许填充。您将轮廓分解为无法填充的断开的小段。如果不这样做,破解to path就不会起作用。那么我们能做什么?一种临时可能性是使用execute at end to。缺点是破解会多次绘制多边形,并且在早期版本中仅部分绘制(如果用户愿意提供角数,可以修复),好的一面是它将断开的路径集合变成具有良好线连接的多边形。填充样式等存储在中fancy reflect/.style

\documentclass{report}
\usepackage{tikz}
\usetikzlibrary{calc}
\newcounter{reflectedcoord}
\tikzset{fancy reflect at/.style args={#1--#2}{ini reflect,to path={%
            ($2*($(#1)!(\tikztostart)!(#2)$)-(\tikztostart)$)
            \pgfextra{\stepcounter{reflectedcoord}}%
            coordinate(reflectedcoord-\number\value{reflectedcoord})
            -- ($2*($(#1)!(\tikztotarget)!(#2)$)-(\tikztotarget)$)
},execute at end to={%\typeout{\number\value{reflectedcoord}}
\ifnum\value{reflectedcoord}>1
\path[fancy reflect,polygon through={reflectedcoord-1,reflectedcoord-...,reflectedcoord-\number\value{reflectedcoord}}];
\fi}},ini reflect/.code={\setcounter{reflectedcoord}{0}},
polygon through/.style={insert path={
    plot[samples at={#1}] (\x) -- cycle}},
    fancy reflect/.style={fill=red,draw,thick}}

\begin{document}
\begin{tikzpicture}[scale=1]
\draw [very thin, gray] (2,1) grid (12,9);

\node (X) at (7,2) {};
\node (Y) at (7,8) {};

\draw[color=red,very thick](X.center)--(Y.center);

\begin{scope}
\foreach \point [count=\i] in {{(4,2)},{(3,2)},{(3,4)},{(4,4)}} {
\node (A\i) at \point {};
}
\draw[fill=black] (A1.center)--(A2.center)--(A3.center)--(A4.center)--cycle;
\draw[fancy reflect at=X--Y] (A1) to (A2) (A2) to (A3) (A3) to (A4) (A4) to (A1);
\end{scope}

\begin{scope}
\foreach \point [count=\i] in {{(3,6)},{(4,6)},{(4,8)},{(3,8)}} {
    \node (A\i) at \point {};
}
\draw[fill=black] (A1.center)--(A2.center)--(A3.center)--(A4.center)--cycle;
\draw[fancy reflect at=X--Y] (A1) to (A2) (A2) to (A3) (A3) to (A4) (A4) to (A1);
\end{scope}

\begin{scope}
\foreach \point [count=\i] in {{(5,5)},{(5,7)},{(7,5)},{(7,3)}} {
    \node (A\i) at \point {};
}
\draw[fill=black] (A1.center)--(A2.center)--(A3.center)--(A4.center)--cycle;
\draw[fancy reflect at=X--Y] (A1) to (A2) (A2) to (A3) (A3) to (A4) (A4) to (A1);
\end{scope}

\begin{scope}
\foreach \point [count=\i] in {{(9,5)},{(9,7)},{(10,8)},{(10,6)}} {
    \node (A\i) at \point {};
}
\draw[fill=black] (A1.center)--(A2.center)--(A3.center)--(A4.center)--cycle;
\draw[fancy reflect at=X--Y] (A1) to (A2) (A2) to (A3) (A3) to (A4) (A4) to (A1);
\end{scope}

\begin{scope}
\foreach \point [count=\i] in {{(10,4)},{(10,6)},{(11,6)},{(11,4)}} {
    \node (A\i) at \point {};
}
\draw[fill=black] (A1.center)--(A2.center)--(A3.center)--(A4.center)--cycle;
\draw[fancy reflect at=X--Y] (A1) to (A2) (A2) to (A3) (A3) to (A4) (A4) to (A1);
\end{scope}
\end{tikzpicture}
\end{document}

在此处输入图片描述

可以看出,中间多边形的线连接引起了一些麻烦(例如line join=round,可以使用 关闭)。更简洁的解决方案是让用户提供角的数量,因为这也适用于非平凡的不透明度等。

\documentclass{report}
\usepackage{tikz}
\usetikzlibrary{calc}
\newcounter{reflectedcoord}
\tikzset{fancy reflect at/.style args={#1--#2}{ini reflect,to path={%
            ($2*($(#1)!(\tikztostart)!(#2)$)-(\tikztostart)$)
            \pgfextra{\stepcounter{reflectedcoord}}%
            coordinate(reflectedcoord-\number\value{reflectedcoord})
            -- ($2*($(#1)!(\tikztotarget)!(#2)$)-(\tikztotarget)$)
},execute at end to={%\typeout{\number\value{reflectedcoord}}
\ifnum\value{reflectedcoord}=\pgfkeysvalueof{/tikz/n reflect}
\path[fancy reflect,polygon through={reflectedcoord-1,reflectedcoord-...,reflectedcoord-\number\value{reflectedcoord}}];
\fi}},ini reflect/.code={\setcounter{reflectedcoord}{0}},
polygon through/.style={insert path={
    plot[samples at={#1}] (\x) -- cycle}},
    fancy reflect/.style={fill=red,draw,thick},
    n reflect/.initial=4}

\begin{document}
\begin{tikzpicture}[scale=1]
\draw [very thin, gray] (2,1) grid (12,9);

\node (X) at (7,2) {};
\node (Y) at (7,8) {};

\draw[color=red,very thick](X.center)--(Y.center);

\begin{scope}
\foreach \point [count=\i] in {{(4,2)},{(3,2)},{(3,4)},{(4,4)}} {
\node (A\i) at \point {};
}
\draw[fill=black] (A1.center)--(A2.center)--(A3.center)--(A4.center)--cycle;
\draw[fancy reflect at=X--Y] (A1) to (A2) (A2) to (A3) (A3) to (A4) (A4) to (A1);
\end{scope}

\begin{scope}
\foreach \point [count=\i] in {{(3,6)},{(4,6)},{(4,8)},{(3,8)}} {
    \node (A\i) at \point {};
}
\draw[fill=black] (A1.center)--(A2.center)--(A3.center)--(A4.center)--cycle;
\path[fancy reflect at=X--Y] (A1) to (A2) (A2) to (A3) (A3) to (A4) (A4) to (A1);
\end{scope}

\begin{scope}
\foreach \point [count=\i] in {{(5,5)},{(5,7)},{(7,5)},{(7,3)}} {
    \node (A\i) at \point {};
}
\draw[fill=black] (A1.center)--(A2.center)--(A3.center)--(A4.center)--cycle;
\path[fancy reflect at=X--Y] (A1) to (A2) (A2) to (A3) (A3) to (A4) (A4) to (A1);
\end{scope}

\begin{scope}
\foreach \point [count=\i] in {{(9,5)},{(9,7)},{(10,8)},{(10,6)}} {
    \node (A\i) at \point {};
}
\draw[fill=black] (A1.center)--(A2.center)--(A3.center)--(A4.center)--cycle;
\path[fancy reflect at=X--Y] (A1) to (A2) (A2) to (A3) (A3) to (A4) (A4) to (A1);
\end{scope}

\begin{scope}
\foreach \point [count=\i] in {{(10,4)},{(10,6)},{(11,6)},{(11,4)}} {
    \node (A\i) at \point {};
}
\draw[fill=black] (A1.center)--(A2.center)--(A3.center)--(A4.center)--cycle;
\path[fancy reflect at=X--Y] (A1) to (A2) (A2) to (A3) (A3) to (A4) (A4) to (A1);
\end{scope}
\end{tikzpicture}
\end{document}

在此处输入图片描述

相关内容