tikz画阴影部分图,clip加foreach怎么实现

发布于 2021-10-01 15:46:49

在群里看见画阴影部分,有个大佬说用foreach+clip,翻了书,不会,来请教一下。代码如下

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{patterns}
\begin{document}
  \begin{tikzpicture}
    \draw[-latex] (0, 0) node[below left]{O}-- (5, 0);
    \draw[-latex] (0, 0) -- (0, 5);
    \draw (0, 0) -- (4, 0) -- (4, 4) -- (0, 4);
    \draw[clip] (1.5, 0) -- (4, 2.5) -- (4, 4) -- (2.5, 4) -- (0, 1.5) -- (0, 0) -- cycle;
  \end{tikzpicture}
  \begin{tikzpicture}
    \draw[-latex] (0, 0) node[below left]{O}-- (5, 0);
    \draw[-latex] (0, 0) -- (0, 5);
    \draw (0, 0) -- (4, 0) -- (4, 4) -- (0, 4);
    \path[draw, pattern=vertical lines] (1.5, 0) -- (4, 2.5) -- (4, 4) -- (2.5, 4) -- (0, 1.5) -- (0, 0) -- cycle;
  \end{tikzpicture}
\end{document}

查看更多

关注者
0
被浏览
1.5k
1 个回答
:
2021-10-03
这家伙很懒,什么也没写!

理解了clip的作用,就写出来了,应该是clip+foreach而不是foreach+clip

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{patterns}
\begin{document}
  \begin{tikzpicture}
    \draw[-latex] (0, 0) node[below left]{O}-- (5, 0);
    \draw[-latex] (0, 0) -- (0, 5);
    \draw (0, 0) -- (4, 0) -- (4, 4) -- (0, 4);
    \draw[clip] (1.5, 0) -- (4, 2.5) -- (4, 4) -- (2.5, 4) -- (0, 1.5) -- (0, 0) -- cycle;
    \foreach \x in {0, 0.1,...,5}{
      \draw (\x, 0) -- (\x, 5);
    }
  \end{tikzpicture}
  \begin{tikzpicture}
    \draw[-latex] (0, 0) node[below left]{O}-- (5, 0);
    \draw[-latex] (0, 0) -- (0, 5);
    \draw (0, 0) -- (4, 0) -- (4, 4) -- (0, 4);
    \path[draw, pattern=vertical lines] (1.5, 0) -- (4, 2.5) -- (4, 4) -- (2.5, 4) -- (0, 1.5) -- (0, 0) -- cycle;
  \end{tikzpicture}
\end{document}
%%% Local Variables:
%%% mode: latex
%%% TeX-master: t
%%% End:

结果如下
result2.png

撰写答案

请登录后再发布答案,点击登录

发布
问题

分享
好友

手机
浏览

扫码手机浏览