如何将坐标参数传递给函数?

如何将坐标参数传递给函数?

我试图通过调用函数来绘制弧,而不是。我不知道如何将坐标传递给函数。我尝试了两种方法:一种是直接传递(首选),另一种是分别传递其 x 和 y 坐标。它们都失败了,并显示“!缺少 \endcsname 插入。\sp”。请告诉我调试以下代码。谢谢。

\documentclass[border=1cm]{standalone}
\usepackage{tikz}
\usetikzlibrary {math}
\usetikzlibrary{calc}   
\begin{document}
    \begin{tikzpicture} 
        \draw[loosely dashed] (0,0) grid [step=2] (5,5)
        \def \cpt {(1,2)}
        \draw (0.3,0.5) node [right] {cpt position coordinate = \cpt } ;
        \def \cptTwo {(4,2)+(180-30:3)}
  \tikzmath{
    function    arcStartPoint(\center, \bAngle, \eAngle, \radius)   {
    %function   arcStartPoint(\cx, \cy, \bAngle, \eAngle, \radius)  {
        coordinate \sp; % arc starting point
        \sp = \center + ({radius * cos(\bAngle)}, {radius * sin(\bAngle)});
        %\sp = (\cx, \cy) + ({radius * cos(\bAngle)}, {radius * sin(\bAngle)});
        { \draw[ultra thick, orange] (1,2) arc (\bAngle:\eAngle:\radius);   };
    };
    arcStartPoint(\cpt, -30, 30, 1);
    arcStartPoint(\cptTwo, -30, 30, 3);
  };
\end{tikzpicture}
\end{document}

预期结果是:

在此处输入图片描述

答案1

尝试以下代码:

\documentclass[border=1cm]{standalone}
\usepackage{tikz}
\usetikzlibrary{calc}

\begin{document}
    \begin{tikzpicture} 
        \draw[loosely dashed] (0,0) grid [step=2] (5,5);

        % Modify the new command for drawing the arc to include color
        \newcommand{\arcStartPoint}[6]{% #1 = x coordinate, #2 = y coordinate, #3 = start angle, #4 = end angle, #5 = radius, #6 = color
            \draw[ultra thick, #6] (#1,#2) ++(#3:#5) arc (#3:#4:#5);
        }

        % Coordinates
        \coordinate (cpt) at (1,2);

        % Draw nodes for coordinate positions
        \draw (0.3,0.5) node [right] {cpt position coordinate = (1,2) } ;

        % Draw arcs with specified colors
        \arcStartPoint{1}{2}{-30}{30}{1}{orange} % Smaller arc in orange
        \arcStartPoint{1.4}{3.5}{30}{-30}{3}{red!80!black} % Larger arc in dark red
    \end{tikzpicture}
\end{document}

在此处输入图片描述

我还尝试使用以下函数:

\documentclass[border=1cm]{standalone}
\usepackage{tikz}
\usetikzlibrary{math}
\usetikzlibrary{calc}   

\begin{document}
    \begin{tikzpicture} 
        \draw[loosely dashed] (0,0) grid [step=2] (5,5);

        % Define coordinates
        \coordinate (cpt) at (1,2);
        \coordinate (cptTwo) at ($(4,2)+(180-30:3)$);

        % Draw nodes to show positions
        \draw (0.3,0.5) node [right] {cpt position coordinate = (1,2)} ;
        \draw (3.3,0.5) node [right] {cptTwo position coordinate = (4,2)+(180-30:3)} ;

        % TikZ math function
        \tikzmath{
            function arcStartPoint(\cx, \cy, \bAngle, \eAngle, \radius) {
                {
                    \draw[ultra thick, orange] (\cx,\cy) ++(\bAngle:\radius) arc (\bAngle:\eAngle:\radius);
                };
            };
        }

        % Extract coordinates and use in function
        \pgfgetlastxy{\XCoord}{\YCoord};
        \path (cpt); \pgfgetlastxy{\XCoord}{\YCoord};
        arcStartPoint(\XCoord, \YCoord, -30, 30, 1);

        \path (cptTwo); \pgfgetlastxy{\XCoord}{\YCoord};
        arcStartPoint(\XCoord, \YCoord, -30, 30, 3);
    \end{tikzpicture}
\end{document}

将坐标定义为 \coordinate,以便在 TikZ 中更好地处理。

更改了arcStartPoint函数,现在它分别获取坐标的 x 和 y 分量。在调用 之前arcStartPoint,它使用 提取每个坐标的 x 和 y 分量\pgfgetlastxy

这可以运行,但我似乎无法绘制弧线

答案2

似乎无法以简单的方式将坐标传递给函数。使用单独的X传递给函数的值,但是,它可以起作用:

\documentclass[border=1cm]{standalone}
\usepackage{tikz}
\usetikzlibrary{math, calc}
\begin{document}
    \begin{tikzpicture} 
        \draw[loosely dashed] (0,0) grid[step=2] (5,5);
        \tikzmath{
            function arcSeparateCoord(\cx, \cy, \bAngle, \eAngle, \radius) {
                coordinate \start; 
                \start = (\cx*1pt,\cy*1pt) + 
                    ({\radius*cos(\bAngle)}, {\radius*sin(\bAngle)});
                {
                    \draw[ultra thick, orange] (\start) 
                        arc[start angle=\bAngle, end angle=\eAngle, 
                        radius=\radius]; 
                };
            };
            arcSeparateCoord(1, 2, -30, 30, 1);
        };
    \end{tikzpicture}
\end{document}

在此处输入图片描述

如果你想用坐标进行更复杂的计算,你应该在传递之前完成它们X将结果坐标的值传递给函数:

\documentclass[border=1cm]{standalone}
\usepackage{tikz}
\usetikzlibrary{math, calc}
\begin{document}
    \begin{tikzpicture} 
        \draw[loosely dashed] (0,0) grid[step=2] (5,5);
        \tikzmath{
            function arcSeparateCoord(\cx, \cy, \bAngle, \eAngle, \radius) {
                coordinate \start; 
                \start = (\cx*1pt,\cy*1pt) + 
                    ({\radius*cos(\bAngle)}, {\radius*sin(\bAngle)});
                {
                    \draw[ultra thick, orange] (\start) 
                        arc[start angle=\bAngle, end angle=\eAngle, 
                        radius=\radius]; 
                };
            };
            coordinate \a; 
            \a = (4,2)+(180-30:3);
            arcSeparateCoord(\ax, \ay, -30, 30, 3);
        };
    \end{tikzpicture}
\end{document}

在此处输入图片描述

请注意,我(\cx*1pt,\cy*1pt)在函数内部进行设置以确保值被解析为 pt,因为默认坐标系以 cm 为单位。

答案3

根据 Jasper 关于厘米到点转换的回答,我修改了 John 的回答以显示弧。

\documentclass[border=1cm]{standalone}
\usepackage{tikz}
\usetikzlibrary{math}
\usetikzlibrary{calc}   
\begin{document}
    \begin{tikzpicture}[scale=1, every node/.style={draw}]
            \draw[loosely dashed] (0,0) grid [step=2] (5,5);

            % Define coordinates
            \coordinate (cpt) at (1,2);
            \coordinate (cptTwo) at ($(4,2)+(180-30:3)$);
            
            % Draw nodes to show positions
            \path (cpt); \pgfgetlastxy{\XCoord}{\YCoord};
            \pgfmathsetmacro{\xC}{\XCoord*(1pt)/(1cm)}
            \pgfmathsetmacro{\yC}{\YCoord*(1pt)/(1cm)}
            \draw (0.3,1.0) node [right] {cpt pt coordinate = (\XCoord,\YCoord)} ;
            \draw (0.3,0.5) node [right] {cpt cm coordinate = (\xC,\yC)} ;
            
            \path (cptTwo); \pgfgetlastxy{\XCoord}{\YCoord};
            \pgfmathsetmacro{\xCtwo}{\XCoord*(1pt)/(1cm)}
            \pgfmathsetmacro{\yCtwo}{\YCoord*(1pt)/(1cm)}
            % TikZ math function
            \tikzmath{
                function arcStartPoint(\cx, \cy, \bAngle, \eAngle, \radius) {
                    { \draw[ultra thick, orange!80!purple] (\cx, \cy) + 
                            (\bAngle:\radius) arc (\bAngle:\eAngle:\radius);            
                    };  
                };
                arcStartPoint(\xC, \yC, -30, 30, 1);    % must be inside \tikzmath
                arcStartPoint(\xCtwo, \yCtwo, -30, 30, 3);  % must be inside \tikzmath
            };
    \end{tikzpicture}
\end{document}

在此处输入图片描述

相关内容