我有下一个代码,它完全符合我的要求,即绘制三角形并显示每个内角的值。
\documentclass{standalone}
\usepackage{tikz}\usetikzlibrary{angles,quotes,calc}
\begin{document}
\begin{tikzpicture}
\coordinate (A1) at (0,0);
\coordinate (A2) at (2,5);
\coordinate (A3) at (4,-1);
\coordinate (A0) at (A3);
\coordinate (A4) at (A1);
\filldraw[fill=green]
let
\p{11}=($(A2)-(A1)$),
\p{12}=($(A0)-(A1)$),
\n1={atan2(\y{11},\x{11})-atan2(\y{12},\x{12})},
\p{21}=($(A3)-(A2)$),
\p{22}=($(A1)-(A2)$),
\n2={atan2(\y{21},\x{21})-atan2(\y{22},\x{22})},
\p{31}=($(A4)-(A3)$),
\p{32}=($(A2)-(A3)$),
\n3={atan2(\y{31},\x{31})-atan2(\y{32},\x{32})} in
(A1)--(A2)--(A3)--cycle
pic[draw,
"{$\pgfmathparse{\n1}%
\pgfmathprintnumber[fixed,precision=2]{\pgfmathresult}$}",
angle eccentricity=2.5]
{angle = A0--A1--A2}
pic[draw,
"{$\pgfmathparse{\n2}%
\pgfmathprintnumber[fixed,precision=2]{\pgfmathresult}$}",
angle eccentricity=2.5]
{angle = A1--A2--A3}
pic[draw,
"{$\pgfmathparse{\n3}%
\pgfmathprintnumber[fixed,precision=2]{\pgfmathresult}$}",
angle eccentricity=2.5]
{angle = A2--A3--A4};
\end{tikzpicture}
\end{document}
我想包含一个 foreach 循环(实际上是两个)以便创建一个宏(这将是下一步),但我无法做到。我向您展示代码:
\documentclass{standalone}
\usepackage{tikz}\usetikzlibrary{angles,quotes,calc}
\begin{document}
\begin{tikzpicture}
\coordinate (A1) at (0,0);
\coordinate (A2) at (2,5);
\coordinate (A3) at (4,-1);
\coordinate (A4) at (A1);
\coordinate (A0) at (A3);
\filldraw[fill=green]
foreach \k in {1,2,3}
let
\p{\k1}=($(A\the\numexpr\x-1\relax)-(A\k)$),
\p{\k2}=($(A\the\numexpr\x+1\relax)-(A\k)$),
\n\k={atan2(\y{\k1},\x{\k1})-atan2(\y{\k2},\x{\k2})}, in
(A1)--(A2)--(A3)--cycle
foreach \k in {1,2,3}
{pic[draw,
"{$\pgfmathparse{\n\k}%
\pgfmathprintnumber[fixed,precision=2]{\pgfmathresult}$}",
angle eccentricity=2.5]
{angle = A\the\numexpr\k-1\relax--A\k--A\the\numexpr\k+1\relax}};
\end{tikzpicture}
\end{document}
我不知道我的问题是否与语法有关,或者是否与超出我能力范围的更复杂的东西有关(或者两者兼而有之)。我很感激任何有助于更好地设计我的代码的评论。
答案1
问题出在let
需要分配并找到foreach
替代的语法上。
为了解决这个问题,我将 放在了循环let
内部foreach
。为了避免绘制和着色三角形 3 次,我只在循环外部执行了一次。
您的代码给出了负角度,我在这里修改了减法:
\n\k={atan2(\y{\k1},\x{\k1})-atan2(\y{\k2},\x{\k2})}
经过 :
\n\k={atan2(\y{\k2},\x{\k2})-atan2(\y{\k1},\x{\k1})}
\documentclass{standalone}
\usepackage{tikz}\usetikzlibrary{angles,quotes,calc}
\begin{document}
\begin{tikzpicture}
\coordinate (A1) at (0,0);
\coordinate (A2) at (2,5);
\coordinate (A3) at (4,-1);
\coordinate (A4) at (A1);
\coordinate (A0) at (A3);
\filldraw[fill=green](A1)--(A2)--(A3)--cycle;
\foreach \k in {1,2,3}{
\path%[fill=green]
let
\p{\k1}=($(A\the\numexpr\k-1)-(A\k)$),
\p{\k2}=($(A\the\numexpr\k+1)-(A\k)$),
\n\k={atan2(\y{\k2},\x{\k2})-atan2(\y{\k1},\x{\k1})}
in
% (A1)--(A2)--(A3)--cycle
{pic[draw,
"{$\pgfmathparse{\n\k}%
\pgfmathprintnumber[fixed,precision=2]{\pgfmathresult}$}",
angle eccentricity=2.5]
{angle = A\the\numexpr\k-1\relax--A\k--A\the\numexpr\k+1}};
}
\end{tikzpicture}
\end{document}
答案2
不太清楚您想要获得什么(您应该提供一个草图,说明您想要什么)。看看下面的图片是否显示了所需的结果:
使用angle
库和\pgfmathsetmacro
命令,它的代码很简单:
\documentclass{standalone}
\usepackage{tikz}\usetikzlibrary{angles,quotes,calc}
\begin{document}
\begin{tikzpicture}
\coordinate (A1) at (0,0);
\coordinate (A2) at (2,5);
\coordinate (A3) at (4,-1);
\coordinate (A4) at (A1);
\coordinate (A0) at (A3);
\draw (A1)--(A2)--(A3)--cycle;
\foreach \i in {1,2,3}
\pgfmathsetmacro{\j}{int(\i-1)}
\pgfmathsetmacro{\k}{int(\i+1)}
\path pic [draw,fill=red!30, radius=3mm]
{angle = A\j--A\i--A\k};
\end{tikzpicture}
\end{document}
除了使用之外,\pgfmathsetmacro{...}{...}
您还可以定义新的计数器:
\foreach \i [count=\j from 0, count =\k from 2]
in {1,2,3}
\path pic [draw,fill=red!30, angle radius=3mm]
{angle = A\j--A\i--A\k};
附录:现在,当期望的结果更加清晰时,您可以按照以下方式添加角度值(这是AndréC 回答,代码中的差异用 % <---) 表示:
\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{angles, arrows.meta, % <---
calc,
quotes}
\begin{document}
\begin{tikzpicture}[ > = {Straight Barb[angle=60:2pt 3]}, % <---
/pgf/number format/precision = 1 % <---
]
\coordinate (A0) at (0,0); % <---
\coordinate (A1) at (2,5);
\coordinate (A2) at (4,-1);
\draw[fill=green!30] (A0)--(A1)--(A2)--cycle;
%
\foreach \i in {0,1,2}
{
\pgfmathsetmacro{\j}{int(Mod(\i-1,3))} % <---
\pgfmathsetmacro{\k}{int(Mod(\i+1,3))} % <---
\path let \p1=($(A\j)-(A\i)$), % <---
\p2=($(A\k)-(A\i)$) in % <---
pic [draw, <->, % <---
angle radius=9mm, angle eccentricity=1.3,
font=\scriptsize, % <---
"{\pgfmathsetmacro{\ang}{atan2(\y2,\x2)-atan2(\y1,\x1)} % <---
\pgfmathprintnumber[fixed,precision=1]{\ang}}" % <---
]
{angle = A\j--A\i--A\k};
}
\end{tikzpicture}
\end{document}
答案3
这只是提到你foreach
在路径内部使用的方法,正如你所做的那样,实际上是可以说是更干净的版本(参见第节14.14 Foreach 操作pgfmanual v 3.1.4),并且无需定义辅助坐标A0
和A4
,它们只是A3 mod3
和的副本A4 mod 3
,因为 pgf 具有mod
(并且,就此而言,还有Mod
) 函数。因此,您只需定义一次点差并重复使用它们。它们都将具有一个方向,因此我们需要通过添加来翻转一个方向180
。为了“漂亮地打印”角度,可以再次使用Mod
(其中M
确保结果为非负值。所以一切都可以压缩为
\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{angles,quotes,calc}
\begin{document}
\begin{tikzpicture}
\coordinate (A1) at (0,0);
\coordinate (A2) at (2,5);
\coordinate (A3) at (4,-1);
\draw[fill=green] let
\p1=($(A3)-(A1)$),
\p2=($(A1)-(A2)$),
\p3=($(A2)-(A3)$) in
(A1)--(A2)--(A3)--cycle
foreach \k [evaluate=\k as \prevk using {int(1+Mod(\k+1,3))},
evaluate=\k as \nextk using {int(1+Mod(\k,3))}] in {1,2,3}
{pic[draw,
"{$\pgfmathparse{Mod(180-atan2(\y\k,\x\k)+atan2(\y\nextk,\x\nextk),360)}%
\pgfmathprintnumber[fixed,precision=2]{\pgfmathresult}$}",
angle eccentricity=2.5]
{angle = A\prevk--A\k--A\nextk}};
\end{tikzpicture}
\end{document}