考虑下面的椭圆,它有两个标记点,红色和绿色,使用 TikZ 的交叉点库计算。
\documentclass[margin=5mm]{standalone}
\usepackage{tikz}
\usetikzlibrary{calc}
\usetikzlibrary {intersections}
\usetikzlibrary{calc}
\usetikzlibrary{math}
\usetikzlibrary{shapes}
\makeatletter
\newcommand{\gettikzxy}[3]{%
\tikz@scan@one@point\pgfutil@firstofone#1\relax
\edef#2{\the\pgf@x}%
\edef#3{\the\pgf@y}%
}
\makeatother
\begin{document}
\begin{tikzpicture}
\coordinate (CTRL) at (0,0) ;
\node[draw, black, thick, ellipse,
minimum height=3cm, minimum width=2cm,
name=ctrl,
name path=ctrl] at (CTRL) {Control};
\gettikzxy{(ctrl.south)}{\csx}{\csy}
\gettikzxy{(ctrl.north)}{\cnx}{\cny}
\coordinate (C1) at ($(\csx,\csy) + 1/7*(0,\cny-\csy)$) ;
\path[name path=lev1] ($(C1)+(-2,0)$) -- ($(C1) + (2,0)$);
\coordinate (C2) at ($(\csx,\csy) + 2/7*(0,\cny-\csy)$) ;
\path[name path=lev2] ($(C2)+(-2,0)$) -- ($(C2) + (2,0)$);
% how do I name this intersection as I1?
\fill [red,name intersections={of=ctrl and lev1}] (intersection-2) circle (2pt) node {};
% how do I name this intersection as I2?
\fill [green,name intersections={of=ctrl and lev2}] (intersection-2) circle (2pt) node {};
\end{tikzpicture}
\end{document}
我想将红点命名为 ,I1
将绿点命名为I2
,以便稍后在代码中绘制路径时使用它们。我该怎么做?
我尝试查看给出的交叉点的文档这里;使用by
选项几乎似乎回答了这个问题,但是当我尝试在选项中使用如下方法时\fill
\fill [red,name intersections={of=ctrl and lev1},by={a,b}] (b) circle (2pt) node {};
我收到错误
! Package pgfkeys Error: I do not know the key '/tikz/by', to which you passed
'a,I1', and I am going to ignore it. Perhaps you misspelled it.
See the pgfkeys package documentation for explanation.
Type H <return> for immediate help.
...
l.34 ...ntersections={of=ctrl and lev1},by={a,I1}]
(I1) circle (2pt) node {}...
答案1
编辑:
- 路径交叉点的命名在包文档中有描述
tikz
,第 13.3.2 节 任意路径的交点,第 145 页。 - 你的 MWE 编译没有任何错误或警告,但如果你想更改交叉点名称,你需要在命令中声明,例如像这样
\fill [name intersections={of=..., by=I1}, red] (I1) circle[radius=2pt];
C1
通过使用不同的坐标方式、C2
计算和定义交点,您的 MWE 可以变得更短(更简单):
\documentclass[margin=5mm]{standalone}
\usepackage{tikz}
\usetikzlibrary{calc,
intersections,
shapes}
\begin{document}
\begin{tikzpicture}[
E/.style = {ellipse, draw, thick,
minimum height=3cm, minimum width=2cm}
]
% elipse
\node[E, name path=C] (ctrl) {Control};
\coordinate (C1) at ($ (ctrl.south)!1/7!(ctrl.north) $);
\coordinate (C2) at ($ (ctrl.south)!2/7!(ctrl.north) $);
\path[name path=A] (C1) -- ++ (1,0); % changed
\path[name path=B] (C2) -- ++ (1,0); % changed
% intersections
\fill [name intersections={of=C and A, by=I1}, red] (I1) circle[radius=2pt)];
\fill [name intersections={of=C and B, by=I2}, teal] (I2) circle[radius=2pt)];
\end{tikzpicture}
\end{document}