在PSTricks
中存在填充样式vlines
、hlines
,它们在库crosshatch
中有对应项。tikz patterns
然而,PSTricks
它们可以通过以下选项进行修改:
hatchwidth
,hatchwidthinc
,hatchsep
,hatchsepinc
和。hatchcolor
hatchangle
这回答针对这个问题自定义和内置 TikZ 填充图案声明一个带有变量的 pgfpattern:hatchthickness
,hatchspread
和hatchcolor
。
我失踪了hatchangle
。
如果有人更熟悉 pgf 基本层来调整这一点,我很高兴看到这个答案。
顺便说一句,我知道使用 tikz 中的形状旋转填充图案[重复]。但我认为用变量 hatchangle 声明自定义模式应该是可能的。
(pgf/tikz 版本 3.0,2013 年 12 月)
需要修改的相关代码
% defining the new dimensions and parameters
\newlength{\hatchspread}
\newlength{\hatchthickness}
\newlength{\hatchshift}
\newcommand{\hatchcolor}{}
\newcommand{\hatchangle}{} % NEW
% 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}},
hatchangle/.code={\renewcommand{\hatchangle}{#1}}}
% setting the default values
\tikzset{hatchspread=3pt,
hatchthickness=0.4pt,
hatchshift=0pt,% must be >= 0
hatchcolor=black,
hatchangle=0}
% declaring the pattern
\pgfdeclarepatternformonly[\hatchangle,\hatchspread,\hatchthickness,\hatchshift,\hatchcolor]% variables
{vlines}% 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 correctly in all situation this way
\pgfusepath{stroke}
}
答案1
从数学上来说,这就是如何实现具有各种角度的线条图案。
注意:请勿在角度闭合至 0°、90°、180° 等时尝试此操作!!
\documentclass[border=9,tikz]{standalone}
\usetikzlibrary{patterns}
\begin{document}
\newdimen\hatchspread
\newdimen\hatchthickness
\newdimen\hatchshift
\tikzset{
hatchspread/.code={\hatchspread=#1},
hatchthickness/.code={\hatchthickness=#1},
hatchshift/.code={\hatchshift=#1},
hatchcolor/.code={\def\hatchcolor{#1}},
hatchangle/.code={\pgfmathsetmacro\hatchangle{Mod(#1,180)}},
hatchspread=3pt,
hatchthickness=0.4pt,
hatchshift=0pt,
hatchcolor=black,
hatchangle=-30
}
\pgfdeclarepatternformonly[\hatchangle,\hatchspread,\hatchthickness,\hatchshift,\hatchcolor]
{lines}
{\pgfqpoint{\dimexpr-2\hatchthickness}{\dimexpr-2\hatchthickness}}
{\pgfqpoint{\dimexpr\patternwidth pt+2\hatchthickness}{\dimexpr\patternheight pt+2\hatchthickness}}
{\pgfpoint{\patternwidth}{\patternheight}} % \patternwidth and patternheight is calculated as follows
{
\pgfsetlinewidth{\hatchthickness}
\ifdim\hatchangle pt<90pt
\pgfmathsetmacro\patternwidth{\hatchspread*cosec(\hatchangle)}\xdef\patternwidth{\patternwidth}
\pgfmathsetmacro\patternheight{\hatchspread*sec(\hatchangle)}\xdef\patternheight{\patternheight}
\pgfpathmoveto{\pgfqpoint{0pt}{0pt}}
\pgfpathlineto{\pgfqpoint{\patternwidth pt}{\patternheight pt}}
\else
\pgfmathsetmacro\patternwidth{\hatchspread*cosec(\hatchangle)}\xdef\patternwidth{\patternwidth}
\pgfmathsetmacro\patternheight{-\hatchspread*sec(\hatchangle)}\xdef\patternheight{\patternheight}
\pgfpathmoveto{\pgfqpoint{\patternwidth pt}{0pt}}
\pgfpathlineto{\pgfqpoint{0pt}{\patternheight pt}}
\fi
\pgfsetstrokecolor{\hatchcolor}
\pgfusepath{stroke}
}
\foreach\i in{5,15,...,360}{
\tikz\filldraw[pattern=lines,hatchangle=\i](0,0)rectangle(1,1);
}
\end{document}
顺便说一句,我忽略了 shift 选项。它不是实现的核心部分。
答案2
patterns.meta
Mark Wibrow 的Tikz 库
提供模式Lines[angle=22.50]
和Hatch[angle=22.5]
\documentclass[tikz]{standalone}
\usetikzlibrary{patterns, patterns.meta}
\begin{document}
\begin{tikzpicture}
\draw[pattern={horizontal lines},pattern color=orange]
(0,0) rectangle +(1,1);
\draw[pattern={Lines[yshift=-10pt]},pattern color=blue]
(0,0) rectangle +(1,1);
\draw[pattern={vertical lines},pattern color=orange]
(1,0) rectangle +(1,1);
\draw[pattern={Lines[angle=90,yshift=-2pt]},pattern color=blue]
(1,0) rectangle +(1,1);
\draw[pattern={north east lines},pattern color=orange]
(0,1) rectangle +(1,1);
\draw[pattern={Lines[angle=45,distance={3pt/sqrt(2)}]},pattern color=blue]
(0,1) rectangle +(1,1);
\draw[pattern={north west lines},pattern color=orange]
(1,1) rectangle +(1,1);
\draw[pattern={Lines[angle=-22.5,distance={8pt/sqrt(2)}]},pattern color=blue]
(1,1) rectangle +(1,1);
\end{tikzpicture}
\end{document}
该库在 pgf/tikz 版本 3.1 中被标记为实验性的。