自定义形状填充颜色问题

2019-11-08 23:45发布

定义了一个画立方体的形状,填充颜色的时候出了点问题,虽然解决了但是并不知道原理,MWE如下 ```tex \documentclass[tikz]{standalone} %% Language a...

定义了一个画立方体的形状,填充颜色的时候出了点问题,虽然解决了但是并不知道原理,MWE如下 ```tex \documentclass[tikz]{standalone} %% Language and font encodings \usepackage[english]{babel} \usepackage[utf8x]{inputenc} \usepackage[T1]{fontenc} \usepackage{xcolor} \usetikzlibrary{positioning} % Define parallelepiped shape: \makeatletter \pgfkeys{/pgf/.cd, xoffset/.initial=2mm, yoffset/.initial=2mm } \pgfdeclareshape{parallelepiped} { \inheritsavedanchors[from=rectangle] % this is nearly a rectangle \inheritanchorborder[from=rectangle] \inheritanchor[from=rectangle]{north} \inheritanchor[from=rectangle]{north west} \inheritanchor[from=rectangle]{north east} \inheritanchor[from=rectangle]{center} \inheritanchor[from=rectangle]{west} \inheritanchor[from=rectangle]{east} \inheritanchor[from=rectangle]{south} \inheritanchor[from=rectangle]{south west} \inheritanchor[from=rectangle]{south east} \backgroundpath{ % store lower right in xa/ya and upper right in xb/yb \southwest \pgf@xa=\pgf@x \pgf@ya=\pgf@y \northeast \pgf@xb=\pgf@x \pgf@yb=\pgf@y \pgfmathsetlength\pgfutil@tempdima{\pgfkeysvalueof{/pgf/xoffset}} \pgfmathsetlength\pgfutil@tempdimb{\pgfkeysvalueof{/pgf/yoffset}} \def\ppd@offset{\pgfpoint{\pgfutil@tempdima}{\pgfutil@tempdimb}} % front rectangle \pgfpathmoveto{\pgfqpoint{\pgf@xa}{\pgf@ya}} \pgfpathlineto{\pgfqpoint{\pgf@xb}{\pgf@ya}} \pgfpathlineto{\pgfqpoint{\pgf@xb}{\pgf@yb}} \pgfpathlineto{\pgfqpoint{\pgf@xa}{\pgf@yb}} \pgfpathclose % other sides \pgfpathmoveto{\pgfqpoint{\pgf@xb}{\pgf@yb}} \pgfpathlineto{\pgfpointadd{\pgfpoint{\pgf@xb}{\pgf@yb}}{\ppd@offset}} \pgfpathlineto{\pgfpointadd{\pgfpoint{\pgf@xa}{\pgf@yb}}{\ppd@offset}} \pgfpathlineto{\pgfpointadd{\pgfpoint{\pgf@xa}{\pgf@ya}}{\ppd@offset}} \pgfpathlineto{\pgfqpoint{\pgf@xa}{\pgf@ya}} \pgfpathmoveto{\pgfqpoint{\pgf@xa}{\pgf@yb}} \pgfpathlineto{\pgfpointadd{\pgfpoint{\pgf@xa}{\pgf@yb}}{\ppd@offset}} } } \pgfdeclareshape{parallelepiped2} { \inheritsavedanchors[from=rectangle] % this is nearly a rectangle \inheritanchorborder[from=rectangle] \inheritanchor[from=rectangle]{north} \inheritanchor[from=rectangle]{north west} \inheritanchor[from=rectangle]{north east} \inheritanchor[from=rectangle]{center} \inheritanchor[from=rectangle]{west} \inheritanchor[from=rectangle]{east} \inheritanchor[from=rectangle]{south} \inheritanchor[from=rectangle]{south west} \inheritanchor[from=rectangle]{south east} \backgroundpath{ % store lower right in xa/ya and upper right in xb/yb \southwest \pgf@xa=\pgf@x \pgf@ya=\pgf@y \northeast \pgf@xb=\pgf@x \pgf@yb=\pgf@y \pgfmathsetlength\pgfutil@tempdima{\pgfkeysvalueof{/pgf/xoffset}} \pgfmathsetlength\pgfutil@tempdimb{\pgfkeysvalueof{/pgf/yoffset}} \def\ppd@offset{\pgfpoint{\pgfutil@tempdima}{\pgfutil@tempdimb}} \pgfpathmoveto{\pgfqpoint{\pgf@xa}{\pgf@ya}} \pgfpathlineto{\pgfqpoint{\pgf@xb}{\pgf@ya}} \pgfpathlineto{\pgfqpoint{\pgf@xb}{\pgf@yb}} \pgfpathlineto{\pgfqpoint{\pgf@xa}{\pgf@yb}} \pgfpathclose \pgfpathmoveto{\pgfqpoint{\pgf@xa}{\pgf@ya}} \pgfpathlineto{\pgfpointadd{\pgfpoint{\pgf@xa}{\pgf@ya}}{\ppd@offset}} \pgfpathlineto{\pgfpointadd{\pgfpoint{\pgf@xa}{\pgf@yb}}{\ppd@offset}} \pgfpathlineto{\pgfpointadd{\pgfpoint{\pgf@xb}{\pgf@yb}}{\ppd@offset}} \pgfpathlineto{\pgfqpoint{\pgf@xb}{\pgf@yb}} \pgfpathmoveto{\pgfqpoint{\pgf@xa}{\pgf@yb}} \pgfpathlineto{\pgfpointadd{\pgfpoint{\pgf@xa}{\pgf@yb}}{\ppd@offset}} } } \makeatother \tikzset{ conv/.style={ parallelepiped, draw, xoffset=-0.5cm, yoffset=0.5cm, path picture={ \draw[top color=red!50,bottom color=red!50] (path picture bounding box.south west) rectangle (path picture bounding box.north east); }, text=black, }, conv2/.style={ parallelepiped2, draw, xoffset=-0.5cm, yoffset=0.5cm, path picture={ \draw[top color=red!50,bottom color=red!50] (path picture bounding box.south west) rectangle (path picture bounding box.north east); }, text=black, }, } \begin{document} \begin{tikzpicture} \node[conv, minimum width=3cm, minimum height=1cm](conv1){Conv}; \node[conv2, right=1cm of conv1, minimum width=3cm, minimum height=1cm](conv1){Conv2}; \end{tikzpicture} \end{document} ``` 运行结果如下 ![](https://pics.latexstudio.net/data/images/201911/cbf79ccfcf86905.png)
2条回答
咸菜的味道
2019-11-09 23:59
如果试试 ```tex \tikz{ \filldraw [fill=red!50] (0,0)--(1,0)--(1,1)--(0,1)--cycle (0,0)--($(0,0)+(135:0.5)$)--($(0,1)+(135:0.5)$)--($(1,1)+(135:0.5)$)--(1,1) (0,1)--($(0,1)+(135:0.5)$); } ``` 就看到类似的问题,当填充路径时,非封闭的 ``(0,0)--($(0,0)+(135:0.5)$)--($(0,1)+(135:0.5)$)--($(1,1)+(135:0.5)$)--(1,1)`` 会被自动封闭,即首尾连接起来,按非零规则,白色区域不属于被填充区域。试试 ``` \tikz{ \filldraw [fill=red!50] (0,0)--(1,0)--(1,1)--(0,1)--cycle (0,0)--($(0,0)+(135:0.5)$)--($(0,1)+(135:0.5)$)--($(1,1)+(135:0.5)$)--(1,1)--(0,1)--cycle (0,1)--($(0,1)+(135:0.5)$); } ``` 或者 ``` \tikz{ \filldraw [fill=red!50] (0,0)--(0,1)--(1,1)--(1,0)--cycle (0,0)--($(0,0)+(135:0.5)$)--($(0,1)+(135:0.5)$)--($(1,1)+(135:0.5)$)--(1,1) (0,1)--($(0,1)+(135:0.5)$); } ``` 就没有这种问题。

一周热门 更多>