填充规则和边框图案

填充规则和边框图案

根据提供的解决方案TikZ:如何在 tikz 路径的边界上绘制图案我能够完成论文中的所有图形,除了一个形状。正如你在图中看到的

enter image description here

该解决方案适用于正方形,但对于右上角显示的特定形状,重叠会创建第二个白色区域,该区域应被填充。并nonzero rule不能解决问题,而且除了手动计算白色区域外,我不知道如何正确绘制这个形状。

妇女权利委员会:

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{patterns}
\usetikzlibrary{decorations,backgrounds}


% defining the new dimensions and parameters
\newlength{\hatchspread}
\newlength{\hatchthickness}
\newlength{\hatchshift}
\newcommand{\hatchcolor}{}
% declaring the keys in tikz
\tikzset{hatchspread/.code={\setlength{\hatchspread}{#1}},
         hatchthickness/.code={\setlength{\hatchthickness}{#1}},
         hatchshift/.code={\setlength{\hatchshift}{#1}},% must be >= 0
         hatchcolor/.code={\renewcommand{\hatchcolor}{#1}}}
% setting the default values
\tikzset{hatchspread=1.5pt,
         hatchthickness=0.5pt,
         hatchshift=0pt,% must be >= 0
         hatchcolor=black}
% declaring the pattern
\pgfdeclarepatternformonly[\hatchspread,\hatchthickness,\hatchshift,\hatchcolor]% variables
   {custom north west lines}% name
   {\pgfqpoint{\dimexpr-2\hatchthickness}{\dimexpr-2\hatchthickness}}% lower left corner
   {\pgfqpoint{\dimexpr\hatchspread+2\hatchthickness}{\dimexpr\hatchspread+2\hatchthickness}}% upper right corner
   {\pgfqpoint{\dimexpr\hatchspread}{\dimexpr\hatchspread}}% tile size
   {% shape description
    \pgfsetlinewidth{\hatchthickness}
    \pgfpathmoveto{\pgfqpoint{0pt}{\dimexpr\hatchspread+\hatchshift}}
    \pgfpathlineto{\pgfqpoint{\dimexpr\hatchspread+0.15pt+\hatchshift}{-0.15pt}}
    \ifdim \hatchshift > 0pt
      \pgfpathmoveto{\pgfqpoint{0pt}{\hatchshift}}
      \pgfpathlineto{\pgfqpoint{\dimexpr0.15pt+\hatchshift}{-0.15pt}}
    \fi
    \pgfsetstrokecolor{\hatchcolor}
%    \pgfsetdash{{1pt}{1pt}}{0pt}% dashing cannot work Dly in all situation this way
    \pgfusepath{stroke}
   }


\newcounter{tmp}
\makeatletter
\def\pgfdecoratedcontourdistance{0pt}
\pgfset{
  decoration/contour distance/.code=%
    \pgfmathsetlengthmacro\pgfdecoratedcontourdistance{#1}}
\pgfdeclaredecoration{contour lineto closed}{start}{%
  \state{start}[
    next state=draw,
    width=0pt,
    persistent precomputation=\let\pgf@decorate@firstsegmentangle\pgfdecoratedangle]{%
    \pgfextra{\xdef\myList{}\xdef\mySecondList{}}
    \pgfextra{\setcounter{tmp}{0}}
    \pgfpathmoveto{\pgfpointlineattime{.5}
      {\pgfqpoint{0pt}{\pgfdecoratedcontourdistance}}
      {\pgfqpoint{\pgfdecoratedinputsegmentlength}{\pgfdecoratedcontourdistance}}}%
  }%
  \state{draw}[next state=draw, width=\pgfdecoratedinputsegmentlength]{%
    \ifpgf@decorate@is@closepath@%
      \pgfmathsetmacro\pgfdecoratedangletonextinputsegment{%
        -\pgfdecoratedangle+\pgf@decorate@firstsegmentangle}%
    \fi
    \pgfmathsetlengthmacro\pgf@decoration@contour@shorten{%
      -\pgfdecoratedcontourdistance*cot(-\pgfdecoratedangletonextinputsegment/2+90)}%
    \pgfpathlineto
      {\pgfpoint{\pgfdecoratedinputsegmentlength+\pgf@decoration@contour@shorten}
      {\pgfdecoratedcontourdistance}}%
    \stepcounter{tmp}
    \pgfcoordinate{muemmel\thetmp}{\pgfpoint{\pgfdecoratedinputsegmentlength+\pgf@decoration@contour@shorten}
      {\pgfdecoratedcontourdistance}}
    \pgfcoordinate{feep\thetmp}{\pgfpoint{\pgfdecoratedinputsegmentlength}{0pt}}      
    \pgfextra{\xdef\myList{\myList (muemmel\thetmp) -- }%
        \xdef\mySecondList{\mySecondList (feep\thetmp) -- }}
    \ifpgf@decorate@is@closepath@%
      \pgfpathclose
      \pgfextra{\xdef\myList{\myList cycle}% 
      \xdef\mySecondList{\mySecondList cycle}}
    \fi
  }%
  \state{final}{}%
}
\makeatother



\tikzset{EDR/.style={
draw=none,
preaction={draw=red,line width=0.5pt},
preaction={decoration={contour lineto closed, contour distance=-2pt},
decorate,
},
postaction={
insert path={%
\pgfextra{%
\pgfinterruptpath
\path[pattern=custom north west lines,hatchspread=1.5pt,hatchthickness=0.25pt,hatchcolor=red,even odd rule] 
\mySecondList \myList
;
\endpgfinterruptpath}
}},
}}

\begin{document}

\begin{tikzpicture}[scale=0.2]
\draw[EDR](35,57) -- (37,57) -- (37,55) -- (35,55) -- cycle;
\draw[EDR](41.1298,62.3343) -- (42.8362,62.341) -- (41.8438,58.9344) -- (41.5201,58.9331) -- cycle;
%\draw[thin](41.1298,62.3343) -- (42.8362,62.341) -- (41.61147,58.13688) -- cycle;
\end{tikzpicture}

\end{document}

答案1

首先,很遗憾听到存在问题。sorry^2对于使用\pgfextra。这不是这里的问题,但它确实很糟糕。(尽管回答者知道这是不好的。不幸的是,我上面的代码已经促成了这种做法的传播。\pgfextra但是,可以用于导出定义,如 中\pgfextra{\xdef\myList{}\xdef\mySecondList{}},据我所知这并不危险。在 中绘制路径\pgfextra 危险的。)

现在回到真正的问题。这里有一个替代方案。两个缺点:

  1. 如果在边界后绘制任何东西,则需要使用范围。(恕我直言,这还不算太糟糕。)
  2. 此版本不适用于大于 180 度的角度。此处的角度是观察者在内部看到的角度。因此,矩形的角度为 4 倍 90 度(而不是 270 度)。(如果需要,覆盖更大的角度也非常简单。)

这是代码。

\documentclass[tikz,border=3.14mm]{standalone}
\usetikzlibrary{patterns,decorations.pathreplacing,calc}


% defining the new dimensions and parameters
\newlength{\hatchspread}
\newlength{\hatchthickness}
\newlength{\hatchshift}
\newcommand{\hatchcolor}{}
% declaring the keys in tikz
\tikzset{hatchspread/.code={\setlength{\hatchspread}{#1}},
         hatchthickness/.code={\setlength{\hatchthickness}{#1}},
         hatchshift/.code={\setlength{\hatchshift}{#1}},% must be >= 0
         hatchcolor/.code={\renewcommand{\hatchcolor}{#1}}}
% setting the default values
\tikzset{hatchspread=1.5pt,
         hatchthickness=0.5pt,
         hatchshift=0pt,% must be >= 0
         hatchcolor=black}
% declaring the pattern
\pgfdeclarepatternformonly[\hatchspread,\hatchthickness,\hatchshift,\hatchcolor]% variables
   {custom north west lines}% name
   {\pgfqpoint{\dimexpr-2\hatchthickness}{\dimexpr-2\hatchthickness}}% lower left corner
   {\pgfqpoint{\dimexpr\hatchspread+2\hatchthickness}{\dimexpr\hatchspread+2\hatchthickness}}% upper right corner
   {\pgfqpoint{\dimexpr\hatchspread}{\dimexpr\hatchspread}}% tile size
   {% shape description
    \pgfsetlinewidth{\hatchthickness}
    \pgfpathmoveto{\pgfqpoint{0pt}{\dimexpr\hatchspread+\hatchshift}}
    \pgfpathlineto{\pgfqpoint{\dimexpr\hatchspread+0.15pt+\hatchshift}{-0.15pt}}
    \ifdim \hatchshift > 0pt
      \pgfpathmoveto{\pgfqpoint{0pt}{\hatchshift}}
      \pgfpathlineto{\pgfqpoint{\dimexpr0.15pt+\hatchshift}{-0.15pt}}
    \fi
    \pgfsetstrokecolor{\hatchcolor}
    \pgfusepath{stroke}
   }

\pgfset{
  decoration/contour distance/.code=%
    \pgfmathsetlengthmacro\pgfdecoratedcontourdistance{#1}}

\begin{document}
\tikzset{EDR/.style={clip,
decoration={show path construction,
lineto code={
\path[pattern=custom north west lines,hatchspread=1.5pt,hatchthickness=0.25pt,hatchcolor=red] 
(\tikzinputsegmentfirst) 
--
($(\tikzinputsegmentfirst)!\pgfdecoratedcontourdistance!90:(\tikzinputsegmentlast)$)
--
($(\tikzinputsegmentlast)!\pgfdecoratedcontourdistance!-90:(\tikzinputsegmentfirst)$)
-- (\tikzinputsegmentlast);
},closepath code={
\path[pattern=custom north west lines,hatchspread=1.5pt,hatchthickness=0.25pt,hatchcolor=red] 
(\tikzinputsegmentfirst) 
--
($(\tikzinputsegmentfirst)!\pgfdecoratedcontourdistance!90:(\tikzinputsegmentlast)$)
--
($(\tikzinputsegmentlast)!\pgfdecoratedcontourdistance!-90:(\tikzinputsegmentfirst)$)
-- (\tikzinputsegmentlast);
}},preaction={draw=red,line width=0.5pt},postaction=decorate}}
\begin{tikzpicture}[scale=0.2]
\pgfset{decoration/contour distance=-2pt}
\begin{scope}
\path[EDR]
(35,57) -- (37,57) -- (37,55) -- (35,55) -- cycle;
\end{scope}
\path[EDR]
(41.1298,62.3343) -- (42.8362,62.341) -- (41.8438,58.9344) -- (41.5201,58.9331) -- cycle;
%\draw[thin](41.1298,62.3343) -- (42.8362,62.341) -- (41.61147,58.13688) -- cycle;
\end{tikzpicture}
\end{document}

enter image description here

相关内容