![如何在 tikz 中将直角三角形定义为节点](https://linux22.com/image/370841/%E5%A6%82%E4%BD%95%E5%9C%A8%20tikz%20%E4%B8%AD%E5%B0%86%E7%9B%B4%E8%A7%92%E4%B8%89%E8%A7%92%E5%BD%A2%E5%AE%9A%E4%B9%89%E4%B8%BA%E8%8A%82%E7%82%B9.png)
为了得到等边三角形我使用
\tikzstyle{triangle} = [fill=green!20, regular polygon, regular polygon sides=3]
但我正在努力定义直角三角形的 tikz 节点
我想做这样的事
\tikzstyle{anytriangle} = [draw, A--B--C--A]
进而
\begin{tikzpicture}[ auto, node distance=4em]
% Gridline
\draw [step=0.5cm,draw=gray] (-4,-4) grid (4,4);
\coordinate (A) at (0,0);
\coordinate (B) at (4,0);
\coordinate (C) at (0,4);
\node [anytriangle] {};
\end{tikzpicture}
但是我收到一个错误:未知坐标 A
答案1
也许是这样的(但我不得不承认我不完全明白你想要实现什么):
\documentclass[tikz]{standalone}
\begin{document}
\tikzset{triangle/.append style={fill=green!20, regular polygon, regular polygon sides=3},
anytriangle/.append style={draw, append after command={ \pgfextra{ \draw (A) -- (B) -- (C) -- (A); } }}}
\begin{tikzpicture}[auto, node distance=4em]
% Gridline
\draw [step=0.5cm,draw=gray] (-4,-4) grid (4,4);
\coordinate (A) at (0,0);
\coordinate (B) at (4,0);
\coordinate (C) at (0,4);
\node [anytriangle] {};
\end{tikzpicture}
\end{document}
答案2
您可以使用isosceles triangle
带有 的形状apex angle=90
。
\documentclass[tikz,border=2mm]{standalone}
\usetikzlibrary{positioning, shapes.geometric}
\begin{document}
\begin{tikzpicture}[
right angle triangle/.style={
isosceles triangle,
isosceles triangle apex angle=90},
every node/.style={right angle triangle,
draw, inner sep=0pt,
anchor=left corner,
shape border rotate=90},
paint/.style={draw=#1!75, fill=#1!20}
]
\draw[help lines] grid (5,2);
\foreach \a/\c in {1.5/blue, 1/green, 0.5/red}{
\node[paint=\c, minimum width=\a cm] at (0,0) {};
\node[paint=\c, minimum height=\a cm] at (2,0) {};
}
\begin{scope}[yshift=-3cm,
every node/.style={
right angle triangle,
isosceles triangle stretches=false,
draw, inner sep=0pt,
minimum height=1cm,
anchor=apex}
]
\draw[help lines] (0,0) grid (2,2);
\foreach \a/\c in {45/blue, 135/green,225/red,315/orange}
\node[paint=\c,
shape border rotate=\a] at (1,1) {};
\end{scope}
\end{tikzpicture}
\end{document}
答案3
从你的问题中我了解到你正在寻找这个:
我用大头针标记了坐标 A、B 和 C(您可以看到,那个三角形与它完全匹配。
对于上述图片我建议两种解决方案:
- 使用等腰三角形(类似于伊格纳西回答:
\documentclass[tikz, margin=3mm]{standalone}
\usetikzlibrary{shapes.geometric}
\begin{document}
\begin{tikzpicture}[
trian/.style = {isosceles triangle,
isosceles triangle apex angle=90,
minimum size=4cm/sqrt(2), inner sep=0pt,
anchor=east,rotate=-135,
draw, fill=green!20}
]
\coordinate[pin=below:A] (A) at (0,0);
\coordinate[pin=below:B] (B) at (4,0);
\coordinate[pin=C] (C) at (0,4);
%
\node [trian] {};
\end{tikzpicture}
\end{document}
- 带有小图片(
\pic
),它不是节点,但具有类似的特征(从可能的节点锚点中我仅将三个角定义为-a
,-b
和˙-c`):
\documentclass[tikz, margin=3mm]{standalone}
\begin{document}
\begin{tikzpicture}[
trian/.pic = {
\draw [fill=green!20]
(0,0) coordinate (-a) --
(4,0) coordinate (-b) --
(0,4) coordinate (-c) -- cycle;
}
]
\coordinate[pin=below:A] (A) at (0,0);
\coordinate[pin=below:B] (B) at (4,0);
\coordinate[pin=C] (C) at (0,4);
%
\pic[above right] at (A) {trian};
\end{tikzpicture}
\end{document}
两种情况下的结果都是一样的