Tikz 计算错误的坐标

Tikz 计算错误的坐标

我正在尝试计算一些坐标以获得准确的 tikz 图形。我设法通过硬编码一些坐标来做到这一点,但由于我必须对其中许多坐标重复该过程,所以我认为编写一个函数会更好。问题是,虽然我没有错误,但坐标是错误的,我不明白为什么。这是 MWE:

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{calc,shapes.misc,math}

\begin{document}

\tikzmath{
\u1=1/3;
\v1=1/3;
}

\tikzset{cross/.style={cross out, draw=black, minimum size=4*(#1-\pgflinewidth), inner sep=0pt, outer sep=0pt},
%default radius will be 1pt. 
cross/.default={1pt}}

\begin{tikzpicture}[
  declare function={map(\xi,\eta,\u,\v,\w) = (1-\xi-\eta)*\u+\xi*\v+\eta*\w;}
]

\begin{scope} []

% define internal node coordinates
\coordinate (X1) at (-1.66,0.3012);
\coordinate (X2) at (0.1332,0.9123);
\coordinate (X3) at (-0.6667,0.2123);

% draw triangle
\draw [] (X1) -- (X2) -- (X3) -- cycle;

\path let \p1 = (X1), \p2 = (X2), \p3 = (X3) in node[cross] at ({map(1/3,1/3,-1.66,0.1332,-0.6667)},{map(1/3.,1/3.,0.3012,0.9123,0.2123)}) {};
%\path let \p1 = (X1), \p2 = (X2), \p3 = (X3) in node[cross] at ({map(\u1,\v1,-1.66,0.1332,-0.6667)},{map(\u1,\v1,0.3012,0.9123,0.2123)}) {};
%\path let \p1 = (X1), \p2 = (X2), \p3 = (X3) in node[cross] at ({map(1/3,1/3,\x1,\x2,\x3)},{map(1/3.,1/3.,\y1,\y2,\y3)}) {};
%\path let \p1 = (X1), \p2 = (X2), \p3 = (X3) in node[cross] at ({map(\u1,\v1,\x1,\x2,\x3)},{map(\u1,\v1,\y1,\y2,\y3)}) {};

\end{scope}

\end{tikzpicture}

\end{document}

十字形正确地放置在三角形的中心,但是当我使用函数(注释行)时,十字形会移动到其他位置。前两个​​命令工作正常,因此我设法使用\u1v1,但后两个命令不起作用,因此我显然在语法上做了一些错误let。我在 Tikz 手册中找不到与此相关的任何内容。

答案1

原因是代替。因此\x1,解决问题的一种方法是将它们转换回。我已经定义并使用了一个可以执行此操作的函数。\x2ptcmcm

\documentclass[tikz,border=7pt]{standalone}
\usetikzlibrary{calc,shapes.misc,math}

\tikzmath{
  \u1=1/3;
  \v1=1/3;
}

\tikzset{
  cross/.style={
    cross out,
    draw=black,
    minimum size=4*(#1-\pgflinewidth),
    inner sep=0pt,
    outer sep=0pt
  },
  %default radius will be 1pt.
  cross/.default={1pt},
  declare function={cm(\t) = \t*0.03514598;}, % 1pt = 0.03514598 cm
  declare function={map(\xi,\eta,\u,\v,\w) = (1-\xi-\eta)*\u+\xi*\v+\eta*\w;}
}

\begin{document}
  \begin{tikzpicture}
    % define internal node coordinates
    \coordinate (X1) at (-1.66,0.3012);
    \coordinate (X2) at (0.1332,0.9123);
    \coordinate (X3) at (-0.6667,0.2123);

    % draw triangle
    \draw [] (X1) -- (X2) -- (X3) -- cycle;

    \path let \p1 = (X1), \p2 = (X2), \p3 = (X3)
      in node[cross]
      at ({map(1/3,1/3,-1.66,0.1332,-0.6667)},{map(1/3.,1/3.,0.3012,0.9123,0.2123)}) {};
    \path let \p1 = (X1), \p2 = (X2), \p3 = (X3)
      in node[cross, red]
      at ({map(\u1,\v1,-1.66,0.1332,-0.6667)},{map(\u1,\v1,0.3012,0.9123,0.2123)}) {};
    \path let \p1 = (X1), \p2 = (X2), \p3 = (X3)
      in node[cross, blue, very thin]
      at ({map(1/3,1/3,cm(\x1),cm(\x2),cm(\x3))},{map(1/3.,1/3.,cm(\y1),cm(\y2),cm(\y3))}) {};
    \path let \p1 = (X1), \p2 = (X2), \p3 = (X3)
      in node[cross, orange, ultra thin]
      at ({map(\u1,\v1,cm(\x1),cm(\x2),cm(\x3))},{map(\u1,\v1,cm(\y1),cm(\y2),cm(\y3))}) {};
  \end{tikzpicture}
\end{document}

在此处输入图片描述

暗示:如果您想计算点的重心坐标,有更好的方法可以做到。

相关内容