![TikZ:使用名称交叉点来定义节点](https://linux22.com/image/274374/TikZ%EF%BC%9A%E4%BD%BF%E7%94%A8%E5%90%8D%E7%A7%B0%E4%BA%A4%E5%8F%89%E7%82%B9%E6%9D%A5%E5%AE%9A%E4%B9%89%E8%8A%82%E7%82%B9.png)
两个问题:
- 这些从交叉点定义的节点出了什么问题?
- 能否使用
\foreach
语法自动找到第二点的其他向量?我没搞明白。
\documentclass[tikz]{standalone}
\usepackage{amsmath}
\usetikzlibrary{intersections}
\usetikzlibrary{calc}
% Used for both ellipses and circles
\tikzset{%
partial ellipse/.style args = {#1:#2:#3}{%
insert path = {+ (#1:#3) arc (#1:#2:#3)}
}
}
% unit vectors
\newcommand{\unit}[1]{\hat{\mathbf{#1}}}
\begin{document}
\begin{tikzpicture}
\coordinate (O) at (0, 0);
\draw[-latex] (O) -- (4, 0) node[pos = 1.1, font = \scriptsize] {\(x\)};
\draw[-latex] (O) -- (0, 4) node[pos = 1.1, font = \scriptsize] {\(y\)};
\draw[blue, name path = arc] (O) [partial ellipse = -15:75:3];
\foreach \i/\angle in {1/60, 2/30}{
\path[name path global = line\i] (O) -- (\angle:3.5);
\path[name intersections = {of = arc and line\i, by = P\i}];
\path[name path global = circ\i] (P\i) circle[radius = 1bp];
\filldraw[black] (P\i) circle[radius = .025cm];
}
\node[coordinate, name intersections = {of = circ1 and arc}] (A1) at
(intersection-1) {};
\node[coordinate, name intersections = {of = circ1 and arc}] (A2) at
(intersection-2) {};
\draw[name intersections = {of = circ1 and arc}, -latex] (P1) --
($(A2)!.75cm!(A1)$) node[pos = 1.125, font = \tiny] {\(\unit{u}\)} coordinate
(B1);
\draw[-latex] (P1) -- ($(P1)!.75cm!-270:(B1)$) node[pos = 1.125,
font = \tiny] {\(\unit{n}\)};
\end{tikzpicture}
\end{document}
从图中我们可以看到,第一个点绘制正确,只是存在相关错误。
答案1
对于现在遇到的错误,只需在 \foreach 循环中使用以下代码(pgf-manual p.496):
\path[name path global/.expanded = line\i] (O) -- (\angle:3.5);
\path[name intersections/.expanded = {of = arc and line\i, by = P\i}];
\path[name path global/.expanded = circ\i] (P\i) circle[radius = 1bp];
\foreach
这将允许您在循环外使用循环内定义的路径。
如果只是复制循环内的最后一个命令,\foreach
则会出现以下错误:
! Paragraph ended before \tikz@intersect@path@names@parse was complete.
这是因为of
键 (\tikz@intersect@path@names@parse) 需要周围的空格,and
如果您使用 ,则空格显然会被剥离of = circ\i and arc
,因此您需要使用of = {circ\i} and arc
。
\documentclass[tikz]{standalone}
\usepackage{amsmath}
\usetikzlibrary{intersections}
\usetikzlibrary{calc}
% Used for both ellipses and circles
\tikzset{%
partial ellipse/.style args = {#1:#2:#3}{%
insert path = {+ (#1:#3) arc (#1:#2:#3)}
}
}
% unit vectors
\newcommand{\unit}[1]{\hat{\mathbf{#1}}}
\begin{document}
\begin{tikzpicture}
\coordinate (O) at (0, 0);
\draw[-latex] (O) -- (4, 0) node[pos = 1.1, font = \scriptsize] {\(x\)};
\draw[-latex] (O) -- (0, 4) node[pos = 1.1, font = \scriptsize] {\(y\)};
\draw[blue, name path = arc] (O) [partial ellipse = -15:75:3];
\foreach \i/\angle in {1/60, 2/30 }{
\path[name path global= line\i] (O) -- (\angle:3.5);
\path[name intersections = {of = arc and line\i, by = P\i}];
\path[name path global = circ\i] (P\i) circle[radius = 1bp];
\filldraw[black] (P\i) circle[radius = .025cm];
\node[coordinate, name intersections = {of = {circ\i} and arc}] (A1) at (intersection-1) {};
\node[coordinate, name intersections = {of = {circ\i} and arc}] (A2) at (intersection-2) {};
\draw[name intersections/.expanded = {of = {circ\i} and arc}, -latex] (P\i) -- ($(A2)!.75cm!(A1)$) node[pos = 1.125, font = \tiny] {\(\unit{u}\)} coordinate (B1);
\draw[-latex] (P\i) -- ($(P\i)!.75cm!-270:(B1)$) node[pos = 1.125, font = \tiny] {\(\unit{n}\)};
}
\end{tikzpicture}
\end{document}