绘制路径


绘制路径只是个一般的说法,一旦用路径元素构造好路径,你就可以在路径上做许多事。 你可以用线勾勒,用颜色填充,可以使用阴影,可以裁剪等

\draw
\fill
\filldraw
\pattern
\shade
\shadedraw
\clip
\useasboundingbox

你可以有两种方法绘制路径,一种是直接用相应的命令作用于路径,比如勾勒可以用命令 “\draw”。另一种是用命令“\path”构造出路径,通过其选项绘制。

指定颜色

绘制过程中需要改变颜色时可以用选项

color=颜色表达式
“颜色表达式”可以是预先定义的颜色名字,也可以是“xcolor”包中的颜色定义。 比如
\tikz \fill[color=red!20] (0,0) circle (1ex);

勾勒路径

除了用命令“\draw”,还可以用选项

draw=颜色表达式
来勾勒路径。比如
\begin{tikzpicture}
\path[draw=red] (0,0) -- (1,1) -- (2,1) circle (10pt);
\end{tikzpicture}

你可以用下列选项改变勾勒路径时的线宽

line width=长度
ultra thin
very thin
thin
semithick
thick
very thick
ultra thick
比如
\tikz \draw[ultra thick] (0,0) -- (1cm,1.5ex);

选项

line cap=类型
会影响线如何结束,也就是线冠的形状,有三个值“round”、“rect”、“butt”。 比如
\begin{tikzpicture}
\begin{scope}[line width=10pt]
\draw[line cap=rect] (0,0 ) -- (1,0);
\draw[line cap=butt] (0,.5) -- (1,.5);
\draw[line cap=round] (0,1 ) -- (1,1);
\end{scope}
\draw[white,line width=1pt]
(0,0 ) -- (1,0) (0,.5) -- (1,.5) (0,1 ) -- (1,1);
\end{tikzpicture}

选项

line join=类型
影响线的连接方式,有三个值“round”、“bevel”、“miter”。比如
\begin{tikzpicture}
\draw[line join=round] (0,0) -- ++(.5,1) -- ++(.5,-1);
\draw[line join=bevel] (1.25,0) -- ++(.5,1) -- ++(.5,-1);
\draw[line join=miter] (2.5,0) -- ++(.5,1) -- ++(.5,-1);
\useasboundingbox (0,1.5);
\end{tikzpicture}

如果以“miter”方式连接,连接处会是一个尖锐的角。选项

miter limit=线宽倍数
可以指定连接处突出的长度。比如
\begin{tikzpicture}
\draw (0,0) -- ++(5,.5) -- ++(-5,.5);
\draw[miter limit=25] (6,0) -- ++(5,.5) -- ++(-5,.5);
\useasboundingbox (14,0);
\end{tikzpicture}

你还可以用各种虚线来勾勒路径。选项

dash pattern=on 点长度 off 点划间距 on 划长度 off 间距
用来指定点划线模式。比如
\begin{tikzpicture}[dash pattern=on 2pt off 3pt on 4pt off 4pt]
\draw (0pt,0pt) -- (3.5cm,0pt);
\end{tikzpicture}
选项
dash phase=长度
可以指定点划线的平移量,比如
\begin{tikzpicture}[dash pattern=on 20pt off 10pt]
\draw[dash phase=0pt] (0pt,3pt) -- (3.5cm,3pt);
\draw[dash phase=10pt] (0pt,0pt) -- (3.5cm,0pt);
\end{tikzpicture}
系统预先定义好了一些点划线
solid
dotted
densely dotted
loosely dotted
dashed
densely dashed
loosely dashed
比如
\tikz \draw[densely dashed] (0pt,0pt) -- (50pt,0pt);

有时需要在路径上添加箭头,选项

arrows=起始端箭头-结束端箭头
用于指定路径两头的箭头,可以只指定一端,不过“-”要保留。 比如
\begin{tikzpicture}
\draw[->] (0,0) -- (1,0);
\draw[o-stealth] (0,0.3) -- (1,0.3);
\end{tikzpicture}
其中表示尾端箭头的“>”可以用选项
>=箭头
重新定义。箭头会凸出去,你可以用选项
shorten >=长度
削短它。起始端箭头可以用
shorten <=长度
削短。tikz库“arrows”定义了大量的箭头,要使用这些箭头,需要在LaTeX导言中 加上
\usetikzlibrary{arrows}
下面是其中一些箭头的名字
to
to reversed
implies
latex
latex reversed
latex'
latex' reversed
stealth
stealth reversed
stealth'
stealth' reversed
triangle 90
triangle 90 reversed
triangle 60
triangle 60 reversed
triangle 45
triangle 45 reversed
open triangle 90
open triangle 90 reversed
open triangle 60
open triangle 60 reversed
open triangle 45
open triangle 45 reversed
o
*
diamond
open diamond
triangle 90 cap
triangle 90 cap reversed
fast cap
fast cap reversed

有时我们需要用双线,而不是单线条勾勒路径,这可以用选项

double=双线间的颜色
如果省略“双线间的颜色”,就是背景色。你还可以用选项
double distance=双线间的距离
如果是粗线,可以用
double distance between line centers=两条线各自中心间的距离
比如
\begin{tikzpicture}
\draw[very thick,double] (0,0) arc (180:90:1cm);
\draw[very thick,double distance=2pt] (1,0) arc (180:90:1cm);
\end{tikzpicture}

填充路径

填充路径可以用命令“\fill”,也可以在选项中使用

draw=颜色表达式
“颜色表达式”的含义与勾勒部分是一样的。如果省略,用背景色。比如
\begin{tikzpicture}
\fill (0,0) -- (1,1) -- (2,1);
\fill[even odd rule] (6,0) circle (.5cm) (6.5,0) circle (.5cm);
\end{tikzpicture}

模式填充

除了单一的颜色填充,还可以用模式进行填充。所谓模式,就是一个包含简单图案的片段, 将它填充到路径围成的区域,一种办法是用命令“\pattern”,另一种办法是使用选项

pattern=模式的名字
你还可以用选项
pattern color=颜色表达式
改变填充时使用的颜色。比如
\begin{tikzpicture}
\draw[pattern color=red,pattern=fivepointed stars] (0,0) circle (1cm);
\draw[pattern color=blue,pattern=fivepointed stars] (0,0) rectangle (3,1);
\end{tikzpicture}

tikz的“patterns”库中包含了许多模式,为了使用它,请在LaTeX导言中加入

\usetikzlibrary{patterns}
下面是一些模式的名字
horizontal lines
vertical lines
north east lines
north west lines
grid
crosshatch
dots
crosshatch dots
fivepointed stars
sixpointed stars
bricks
checkerboard
checkerboard light gray
horizontal lines light gray
horizontal lines gray
horizontal lines dark gray
horizontal lines light blue
horizontal lines dark blue
crosshatch dots gray
crosshatch dots light steel blue

当路径围成的区域交叉时,那些区域算内部呢?指定计算的规则可以用选项

nonzero rule
even odd rule
比如
\begin{tikzpicture}
\filldraw[fill=examplefill,even odd rule] (0,0) rectangle (1,1) (0.5,0.5) circle (0.4cm);
\draw[->] (0.5,0.5) -- +(0,1) [above] node{crossings: $1+1 = 2$};
\end{tikzpicture}

用任意的图形图像填充,情况要复杂一些。你可以用选项

path picture={代码}
来定义用于填充的图形图像,它可以是tikz代码,如果是外部的图像,你可能 要用到一个预先定义的节点,它的名字是
path picture bounding box
比如
\begin{tikzpicture}[path image/.style={
path picture={\node at (path picture bounding box.center) {
\includegraphics[height=3cm]{#1}};}}]
\draw [help lines] (0,0) grid (3,2);
\draw [path image=brave-gnu-world-logo,draw=blue,thick] (0,1) circle (1);
\draw [path image=brave-gnu-world-logo,draw=red,very thick,->] (1,0) parabola[parabola height=2cm] (3,0);
\end{tikzpicture}

渐变

在路径围成的区域内的色彩渐变可以用命令“\shade”,也可以用选项

shade=名字
你还可以用选项
shade angle=角度
改变渐变角度。比如
\tikz \shadedraw [shading=axis,shading angle=90] (0,0) rectangle (1,1);

tikz的库“shadings”包含了许多渐变,为了使用它,你需要在LaTeX导言中加入

\usetikzlibrary{shadings}
名字“axis”定义了三条水平线间的渐变,你可以用选项
top color=颜色表达式
bottom color=颜色表达式
middle color=颜色表达式
left color=颜色表达式
right color=颜色表达式
改变颜色。比如
\tikz \draw[top color=white,bottom color=black,middle color=red] (0,0) rectangle (2,1);

名字“ball”定义球形渐变,你可以用选项

ball color=颜色表达式
改变球的颜色。比如
\begin{tikzpicture}
\shade[ball color=white] (0,0) circle (2ex);
\shade[ball color=red] (1,0) circle (2ex);
\shade[ball color=black] (2,0) circle (2ex);
\end{tikzpicture}

名字“bilinear interpolation”定义了双线性插值渐变,颜色可以通过选项

lower left=颜色表达式
upper left=颜色表达式
upper right=颜色表达式
lower right=颜色表达式
改变,比如
\tikz \shade[upper left=red,upper right=green,lower left=blue,lower right=yellow]
(0,0) rectangle (3,2);

名字“color wheel”定义了一个圆盘上的渐变

\tikz \shade[shading=color wheel] (0,0) circle (1.5);
如果想得到一个圆环,可以这样
\tikz \shade[shading=color wheel] [even odd rule] (0,0) circle (1.5) (0,0) circle (1);

名字“color wheel black center”定义了中心为黑色的圆盘渐变

\tikz \shade[shading=color wheel black center] (0,0) circle (1.5);

名字“color wheel white center”定义了中心为白色的圆盘渐变

\tikz \shade[shading=color wheel white center] (0,0) circle (1.5);

名字“Mandelbrot set”定义了一个缩放的Mandelbrot set

\tikz \shade[shading=Mandelbrot set] (0,0) rectangle (2,2);

名字“radial”定义了放射状的渐变,你可以用选项

inner color=颜色表达式
outer color=颜色表达式
改变渐变颜色。比如
\tikz \draw[inner color=red] (0,0) rectangle (2,1);

建立一个外接矩形

如果想画出图形的外接矩形,可以用命令“\useasboundingbox”,也可以用选项

use as bounding box
比如
\begin{tikzpicture}
\draw[use as bounding box] (2,0) rectangle (3,1);
\draw (1,0) -- (4,.75);
\end{tikzpicture}

裁剪

沿着路径裁剪可以用命令“\clip”,也可以用选项

clip
比如
\begin{tikzpicture}
\draw[clip] (0,0) circle (1cm);
\fill[red] (1,0) circle (1cm);
\end{tikzpicture}

多个作用于路径的动作

有时需要对路径做许多动作,选项

preactions={选项1,选项2,...}
中指定的选项发生在路径已经构造好,被使用之前。反之,选项
postaction={选项1,选项2,...}
指定的选项发生在路径已经使用。比如
\begin{tikzpicture}
\draw[help lines] (0,0) grid (3,2);
\draw [preaction={draw,line width=4mm,blue}] [line width=2mm,red] (0,0) rectangle (2,2);
\end{tikzpicture}


目录