以下是我当前需要输入的内容:
\documentclass{standalone}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\coordinate (a) at (90:3cm);
\coordinate (b) at (210:3cm);
\coordinate (c) at (-30:3cm);
\node [above] at (a) {$a$};
\node [below left] at (b) {$b$};
\node [below right] at (c) {$c$};
\draw [thick,green, fill=green,opacity=0.5]
(barycentric cs:a=1,b=0,c=0) --
(barycentric cs:a=1,b=0,c=1) --
(barycentric cs:a=1,b=1,c=0) -- cycle;
\draw [ultra thick] (a) -- (b) -- (c) --cycle;
\end{tikzpicture}
\end{document}
这有点乏味。因为我想使用相同的基于 a、b、c 的重心坐标绘制许多形状。有没有办法将选项传递给scope
例如允许我将其(1,0,1)
评估为(barycentric cs:a=1,b=0,c=1)
?
答案1
如果您可以使用标准化重心坐标,即(x,y,z)
,x+y+z=1
那么您可以简单地设置x=(a),y=(b),z=(c)
。因此,(1,1,0)
您应该使用 来代替(.5,.5,0)
。
\documentclass{standalone}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\path
(90:3cm) coordinate (a) node[above] {$a$}
(210:3cm) coordinate (b) node[below left] {$b$}
(-30:3cm) coordinate (c) node[below right] {$c$};
\draw [thick,green, fill=green,opacity=0.5]
(barycentric cs:a=1,b=0,c=0) --
(barycentric cs:a=1,b=0,c=1) --
(barycentric cs:a=1,b=1,c=0) -- cycle;
\draw [ultra thick] (a) -- (b) -- (c) --cycle;
% set x=(a),y=(b),z=(c) and use normalized barycentric coordinates
\draw[ultra thick, red, dashed, x=(a),y=(b),z=(c)]
(1,0,0) -- (.5,0,.5) -- (.5,.5,0) --cycle;
\end{tikzpicture}
\end{document}
答案2
您还可以使用insert path
来缩写坐标。
\documentclass{standalone}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}[bcs/.style args={#1|#2|#3}{insert path={--(barycentric
cs:a=#1,b=#2,c=#3)}}]
\coordinate (a) at (90:3cm);
\coordinate (b) at (210:3cm);
\coordinate (c) at (-30:3cm);
\node [above] at (a) {$a$};
\node [below left] at (b) {$b$};
\node [below right] at (c) {$c$};
\draw [thick,green, fill=green,opacity=0.5] (a)
[bcs={1|0|1},bcs={1|1|0}] -- cycle;
\draw [ultra thick] (a) -- (b) -- (c) --cycle;
\end{tikzpicture}
\end{document}
你可以做的另一件事是局部改变 Ti钾Z 解析器. 那么整个路径实际上归结为
\begin{scope}[bary={a}{b}{c}]
\draw [thick,green, fill=green,opacity=0.5]
(1,0,0) -- (1,0,1) -- (1,1,0) -- cycle;
\end{scope}
在范围内安装重心坐标系的位置bary={a}{b}{c}
(我们不希望它无处不在)并且您实际上只需要指定三个数字。
\documentclass[tikz,border=3.14mm]{standalone}
\makeatletter % https://tex.stackexchange.com/a/365418/121799
\tikzset{bary/.code n args={3}{
\def\tikz@parse@splitxyz##1##2##3,##4,{%
\def\@next{\tikz@scan@one@point##1(barycentric cs:#1=##2,#2=##3,#3=##4)}%
}}}
\makeatother
\begin{document}
\begin{tikzpicture}
\coordinate (a) at (90:3cm);
\coordinate (b) at (210:3cm);
\coordinate (c) at (-30:3cm);
\node [above] at (a) {$a$};
\node [below left] at (b) {$b$};
\node [below right] at (c) {$c$};
\begin{scope}[bary={a}{b}{c}]
\draw [thick,green, fill=green,opacity=0.5]
(1,0,0) -- (1,0,1) -- (1,1,0) -- cycle;
\end{scope}
\draw [ultra thick] (a) -- (b) -- (c) --cycle;
\end{tikzpicture}
\end{document}
答案3
您可以定义
\newcommand{\foo}[3]{(barycentric cs:a=#1,b=#2,c=#3)}
然后使用它作为
\draw [thick,green, fill=green,opacity=0.5]
\foo{1}{0}{0} --
\foo{1}{0}{1} --
\foo{1}{1}{0} -- cycle;
或与另一个带有 9 个参数的命令组合
\newcommand{\faa}[9]{
\draw [thick,green, fill=green,opacity=0.5]
\foo{#1}{#2}{#3} --
\foo{#4}{#5}{#6} --
\foo{#7}{#8}{#9} -- cycle;
}
并使用
\faa{1}{0}{0}{1}{0}{1}{1}{1}{1}
平均能量损失
\documentclass{standalone}
\usepackage{tikz}
\newcommand{\foo}[3]{(barycentric cs:a=#1,b=#2,c=#3)}
\newcommand{\faa}[9]{
\draw [thick,green, fill=green,opacity=0.5]
\foo{#1}{#2}{#3} --
\foo{#4}{#5}{#6} --
\foo{#7}{#8}{#9} -- cycle;
}
\begin{document}
\begin{tikzpicture}
\coordinate (a) at (90:3cm);
\coordinate (b) at (210:3cm);
\coordinate (c) at (-30:3cm);
\node [above] at (a) {$a$};
\node [below left] at (b) {$b$};
\node [below right] at (c) {$c$};
\draw [thick,green, fill=green,opacity=0.5]
\foo{1}{0}{0} --
\foo{1}{0}{1} --
\foo{1}{1}{0} -- cycle;
\faa{1}{0}{0}{1}{0}{1}{1}{1}{1}
\draw [ultra thick] (a) -- (b) -- (c) --cycle;
\end{tikzpicture}
\end{document}