2

I am working on a Tikz figure, which uses the sin and cos functions. Currently I use a Python script to compute the six function parameters, which are given to the Tikz "g" function, as shown below:

\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}

However, these parameters are all based on a single numeric value (namely, an angle), and are computed as follows:

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

Therefore, I though it would be nice to compute these values directly in Tikz, without having to rely on an additional script. I tried to implement this in the "f" function, which takes only one parameter, but for some reason it does not work as expected. I added a picture of the result below. In theory, both figures should look the same.

The resulting figures

Thanks for your help, Sven

2

You have to convert the angles in degree using the \deg(angle) or \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}

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.