TikZでマスキング(指定した領域以外に編集効果が及ばないように)する方法を書く。
1. タイプセット環境
macOS Sierra,texlive2016,lualatex
2. コードと実行結果
- コード
-
% % preamble for lualatex % \RequirePackage{luatex85} \documentclass[border={15pt}]{standalone} \usepackage{tikz} \begin{document} \begin{tikzpicture} % % clipオプションをつけたpathで囲まれた部分に描画する。 % \path[clip, draw] (0,0) circle[radius=30mm]; % % 上のpathで囲まれた部分から,はみ出た部分が描画されない。 % \path[draw,fill,color=cyan] (-2.5,-2.5) rectangle (2.5,2.5); \end{tikzpicture} \end{document}
- 実行結果
3. 説明
- 3.1. preambleの説明
-
% % preamble for lualatex % \RequirePackage{luatex85} \documentclass[border={15pt}]{standalone} \usepackage{tikz}
特になし。
- 3.2. documentの説明
-
\begin{tikzpicture} % % clipオプションをつけたpathで囲まれた部分に描画する。 % \path[clip, draw] (0,0) circle[radius=30mm]; % % 上のpathで囲まれた部分から,はみ出た部分が描画されない。 % \path[draw,fill,color=cyan] (-2.5,-2.5) rectangle (2.5,2.5); \end{tikzpicture}
clipオプションをつけた path を作ると,その path で囲まれた領域内部だけが編集可能になる。
一度 clip の path をつくると,その後は領域外部が編集できなくなるが, scope 環境内で clip すれば,マスキングの有効範囲が scope 環境内だけに限定される。応用例を参照。
4. 応用例
- 斜線で塗りつぶし
-
\RequirePackage{luatex85} \documentclass[border=15pt]{standalone} \usepackage{tikz} \usepackage{newpxtext, newpxmath} \begin{document} \begin{tikzpicture} \path[draw] plot[domain=0:2*pi] ({\x},{sin(\x r)}); \path[draw,->,>=stealth] (0, -2) -- (0, 2) node[above]{$y$}; \path[draw,->,>=stealth] (-1, 0) -- (7.5, 0) node[below]{$x$}; \node at (pi,1) {$y=\sin x$}; \node[below left] at (0,0){$\mathrm{O}$}; \begin{scope} \path[clip] plot[domain=0:pi] ({\x}, {sin(\x r)}) -- cycle; \foreach \t in {1,2,...,20}{ \path[draw] (0.15*\t, 0) -- (0.15*\t + 0.4, 1); } \end{scope} \end{tikzpicture} \end{document}
- 水玉で塗りつぶし
-
\RequirePackage{luatex85} \documentclass[border=15pt]{standalone} \usepackage{tikz} \usetikzlibrary{math} \begin{document} \begin{tikzpicture} \begin{scope} \path[clip,draw] (0,0) circle[radius=3cm]; \foreach \t in {1,2,...,200} { \tikzmath{ real \r; \r1 = 10*rand; \r2 = 10*rnd; \r{x} = 3*rand; \r{y} = 3*rand; if \r2 < 3 then { let \s = cyan;} else { if \r2 < 7 then {let \s = magenta;} else { let \s = yellow; }; }; } \node[inner sep=0pt,circle,fill,color=\s,minimum size=\r1 mm] at (\r{x}, \r{y}) {}; } \end{scope} \end{tikzpicture} \end{document}
- 三角形を覗く
-
\RequirePackage{luatex85} \documentclass[border=15pt]{standalone} \usepackage{tikz} \usetikzlibrary{math} \begin{document} \begin{tikzpicture} \foreach \t in {0,1,2,...,10}{ \begin{scope} \path[draw,clip] (\t,0) circle[radius=2mm] ++ (0,1) circle[radius=2mm] ++ (0,1) circle[radius=2mm] ++ (0,1) circle[radius=2mm] ++ (0,1) circle[radius=2mm] ++ (0,1) circle[radius=2mm] ++ (0,1) circle[radius=2mm] ++ (0,1) circle[radius=2mm] ++ (0,1) circle[radius=2mm] ++ (0,1) circle[radius=2mm] ++ (0,1) circle[radius=2mm]; \tikzmath{ if \t < 4 then {let \color = cyan;} else { if \t < 7 then {let \color = magenta;} else { let \color = yellow; }; }; } \path[fill,color=\color] (0,0) -- (10,0) -- (5,10) -- cycle; \end{scope} } \end{tikzpicture} \end{document}
おしまい。