使用 TikZ/Calc 进行直角符号对齐

使用 TikZ/Calc 进行直角符号对齐

我使用了@Jake 在这篇文章中提供的解决方案在两条线的交点处插入垂直符号,但对齐不太正确。我怀疑原因,但我对pgf代码的理解不够深入,无法弄清楚。

这是我的 MWE:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{shapes,snakes,backgrounds,arrows}   
\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}[line width=1pt,>=triangle 45,x=1.0cm,y=1.0cm,point/.style={name={#1}},sharp corners]
\clip (-0.1,-0.1)   rectangle   (6.1,6.1);
\draw   [step=0.5cm,dotted,line width=0.35pt]   
    (0,0)       grid        (6,6);
%   Parallel Line Graph
%   Axis
\draw   [line width=0.75pt]         (2,0)                   --(2,6)
                                (0,2)                   --(6,2);
%   Lines & Right Angle 
\node   [point=A]                   at                  (5.5,5) {};
\node   [point=B]                   at                  (1,0.5) {};
\node   [point=C]                   at                  (5.5,0.5)   {};
\node   [point=D]                   at                  (0.5,5) {};
\draw   [<->]       (A) -- (B); 
\draw   [<->]       (C) -- (D); 
\draw [right angle quadrant=4,right angle symbol={A}{B}{D}] ($(A)!(D)!(B)$);
\end{tikzpicture}%
\end{document}

我想象解决方案是在宏内,但有人能帮助我了解在哪里吗?

答案1

(D)从到点的线与从到 的($(A)!(D)!(B)$)线成直角。(A)(B)

或者换句话说:从(A)到 的线与从到 的(B)线在 处不相交。(C)(D)($(A)!(D)!(B)$)

或者换句话说:这些线条没有以直角相交。

如果您使用 for (C) (5,.5),它就会起作用。

node另外,如果声明坐标,请不要使用s,而应使用 s。此图中的coordinate节点已突出显示 ( ):draw=red

在此处输入图片描述

代码

\documentclass[tikz,convert]{standalone}
\usetikzlibrary{arrows,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)$),     % Intersection
                \p1 = ($(\p0)!\quadranta*\rightanglelength!(#3)$), % Point on base line
                \p2 = ($(\p0)!\quadrantb*\rightanglelength!(#2)$), % Point on perpendicular line
                \p3 = ($(\p1)+(\p2)-(\p0)$) in  % Corner point of symbol
            (\p1) -- (\p3) -- (\p2)
        }
    }
}

\begin{document}
\begin{tikzpicture}[line width=1pt,>=triangle 45,x=1.0cm,y=1.0cm,point/.style={name={#1}},sharp corners]
\clip (-0.1,-0.1)   rectangle   (6.1,6.1);
\draw [step=0.5cm,dotted,line width=0.35pt] (0,0) grid (6,6);

\draw  [line width=0.75pt] (2,0) -- (2,6)
                           (0,2) -- (6,2);
\coordinate (A) at (5.5,5);
\coordinate (B) at (1,0.5);
\coordinate (C) at (5,0.5);
\coordinate (D) at (0.5,5);
%\node [draw=red, point=A] at (5.5,5)   {};
%\node [draw=red, point=B] at (1,0.5)   {};
%\node [draw=red, point=C] at (5,0.5) {};
%\node [draw=red, point=D] at (0.5,5)   {};

\draw [<->] (A) -- (B); 
\draw [<->] (C) -- (D); 

\draw [right angle quadrant=4,right angle symbol={A}{B}{D}] ($(A)!(D)!(B)$);
\end{tikzpicture}
\end{document}

输出

在此处输入图片描述

相关内容