帮助使用 tikz 绘制三角形

帮助使用 tikz 绘制三角形

我正在尝试使用 TikZ 绘制一个三角形。我设法完成了它,但现在我遇到了让节点进入三角形主体的问题。我只使用 TikZ 几天,所以这个基本练习花费的时间比我愿意承认的要长得多。

我该如何将节点放入体内?

尽管这确实画出了一个三角形,但你认为这样做正确吗?还有更好的方法吗?

\begin{center}
    \begin{tikzpicture} 
        \draw[gray] ++(150:2.3) -- (0,0); %hypotenuse
        \draw[teal] ++(180:2) -- (0,0); %adjacent
        \draw[orange] (-2,1.15) -- (-2,0); %opposite

        \draw[thin] (-0.5,0.25) arc (150:180:0.5) 
            node[left] {\small $30^\circ$};
    \end{tikzpicture}
\end{center}

答案1

处理原代码:

\documentclass{article}
\usepackage{tikz}

\begin{document}

\begin{tikzpicture} 
\draw[gray] ++(150:2.3) -- (0,0); %hypotenuse
\draw[teal] ++(180:2) -- (0,0); %adjacent
\draw[orange] (-2,1.15) -- (-2,0); %opposite

\draw[thin] (-0.5,0.25) arc (150:180:0.5) 
  node[left] {\small $30^\circ$};
\end{tikzpicture}

\end{document}

并缩放生成的对象,揭示了另外两个问题:圆弧不接触斜边,并且斜边和较短的直角边不相交:

在此处输入图片描述

这两个问题基本上都是由于同一个原因造成的:手动计算坐标。

为了解决圆弧的问题,我建议您使用剪辑和附加节点来绘制圆;这样,您可以解决两个问题:以自动方式正确绘制圆弧(不需要像其他答案那样猜测角度),并且可以更好地控制标签位置:

\documentclass{article}
\usepackage{tikz}

\begin{document}

\begin{center}
\begin{tikzpicture}
\draw[gray] ++(150:2.3) -- (0,0); %hypotenuse
\draw[teal] ++(180:2) -- (0,0); %adjacent
\draw[orange] (-2,1.15) -- (-2,0); %opposite
\path[clip] (0,0) -- (-2,0) -- (-2,1.15) -- cycle;
\node[circle,draw,minimum size=40pt] at (0,0) (circ) {};
\node[font=\footnotesize,left] at (circ.160) {$30^\circ$};
\end{tikzpicture}
\end{center}

\end{document}

在此处输入图片描述

还有一个细节需要改进:斜边和对边不相交。这可以通过使用库intersections自动计算对边的延长线和斜边的交点来解决:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{intersections}

\begin{document}

\begin{center}
\begin{tikzpicture}
% place coordinates at the two initial vertices 
\coordinate (a) at (0,0);
\coordinate (b) at (-2,0);

% automatically calculate the third vertex
\path[name path=line 1] (-2,0) -- (-2,2);
\path[name path=line 2] (0,0) -- (150:2.4);
\path [name intersections={of=line 1 and line 2, by={c}}];

% draw the lines
\draw[gray] (a) -- (c); %hypotenuse
\draw[teal] (a) -- (b); %adjacent
\draw[orange] (b) -- (c); %opposite

% draw the arc clipping a circle against the triangle and place the label
\path[clip] (a) -- (b) -- (c) -- cycle;
\node[circle,draw,minimum size=40pt] at (0,0) (circ) {};
\node[font=\footnotesize,left] at (circ.160) {$30^\circ$};
\end{tikzpicture}
\end{center}

\end{document}

在此处输入图片描述

答案2

正如 Sigur 指出的那样,您可以使用above left进行放置。另外,我建议您使用scale选项来调整内容大小:

\documentclass{article}
\usepackage{tikz}

\begin{document}
\begin{tikzpicture}
    \begin{tikzpicture}
        \draw[gray] ++(150:2.3) -- (0,0); %hypotenuse
        \draw[teal] ++(180:2) -- (0,0); %adjacent
        \draw[orange] (-2,1.15) -- (-2,0); %opposite

        \draw[thin] (-0.45,0.255) arc (149:180:0.5)
            node[ above left, scale=.7] { $30^\circ$};
    \end{tikzpicture}
\end{tikzpicture}
\end{document}

在此处输入图片描述

答案3

使用选项node[above left]

相关内容