我正在制作一个 Tikz 图形,它使用 sin 和 cos 函数。目前,我使用 Python 脚本来计算六个函数参数,这些参数被提供给 Tikz “g” 函数,如下所示:
\documentclass{article}
\usepackage{tikz}
\usepackage{adjustbox}
\usetikzlibrary{calc,shadings,shadows,shapes.arrows,positioning, shadows,arrows, trees,fit,shadings,calc,arrows,shapes,shadows,shapes.arrows,decorations.markings, decorations.pathreplacing}
\newcommand*{\g}[6]{
\begin{tikzpicture}[]
\coordinate (needle_a) at (#1, #2);
\coordinate (needle_b) at (#3, #4);
\coordinate (needle_c) at (#5, #6);
\draw [top color=white, bottom color=orange, shading angle=45] (needle_a) -- (needle_b) -- (needle_c) -- cycle;
\end{tikzpicture}}
\newcommand*{\f}[1]{
\begin{tikzpicture}[]
\pgfmathparse{#1}
\pgfmathsetmacro{\angle}{\pgfmathresult}
\pgfmathparse{cos(\angle + 0.314) * 0.1}
\pgfmathsetmacro{\mya}{\pgfmathresult}
\pgfmathparse{sin(\angle + 0.314) * 0.1 + 0.15}
\pgfmathsetmacro{\myb}{\pgfmathresult}
\pgfmathparse{cos(\angle - 0.314) * 0.1}
\pgfmathsetmacro{\myc}{\pgfmathresult}
\pgfmathparse{sin(\angle - 0.314) * 0.1 + 0.15}
\pgfmathsetmacro{\myd}{\pgfmathresult}
\pgfmathparse{cos(\angle) * 0.5}
\pgfmathsetmacro{\mye}{\pgfmathresult}
\pgfmathparse{sin(\angle) * 0.5 + 0.15}
\pgfmathsetmacro{\myf}{\pgfmathresult}
\coordinate (needle_a) at (\mya, \myb);
\coordinate (needle_b) at (\myc, \myd);
\coordinate (needle_c) at (\mye, \myf);
\draw [top color=white, bottom color=orange, shading angle=45] (needle_a) -- (needle_b) -- (needle_c) -- cycle;
\end{tikzpicture}}
\begin{document}
\begin{figure}
\centering
\begin{adjustbox}{width=0.8\textwidth}
\begin{tikzpicture}[]
\node [scale=2.5] (s1) at (0, 0) {\g{0.088}{0.198}{0.099}{0.137}{0.491}{0.244}};
\end{tikzpicture}
\end{adjustbox}
\caption{g}
\end{figure}
\begin{figure}
\centering
\begin{adjustbox}{width=0.8\textwidth}
\begin{tikzpicture}[]
\node [scale=2.5] (s2) at (0, 0) {\f{0.188}};
\end{tikzpicture}
\end{adjustbox}
\caption{f}
\end{figure}
\end{document}
然而,这些参数都是基于单个数值(即角度),并按如下方式计算:
a = math.cos(angle + 0.314) * 0.1
b = math.sin(angle + 0.314) * 0.1 + 0.15
c = math.cos(angle - 0.314) * 0.1
d = math.sin(angle - 0.314) * 0.1 + 0.15
e = math.cos(angle) * 0.5
f = math.sin(angle) * 0.5 + 0.15
因此,我认为直接在 Tikz 中计算这些值会很好,而不必依赖额外的脚本。我尝试在“f”函数中实现这一点,该函数只接受一个参数,但由于某种原因,它不能按预期工作。我在下面添加了结果图片。理论上,两个图应该看起来一样。
谢谢你的帮助,斯文
答案1
您必须使用\deg(angle)
或将角度转换为度数\angle r
:
\documentclass{article}
\usepackage{tikz}
\usepackage{adjustbox}
\newcommand*{\g}[6]{
\begin{tikzpicture}[]
\coordinate (needle_a) at (#1, #2);
\coordinate (needle_b) at (#3, #4);
\coordinate (needle_c) at (#5, #6);
\draw [top color=white, bottom color=orange, shading angle=45] (needle_a) -- (needle_b) -- (needle_c) -- cycle;
\end{tikzpicture}}
\newcommand*{\f}[1]{%
\begin{tikzpicture}[]
\pgfmathsetmacro{\angle}{#1 r}
%
\pgfmathsetmacro{\mya}{cos(\angle + 0.314 r) * 0.1}
\pgfmathsetmacro{\myb}{sin(\angle + 0.314 r) * 0.1 + 0.15}
\pgfmathsetmacro{\myc}{cos(\angle - 0.314 r) * 0.1}
\pgfmathsetmacro{\myd}{sin(\angle - 0.314 r) * 0.1 + 0.15}
\pgfmathsetmacro{\mye}{cos(\angle) * 0.5}
\pgfmathsetmacro{\myf}{sin(\angle) * 0.5 + 0.15}
%
\coordinate (needle_a) at (\mya, \myb);
\coordinate (needle_b) at (\myc, \myd);
\coordinate (needle_c) at (\mye, \myf);
%
\draw [top color=white, bottom color=orange, shading angle=45]
(needle_a) -- (needle_b) -- (needle_c) -- cycle;
\end{tikzpicture}}
\begin{document}
\begin{figure}
\centering
\begin{adjustbox}{width=0.8\textwidth}
\begin{tikzpicture}[]
\node [scale=2.5] (s1) at (0, 0) {\g{0.088}{0.198}{0.099}{0.137}{0.491}{0.244}};
\end{tikzpicture}
\end{adjustbox}
\caption{g}
\end{figure}
\begin{figure}
\centering
\begin{adjustbox}{width=0.8\textwidth}
\begin{tikzpicture}[]
\node [scale=2.5] (s2) at (0, 0) {\f{0.188}};
\end{tikzpicture}
\end{adjustbox}
\caption{f}
\end{figure}
\end{document}