为什么在 TikZ 代码中,偏移量设置为 0 cm 时,会产生两个相似的并排结果

为什么在 TikZ 代码中,偏移量设置为 0 cm 时,会产生两个相似的并排结果

在下面的代码中

\documentclass[10pt]{article}
\usepackage{tikz}
\usetikzlibrary{intersections}
\begin{document}
\begin{tikzpicture} 
\clip (-0.5,-0.75) rectangle (3.25,2.25); 
\foreach \pathname/\shift in {line/0cm, curve/2cm}{ \tikzset{xshift=\shift} 
\draw [->, name path=curve] (1,1.5) .. controls (-1,1) and (2,0.5) .. (0,0); 
\draw [->, name path=line](0,-.5) -- (1,2) ; \fill [name intersections={of=line and curve,sort by=\pathname, name=i}] [red, opacity=0.5, every node/.style={left=.25cm, black, opacity=1}] \foreach \s in {1,2,3} {(i-\s) circle (2pt) node {\footnotesize\s}}; 
} 
\end{tikzpicture}
\end{document}

这里假设\pathname line偏移量设置为 0cm,但为什么会显示这个结果呢?在此处输入图片描述

而且我也有点困惑这部分代码到底是什么意思

\foreach \pathname/\shift in {line/0cm, curve/2cm}{ \tikzset{xshift=\shift} 
\draw [->, name path=curve] (1,1.5) .. controls (-1,1) and (2,0.5) .. (0,0); 
\draw [->, name path=line](0,-.5) -- (1,2) ; \fill [name intersections={of=line and curve,sort by=\pathname, name=i}] [red, opacity=0.5, every node/.style={left=.25cm, black, opacity=1}] \foreach \s in {1,2,3} {(i-\s) circle (2pt) node {\footnotesize\s}}; 
} 

答案1

这是选项文档下的示例/tikz/intersection/sort by=<path name>这是PGF 手册中

没有任何\foreach,左侧可以绘制为

\begin{tikzpicture} 
  \draw[->, name path=curve] (1,1.5) .. controls (-1,1) and (2,0.5) .. (0,0); 
  \draw[->, name path=line] (0,-.5) -- (1,2); 
  \fill
    [name intersections={of=line and curve, sort by=line, name=i}]
    [red, opacity=0.5, every node/.style={left=.25cm, black, opacity=1}] 
    (i-1) circle (2pt) node {\footnotesize1} 
    (i-2) circle (2pt) node {\footnotesize2}
    (i-3) circle (2pt) node {\footnotesize3};
\end{tikzpicture}

使用内部\foreachcircle可以绘制三个

\fill[name intersections={..., name=i}] [...]
  \foreach \s in {1,2,3}
    { (i-\s) circle (2pt) node {\footnotesize\s} };

为了展示选项 的效果sort by,此示例需要展示sort by=line和如何sort by=curve影响交叉点的编号。这引出了右侧。为了将两个图形并排放置,使用了以下结构:

% left side
\tikzset{xshift=0cm} % useless
\draw[..., name path=curve] ...
\draw[..., name path=line ] ...
\fill[name intersections{..., sort by =line}] ...

% right side
\tikzset{xshift=2cm} % shift every following drawings to right by 2cm
\draw[..., name path=curve] ...
\draw[..., name path=line ] ...
\fill[name intersections{..., sort by =curve}] ...

左右两侧的相似性导致了外部的\foreach

答案2

foreach 循环的语法允许对多个变量进行迭代。这里,循环对名为\pathname和 的两个变量进行迭代\shift。后一个变量定义了通过 向右的水平移动\tikzset{xshift=\shift}

循环中选择的名称{line/0cm, curve/2cm}实际上与路径名无关 name path=curvename path=line如下面的示例所示,我修改了这些名称:{lin/0cm, cur/1cm, lol/2cm}

截屏

\documentclass[10pt]{article}
\usepackage{tikz}
\usetikzlibrary{intersections}
\begin{document}
\begin{tikzpicture} 
%\clip (-0.5,-0.75) rectangle (3.25,2.25); 
\foreach \pathname/\shift in {bin/0cm, cur/1cm, lol/2cm, gag/3cm}{ \tikzset{xshift=\shift} 
\draw [->, name path=curve] (1,1.5) .. controls (-1,1) and (2,0.5) .. (0,0); 
\draw [->, name path=line](0,-.5) -- (1,2) ; 
\fill [name intersections={of=line and curve,%sort by=\pathname,
 name=i}] [red, opacity=0.5, every node/.style={left=.25cm, black, opacity=1}] 
\foreach \s in {1,2,3} {(i-\s) circle (2pt) node {\footnotesize\s}}; 
} 
\end{tikzpicture}
\end{document}

相关内容