Tikz:旋转填充图案

Tikz:旋转填充图案

对于黑白打印,我想旋转填充图案。该怎么做?

在此处输入图片描述

\documentclass[margin=2pt]{standalone}
\usepackage{tikz,siunitx}
\usetikzlibrary{%
    calc,
    intersections,
    patterns}

\begin{document}
\hfill\begin{tikzpicture}[%
    every node/.style={font=\small}]

\def\X{3.5}

\coordinate (A) at (90:\X) ;
\coordinate (B) at (210:\X) ;
\coordinate (C) at (-30:\X) ;

\coordinate (I) at ($(A)!.5!(B)$) ;
\coordinate (J) at ($(A)!.5!(C)$) ;
\coordinate (K) at ($(C)!.5!(B)$) ;
\coordinate (E) at ($(B)!.5!(K)$) ;
\coordinate (F) at ($(C)!.5!(K)$) ;

\coordinate (N) at ($(E)!(F)!(J)$) ;
\coordinate (M) at ($(E)!(I)!(J)$) ;

\coordinate (N') at ($(N)!2!(J)$) ;
\coordinate (F') at ($(F)!2!(J)$) ;
\coordinate (M') at ($(M)!2!(I)$) ;
\coordinate (E') at ($(E)!2!(I)$) ;

\begin{scope}
\clip (0,0) rectangle (0,0) ;

\draw[name path=P1] (N')--($(N')!3!(F')$) ;
\draw[name path=P2] (M')--($(M')!3!(E')$) ;

\path[name intersections={%
    of= P1 and P2,  % nom des paths
    by=D,           % nom des points
    sort by=P1,     % suivant le path
    total=\t}]      % nb de points
\pgfextra{\xdef\InterNb{\t} } ;

\end{scope}

\fill[pattern=checkerboard light gray] (M)--(I)--(A)--(J)--cycle ;

\path[pattern=horizontal lines light blue] (E)--(M)--(I)--(B)--cycle;
\path[pattern=horizontal lines light blue] (E')--(M')--(I)--(A)--cycle;

\path[pattern=horizontal lines light gray] (F)--(N)--(J)--(C)--cycle;
\path[pattern=horizontal lines light gray] (F')--(N')--(J)--(A)--cycle;

%\draw pic[%
%   "\SI{71}{\degree}",
%   draw,
%   fill=blue!15,
%   angle eccentricity=1.4,
%   angle radius=.7cm
%   ] {angle=B--I--M} ;

%\draw pic[%
%   "\SI{131}{\degree}",
%   draw,
%   fill=blue!15,
%   angle eccentricity=1.5,
%   angle radius=.6cm
%   ] {angle=C--F--N} ;

%\draw pic[%
%   "\SI{101}{\degree}",
%   draw,
%   fill=blue!15,
%   angle eccentricity=1.7,
%   angle radius=.6cm
%   ] {angle=A--J--E} ;


\draw (A)--(B)--(C)--cycle ;
\draw (J)--(E) ;
\draw (I)--(M) ;
\draw (F)--(N) ;

\draw (F')--(E') ;
\draw[thick] (M)--(N')--(D)--(M')--cycle ;

\foreach \Coor/\Text/\Pos in 
    {A/$A$/90,
    B/$B$/-135,
    C/$C$/-45,
    E/$E$/-90,
    F/$F$/-90,
    I/$I$/180,
    J/$J$/0,
    M/$M$/-80,
    N/$N$/100%
    } {%
%   \path (\Coor) pic {Cross={2 and black}} ;
    %\node[small dot] at (\Coor) {} ;
    \node[shift=(\Pos:8pt),anchor=center] at (\Coor) {\Text} ;
    }

\end{tikzpicture}\hfill\strut
\end{document}

答案1

正如 bmv 已经回答的那样,旋转图案是不可能的,但定义新的图案并不太难旋转模式。

有关它的信息可以在以下部分找到104 种模式并且可以在文件中找到已经定义的样式pgflibrarypatterns.code.tex

基于checkerboard light gray和,horizontal lines light gray我定义了rotated checkerboard light grayvertical lines light gray。代码如下:

\documentclass[margin=2pt]{standalone}
\usepackage{tikz,siunitx}
\usetikzlibrary{%
    calc,
    intersections,
    patterns}

\pgfdeclarepatterninherentlycolored{rotated checkerboard light gray}
{\pgfpointorigin}{\pgfqpoint{4mm}{4mm}}{\pgfqpoint{4mm}{4mm}}%
{
  \pgfsetfillcolor{black!10}
  \pgfpathrectangle{\pgfpointorigin}{\pgfqpoint{4.1mm}{4.1mm}}% make
                                % slightly larger to ensure that tiles
                                % are really solid
  \pgfusepath{fill}
  \pgfsetfillcolor{black!20}
  \pgfpathmoveto{\pgfqpoint{0mm}{2mm}}
  \pgfpathlineto{\pgfqpoint{2mm}{4mm}}
  \pgfpathlineto{\pgfqpoint{4mm}{2mm}}
  \pgfpathlineto{\pgfqpoint{2mm}{0mm}}
  \pgfpathlineto{\pgfqpoint{0mm}{2mm}}
  \pgfusepath{fill}
}

\pgfdeclarepatterninherentlycolored{vertical lines light gray}
{\pgfpointorigin}{\pgfpoint{4pt}{100pt}}
{\pgfpoint{4pt}{100pt}}
{
  \pgfsetfillcolor{black!10}
  \pgfpathrectangle{\pgfpointorigin}{\pgfpoint{2.5pt}{100pt}}
  \pgfusepath{fill}
  \pgfsetfillcolor{black!15}
  \pgfpathrectangle{\pgfpoint{2pt}{0pt}}{\pgfpoint{2.5pt}{100pt}}
  \pgfusepath{fill}
}

\begin{document}
\hfill\begin{tikzpicture}[%
    every node/.style={font=\small}]

\def\X{3.5}

\coordinate (A) at (90:\X) ;
\coordinate (B) at (210:\X) ;
\coordinate (C) at (-30:\X) ;

\coordinate (I) at ($(A)!.5!(B)$) ;
\coordinate (J) at ($(A)!.5!(C)$) ;
\coordinate (K) at ($(C)!.5!(B)$) ;
\coordinate (E) at ($(B)!.5!(K)$) ;
\coordinate (F) at ($(C)!.5!(K)$) ;

\coordinate (N) at ($(E)!(F)!(J)$) ;
\coordinate (M) at ($(E)!(I)!(J)$) ;

\coordinate (N') at ($(N)!2!(J)$) ;
\coordinate (F') at ($(F)!2!(J)$) ;
\coordinate (M') at ($(M)!2!(I)$) ;
\coordinate (E') at ($(E)!2!(I)$) ;

\begin{scope}
\clip (0,0) rectangle (0,0) ;

\draw[name path=P1] (N')--($(N')!3!(F')$) ;
\draw[name path=P2] (M')--($(M')!3!(E')$) ;

\path[name intersections={%
    of= P1 and P2,  % nom des paths
    by=D,           % nom des points
    sort by=P1,     % suivant le path
    total=\t}]      % nb de points
\pgfextra{\xdef\InterNb{\t} } ;

\end{scope}

\fill[pattern=rotated checkerboard light gray] (M)--(I)--(A)--(J)--cycle ;

\path[pattern=horizontal lines light blue] (E)--(M)--(I)--(B)--cycle;
\path[pattern=horizontal lines light blue] (E')--(M')--(I)--(A)--cycle;

\path[pattern=vertical lines light gray] (F)--(N)--(J)--(C)--cycle;
\path[pattern=horizontal lines light gray] (F')--(N')--(J)--(A)--cycle;

%\draw pic[%
%   "\SI{71}{\degree}",
%   draw,
%   fill=blue!15,
%   angle eccentricity=1.4,
%   angle radius=.7cm
%   ] {angle=B--I--M} ;

%\draw pic[%
%   "\SI{131}{\degree}",
%   draw,
%   fill=blue!15,
%   angle eccentricity=1.5,
%   angle radius=.6cm
%   ] {angle=C--F--N} ;

%\draw pic[%
%   "\SI{101}{\degree}",
%   draw,
%   fill=blue!15,
%   angle eccentricity=1.7,
%   angle radius=.6cm
%   ] {angle=A--J--E} ;


\draw (A)--(B)--(C)--cycle ;
\draw (J)--(E) ;
\draw (I)--(M) ;
\draw (F)--(N) ;

\draw (F')--(E') ;
\draw[thick] (M)--(N')--(D)--(M')--cycle ;

\foreach \Coor/\Text/\Pos in 
    {A/$A$/90,
    B/$B$/-135,
    C/$C$/-45,
    E/$E$/-90,
    F/$F$/-90,
    I/$I$/180,
    J/$J$/0,
    M/$M$/-80,
    N/$N$/100%
    } {%
%   \path (\Coor) pic {Cross={2 and black}} ;
    %\node[small dot] at (\Coor) {} ;
    \node[shift=(\Pos:8pt),anchor=center] at (\Coor) {\Text} ;
    }

\end{tikzpicture}\hfill\strut
\end{document}

在此处输入图片描述

更新

使用patterns.meta库可以旋转模式,也可以用 TiKZ 语法而不是仅仅用 pgf 语法来定义新模式。

以下代码显示了适用于库的先前答案patterns.meta。主要变化是使用参数模式和应用于的Lines示例。tikzdeclarepatternrotated checkboard

 \documentclass[margin=2pt]{standalone}
\usepackage{tikz,siunitx}
\usetikzlibrary{%
    calc,
    intersections,
    patterns.meta}

\tikzdeclarepattern{
    name = rotated checkboard,
    type=uncolored,
    parameters={\squaresize, \boardangle},
    bottom left={(-.1pt,-.1pt)},
    top right={(\squaresize+.1pt,\squaresize+.1pt)},
    tile size={(\squaresize,\squaresize)},
    tile transformation={rotate=\boardangle},
    defaults={
        tile size/.store in=\squaresize, tile size=10pt,
        rotate/.store in=\boardangle, rotate=0
    },
    code={
        \tikzset{x=1pt,y=1pt}
        \fill (0,0) rectangle (\squaresize/2,\squaresize/2);
        \fill (\squaresize/2,\squaresize/2) rectangle (\squaresize,\squaresize);
    }
}

\begin{document}
\hfill\begin{tikzpicture}[%
    every node/.style={font=\small}]

\def\X{3.5}

\coordinate (A) at (90:\X) ;
\coordinate (B) at (210:\X) ;
\coordinate (C) at (-30:\X) ;

\coordinate (I) at ($(A)!.5!(B)$) ;
\coordinate (J) at ($(A)!.5!(C)$) ;
\coordinate (K) at ($(C)!.5!(B)$) ;
\coordinate (E) at ($(B)!.5!(K)$) ;
\coordinate (F) at ($(C)!.5!(K)$) ;

\coordinate (N) at ($(E)!(F)!(J)$) ;
\coordinate (M) at ($(E)!(I)!(J)$) ;

\coordinate (N') at ($(N)!2!(J)$) ;
\coordinate (F') at ($(F)!2!(J)$) ;
\coordinate (M') at ($(M)!2!(I)$) ;
\coordinate (E') at ($(E)!2!(I)$) ;

\begin{scope}
\clip (0,0) rectangle (0,0) ;

\draw[name path=P1] (N')--($(N')!3!(F')$) ;
\draw[name path=P2] (M')--($(M')!3!(E')$) ;

\path[name intersections={%
    of= P1 and P2,  % nom des paths
    by=D,           % nom des points
    sort by=P1,     % suivant le path
    total=\t}]      % nb de points
\pgfextra{\xdef\InterNb{\t} } ;

\end{scope}

\fill[fill=orange!10, postaction={pattern={rotated checkboard[rotate=30, tile size=2mm]}, pattern color=orange}] (M)--(I)--(A)--(J)--cycle ;

\path[fill=cyan!20, postaction={pattern={Lines[angle=20, line width=2pt, distance=5pt]}, pattern color=cyan!70!black}] (E)--(M)--(I)--(B)--cycle;
\path[fill=red!30, postaction={pattern={Lines[angle=-35, line width=2pt, distance=6pt]}, pattern color=red!70!black}] (E')--(M')--(I)--(A)--cycle;

\path[fill=cyan!20, postaction={pattern={Lines[angle=90, line width=1pt, distance=3pt]}, pattern color=cyan!70!black}] (F)--(N)--(J)--(C)--cycle;
\path[fill=red!30, postaction={pattern={Lines[angle=85, line width=1pt, distance=3pt]}, pattern color=red!70!black}] (F')--(N')--(J)--(A)--cycle;

\draw (A)--(B)--(C)--cycle ;
\draw (J)--(E) ;
\draw (I)--(M) ;
\draw (F)--(N) ;

\draw (F')--(E') ;
\draw[thick] (M)--(N')--(D)--(M')--cycle ;

\foreach \Coor/\Text/\Pos in 
    {A/$A$/90,
    B/$B$/-135,
    C/$C$/-45,
    E/$E$/-90,
    F/$F$/-90,
    I/$I$/180,
    J/$J$/0,
    M/$M$/-80,
    N/$N$/100%
    } {%
%   \path (\Coor) pic {Cross={2 and black}} ;
    %\node[small dot] at (\Coor) {} ;
    \node[shift=(\Pos:8pt),anchor=center] at (\Coor) {\Text} ;
    }

\end{tikzpicture}
\end{document}

在此处输入图片描述

答案2

TikZ & PGF 3.0.1a 版手册第 171 页(感谢@Zarko)

模式不太灵活。特别是,如果不声明新模式,就无法更改模式的大小或方向对于复杂的情况,使用两个嵌套语句来模拟模式可能更容易 \foreach,但模式的渲染速度比模拟的快得多。

答案3

这个数字很有趣:杜德尼的铰链解剖。添加一些代码来更改patterns库的默认值,甚至是最近的patterns.meta库,代价是昂贵的,这会增加整个代码的复杂性。所以我会画一条直接的路。

我简化了 OP 的代码,并使用合适的\clip\foreach绘制棋盘(参见这里)。旋转角度JEC可以通过变量轻松计算\patrot出来asin(sqrt(3/7))

对于黑白图形,只需改变颜色。

在此处输入图片描述

\documentclass[tikz,margin=5mm]{standalone}
\usetikzlibrary{calc,patterns}
\begin{document}
\begin{tikzpicture}
\def\r{3.5}
\path 
(90:\r)  coordinate (A)
(210:\r) coordinate (B)
(-30:\r) coordinate (C)
(A)--(B) coordinate[midway] (I)
(A)--(C) coordinate[midway] (J)
(B)--(C) coordinate[pos=.25] (E) coordinate[pos=.75] (F)
($(E)!(F)!(J)$) coordinate (N)      
($(E)!(I)!(J)$) coordinate (M)
(M)--(I) coordinate[pos=2] (M')
(N)--(J) coordinate[pos=2] (N')
(E)--(I) coordinate[pos=2] (E')
(F)--(J) coordinate[pos=2] (F')
(intersection of N'--F' and M'--E') coordinate (D)
;

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Way 1: uniform color      
%\fill[violet!30] (M)--(I)--(A)--(J)--cycle ;

% Way 2: using `patterns` library
%\path[pattern=checkerboard light gray] (M)--(I)--(A)--(J)--cycle ;

% Way 3: using \clip and \foreach  
\pgfmathsetmacro{\patrot}{asin(sqrt(3/7))} % rotation angle JEC
\begin{scope}
\clip (M)--(I)--(A)--(J)--cycle;
\begin{scope}[scale=.2,transform canvas={shift={(M)},rotate=\patrot}]
\def\n{15}
\foreach \i in {0,...,\n}
\foreach \j in {0,...,\n}
{
    \pgfmathparse{mod(\i+\n-\j,2) ? "red" : "white"}
    \fill[\pgfmathresult] (\i,\j) rectangle +(1,1);
}
\end{scope}
\end{scope}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\fill[green!30] (E)--(M)--(I)--(B)--cycle (E')--(M')--(I)--(A)--cycle;
\fill[cyan!30] (F)--(N)--(J)--(C)--cycle (F')--(N')--(J)--(A)--cycle;
\fill[yellow!30] (E)--(N)--(F)--cycle (E')--(D)--(F')--cycle;
\draw (J)--(E) (I)--(M) (F)--(N) (F')--(E') ;
\draw[thick] (A)--(B)--(C)--cycle (M)--(N')--(D)--(M')--cycle ;
        
\foreach \Coor/\Pos in {A/90,B/-135,C/-45,D/90,E/-90,E'/135,F/-90,F'/30,I/180,J/0,M/-80,M'/180,N/0,N'/0}
\path (\Coor)+(\Pos:.3) node{$\Coor$};
\end{tikzpicture}
\end{document}

相关内容