函数获取两个点并返回距离以用于节点文本

函数获取两个点并返回距离以用于节点文本

此代码绘制一个三角形,并标明每条边的长度。目前,它使用 let-in 表达式来计算每条边的距离。我对此不满意,因为最好将其作为一个函数,接受两个点并返回距离。我尝试在 的定义中复制 let-in 表达式中的代码calculateDistanceAndPrint,但它没有按预期工作。

如何防止此示例中的代码重复?

在此处输入图片描述

\documentclass{standalone}
\usepackage{pgfplots}
\usetikzlibrary{calc}
\begin{document}

\begin{tikzpicture}
\pgfkeys{/pgf/number format/.cd,
fixed,
fixed zerofill,
precision=1,}
    \newcommand{\calculateDistanceAndPrint}[2]{%
       \pgfmathsetmacro{\p1}{$({#1}) - ({#2})$ )}
       \pgfmathsetmacro{\distance}{veclen({\x1} - {\y1})}
       \pgfmathparse{\distance/28.45274}
       \pgfmathprintnumber{\pgfmathresult}
    }
    \coordinate[label=A] (A) at (0, 1);
    \coordinate[label=B] (B) at (3, 4);
    \coordinate[label=C] (C) at (8, 3);
    \draw let
        \p{AB} = ($ (B) - (A) $),
        \n{AB} = {veclen(\x{AB},\y{AB})},
        \p{AC} = ($ (C) - (A) $),
        \n{AC} = {veclen(\x{AC},\y{AC})},
        \p{BC} = ($ (B) - (C) $),
        \n{BC} = {veclen(\x{BC},\y{BC})}
    in
    (A) -- node[left]{\pgfmathparse{\n{AB}/28.45274}\pgfmathprintnumber{\pgfmathresult}}
    (B) -- node[left]{\pgfmathparse{\n{BC}/28.45274}\pgfmathprintnumber{\pgfmathresult}}
    (C) -- node[left]{\pgfmathparse{\n{AC}/28.45274}\pgfmathprintnumber{\pgfmathresult}}
    (A) ;

    % (A) -- node[left]{\calculateDistanceAndPrint{(A)}{(B)}}
    % (B) -- node[left]{\calculateDistanceAndPrint{(B)}{(C)}}
    % (C) -- node[left]{\calculateDistanceAndPrint{(C)}{(A)}}
    % (A) ;
\end{tikzpicture}
\end{document}
figures/01-triangles.tex|33 error| Package PGF Math Error: Unknown operator `$' or `$(' (in '$({(A)}) - ({(B)})$ )').
figures/01-triangles.tex|33 error| Undefined control sequence.
figures/01-triangles.tex|33 error| Missing number, treated as zero.
figures/01-triangles.tex|33 error| Argument of \pgfmath@dimen@@ has an extra }.
figures/01-triangles.tex|| Runaway argument?
figures/01-triangles.tex|33 error| Paragraph ended before \pgfmath@dimen@@ was complete.
figures/01-triangles.tex|33 error| Extra }, or forgotten \endgroup.
figures/01-triangles.tex|34 error| Package PGF Math Error: Unknown operator `$' or `$(' (in '$({(B)}) - ({(C)})$ )').
figures/01-triangles.tex|34 error| Undefined control sequence.
figures/01-triangles.tex|34 error| Missing number, treated as zero.
figures/01-triangles.tex|34 error| Argument of \pgfmath@dimen@@ has an extra }.
figures/01-triangles.tex|| Runaway argument?
figures/01-triangles.tex|34 error| Paragraph ended before \pgfmath@dimen@@ was complete.
figures/01-triangles.tex|34 error| Extra }, or forgotten \endgroup.
figures/01-triangles.tex|35 error| Package PGF Math Error: Unknown operator `$' or `$(' (in '$({(C)}) - ({(A)})$ )').
figures/01-triangles.tex|35 error| Undefined control sequence.
figures/01-triangles.tex|35 error| Missing number, treated as zero.
figures/01-triangles.tex|35 error| Argument of \pgfmath@dimen@@ has an extra }.
figures/01-triangles.tex|| Runaway argument?
figures/01-triangles.tex|35 error| Paragraph ended before \pgfmath@dimen@@ was complete.
figures/01-triangles.tex|35 error| Extra }, or forgotten \endgroup.
figures/01-triangles.tex|36 error| Undefined control sequence.
figures/01-triangles.tex|36 error| Extra }, or forgotten \endgroup.
figures/01-triangles.tex|36 error| Missing } inserted.
figures/01-triangles.tex|36 error| Package tikz Error: Giving up on this path. Did you forget a semicolon?.
figures/01-triangles.tex|36 error| Package tikz Error: Giving up on this path. Did you forget a semicolon?.
figures/01-triangles.tex|36 error| Missing } inserted.
figures/01-triangles.tex|36 error| Package tikz Error: Giving up on this path. Did you forget a semicolon?.
figures/01-triangles.tex|37 error| Extra }, or forgotten \endgroup.
figures/01-triangles.tex|37 error| Improper \prevdepth.
figures/01-triangles.tex|37 error| Missing \endgroup inserted.
figures/01-triangles.tex|37 error| Missing \endgroup inserted.
figures/01-triangles.tex|37 error| Missing \endgroup inserted.
figures/01-triangles.tex|37 error| Missing \endgroup inserted.
figures/01-triangles.tex|37 error| Missing } inserted.
figures/01-triangles.tex|37 error| Missing \endgroup inserted.
figures/01-triangles.tex|37 error| Missing \endgroup inserted.
figures/01-triangles.tex|37 error| Missing \endgroup inserted.
figures/01-triangles.tex|37 error| Missing } inserted.
figures/01-triangles.tex|37 error| Missing \endgroup inserted.
figures/01-triangles.tex|37 error| Missing \endgroup inserted.
figures/01-triangles.tex|37 error| Missing \endgroup inserted.
figures/01-triangles.tex|37 error| Missing \endgroup inserted.
figures/01-triangles.tex|37 error| Missing } inserted.

答案1

使用包tkz-elementstikztkz-euclide和编译lualatex

  • 在 Triangle 类中(文档中的第 11 节tkz-elements),

a 是第一个顶点对边的长度 b 是第二个顶点对边的长度 c 是第三个顶点对边的长度

  • tkz-euclide我们使用(或tikz)的长度\tkzUseLua

  • 我们可以定义一个命令来简化代码

     % !TeX TS-program = lualatex
     \documentclass[border=5mm]{standalone} 
     \usepackage{tkz-elements} 
     \usepackage{tkz-euclide}
     \NewDocumentCommand{\mylg}{m}{%
     $\pgfmathprintnumber{\tkzUseLua{T.ABC.#1}}$%
     }
     \begin{document}
     \begin{tkzelements}
       z.A = point: new(0,1)
       z.B = point: new(3,4)
       z.C = point: new(8,3)
       T.ABC = triangle: new (z.A,z.B,z.C)
     \end{tkzelements}
     \pgfkeys{/pgf/number format/.cd,
       fixed,
       fixed zerofill,
       precision=1,}
     \begin{tikzpicture}
       \tkzGetNodes
       \tkzDrawPolygons(A,B,C)
       \tkzLabelPoints(A,C)
       \tkzLabelPoints[above](B)
       % \tkzLabelSegments[sloped](A,B){$\pgfmathprintnumber{\tkzUseLua{T.ABC.c}}$}
       % \tkzLabelSegments[sloped](A,C){$\pgfmathprintnumber{\tkzUseLua{T.ABC.b}}$}
       % \tkzLabelSegments[sloped](B,C){$\pgfmathprintnumber{\tkzUseLua{T.ABC.a}}$}
       %
       % With a command
       \tkzLabelSegments[sloped](A,B){\mylg{c}}
       \tkzLabelSegments[sloped](A,C){\mylg{b}}
       \tkzLabelSegments[sloped](B,C){\mylg{a}}
     \end{tikzpicture}
     \end{document}
    

在此处输入图片描述

相关内容