如何对齐直角

如何对齐直角

正如您在下图中所看到的,直角与边 BC 之间留有一些空间。

我该如何修复它?有没有更好的方法来添加直角?

在此处输入图片描述

\documentclass{article}
\usepackage{tikz}

\begin{document}
   \begin{tikzpicture}
      \draw[fill=gray!10]  (0, 4) coordinate (A) 
       -- node[above=4pt] {$4cm$} (3,4) coordinate (C) 
       -- node[right] {$8cm$} (3,0) coordinate (B) 
       -- node[left] {$xcm$}  (0, 4);
     \draw (2.6,4) -- ++(0,-10pt) -- ++(10pt,0);

        \node at (A)[anchor=east] {$A$};
        \node at (B)[anchor=north] {$B$};
        \node at (C)[anchor=south] {$C$};
      \end{tikzpicture}
\end{document}

答案1

由于小正方形的边为10pt,因此你需要10pt从 的左侧开始C使用,例如

([xshift=-10pt]C)

完整示例:

\documentclass{article}
\usepackage{tikz}

\begin{document}
   \begin{tikzpicture}
      \draw[fill=gray!10]  (0, 4) coordinate (A) 
       -- node[above=4pt] {$4$\, cm} (3,4) coordinate (C) 
       -- node[right] {$8$\,cm} (3,0) coordinate (B) 
       -- node[left] {$x$\,cm}  (0, 4);
     \draw ([xshift=-10pt]C) -- ++(0,-10pt) -- ++(10pt,0);

        \node at (A)[anchor=east] {$A$};
        \node at (B)[anchor=north] {$B$};
        \node at (C)[anchor=south] {$C$};
      \end{tikzpicture}
\end{document}

在此处输入图片描述

如果你要进行大量这样的施工,我建议你tkz-euclide包;它具有非常直观的语法,并有一个预定义的命令\tkzMarkRightAngle可以轻松标记直角:

\documentclass{article}
\usepackage{tkz-euclide}
\usetkzobj{all}

\begin{document}

\begin{tikzpicture}
\tkzDefPoint(0,4){A}
\tkzDefPoint(3,4){C}
\tkzDefPoint(3,0){B}
\tkzDrawPolygon[fill=gray!10](A,B,C)
\tkzLabelPoints(B)
\tkzLabelPoints[left](A)
\tkzLabelPoints[right](C)
\tkzMarkRightAngle(B,C,A)
\tkzLabelSegment[above](A,C){$4$\,cm}
\tkzLabelSegment[left=4pt](B,A){$x$\,cm}
\tkzLabelSegment[right](C,B){$8$\,cm}
\end{tikzpicture}

\end{document}

在此处输入图片描述

答案2

您还可以使用以下right angle symbol样式:

在此处输入图片描述

笔记:

参考:

代码:

\documentclass{article}
\usepackage{tikz}
\usepackage{siunitx}
\usetikzlibrary{calc}

\tikzset{
    right angle quadrant/.code={
        \pgfmathsetmacro\quadranta{{1,1,-1,-1}[#1-1]}     % Arrays for selecting quadrant
        \pgfmathsetmacro\quadrantb{{1,-1,-1,1}[#1-1]}},
    right angle quadrant=1, % Make sure it is set, even if not called explicitly
    right angle length/.code={\def\rightanglelength{#1}},   % Length of symbol
    right angle length=2ex, % Make sure it is set...
    right angle symbol/.style n args={3}{
        insert path={
            let \p0 = ($(#1)!(#3)!(#2)$) in     % Intersection
                let \p1 = ($(\p0)!\quadranta*\rightanglelength!(#3)$), % Point on base line
                \p2 = ($(\p0)!\quadrantb*\rightanglelength!(#2)$) in % Point on perpendicular line
                let \p3 = ($(\p1)+(\p2)-(\p0)$) in  % Corner point of symbol
            (\p1) -- (\p3) -- (\p2)
        }
    }
}

\begin{document}
\begin{tikzpicture}
    \draw[fill=gray!10]  (0, 4) coordinate (A) 
       -- node[above=4pt] {$\SI{4}{\cm}$} (3,4) coordinate (C) 
       -- node[right] {$\SI{8}{\cm}$} (3,0) coordinate (B) 
       -- node[left] {$x$ \si{\cm}}  (0, 4);
     %\draw (2.6,4) -- ++(0,-10pt) -- ++(10pt,0);

    \draw [red,ultra thick, right angle quadrant=2,right angle symbol={B}{C}{A}];
        
    \node at (A)[anchor=east] {$A$};
    \node at (B)[anchor=north] {$B$};
    \node at (C)[anchor=south] {$C$};
\end{tikzpicture}
\end{document}

相关内容