4

quick question:

\begin{tikzpicture}
\draw  (0,0) grid (4,2);
\fill[red] (canvas cs:x=1cm,y={sin(120)}) circle (2pt);
\fill[green] (canvas cs:x=2cm,y={sin(45)}) circle (2pt);
\fill[blue] (canvas cs:x=3cm,y=-{sin(30)}) circle (2pt);
\fill[black] (canvas cs:x=4cm,y=5mm+{sin(30)}) circle (2pt); << it does not work
\end{tikzpicture}

How do I insert a trig function within a coordinate calculation, as above last line?

enter image description here

  • The nodes are exactly where they are supposed to be. e.g. sin(30) is 0.5, which would be 0.5cm, adding 5mm yields 1cm and that's where the black node is placed. – clocktown May 28 '17 at 9:04
  • @clocktown Forget my edit. How do I make tiles 1cmx1cm? – mario May 28 '17 at 10:29
7
\fill[black] (canvas cs:x=4cm,y={5mm+sin(30)}) circle (2pt);

You basically just have to move that one { to the left a bit. TikZ Syntax can be quite tricky sometimes.

Edit:
The result of these sinus-calculations for y seem to be in a weird unit, at least not a metric one it seems. I always thought cm would be default, but that assumption seems to be wrong here. I'm guessing it's something like pt here. Thus I'd suggest:

\begin{tikzpicture}
\draw  (0,0) grid (4,2);
\fill[red] (canvas cs:x=1cm,y={1cm*sin(120)}) circle (2pt);
\fill[green] (canvas cs:x=2cm,y={1cm*sin(45)}) circle (2pt);
\fill[blue] (canvas cs:x=3cm,y=-{1cm*sin(30)}) circle (2pt);
\fill[black] (canvas cs:x=4cm,y={5mm+1cm*sin(30)}) circle (2pt);
\end{tikzpicture}

1cm* converts it into cm, so you can change it to whatever unit you want.

  • @ clocktown Thks, look at it tomorrow. – mario May 25 '17 at 17:13
  • @ clocktown See new image, it seems that 1cm*sin(120) returns 1cm plus sin(120). Is that intended? – mario May 28 '17 at 8:52
  • It's most likely not a weird unit, but points (1 pt = 1/72.27 in). It appears that if no unit is given, then points are used, try for example \fill[red] (canvas cs:x=1cm,y=28.45) circle (2pt); \fill[green] (canvas cs:x=2cm,y=1cm) circle (2pt); (28.45pt is about 1cm) Also, read the description of the xyz coordinate system in the manual, especially the last two paragraphs, starting with Note and Note furthermore. – Torbjørn T. May 28 '17 at 9:45
  • That's what I assumed too. Points is a weird unit to me, anything non metric is for me ;) – clocktown May 28 '17 at 10:00
0

Package calculator seems to make things easier:

\documentclass[convert={density=300,size=1080x800,outext=.png}]{standalone}

\usepackage{tikz}
\usepackage{calculator}
\usetikzlibrary{rulercompass}
\usetikzlibrary{intersections,quotes,angles}
\usetikzlibrary{calc}

\begin{document}
\begin{tikzpicture}
\draw[step=1cm,black!25,thin] (-1,-1) grid (5,2);
%sin(120)=0.86
\DEGREESSIN{120}{\sol}
\TRUNCATE[2]{\sol}{\sol} 
\fill[red] (canvas cs:x=0cm,y={1cm*sin(120)}) circle (2pt) 
node[label={[xshift=0.35cm]{\sol}}] {};

\fill[red] (canvas cs:x=0.2cm,y={1cm*\sol}) circle (2pt) 
node[label={[xshift=0.35cm, yshift=-0.65cm]{\sol}}] {};

\fill[red] (canvas cs:x=.4cm,y=0.86cm) circle (2pt);
%sin(90)=1
\DEGREESSIN{90}{\sol}
%\TRUNCATE[2]{\sol}{\sol} 
\fill[purple] (canvas cs:x=1cm,y={1cm*sin(90)}) circle (2pt) 
node[label={[xshift=0.35cm]{\sol}}] {};

\fill[purple] (canvas cs:x=1.2cm,y={1cm*\sol}) circle (2pt) 
node[label={[xshift=0.35cm, yshift=-0.65cm]{\sol}}] {};

%sin(45)=0.7
\DEGREESSIN{45}{\sol}
\TRUNCATE[2]{\sol}{\sol}
\fill[green] (canvas cs:x=2cm,y={1*sin(45)}) circle (2pt)
node[label={[xshift=0.35cm]{\sol}}] {};

% sin(30) = 0.5
\DEGREESSIN{30}{\sol}
\TRUNCATE[2]{\sol}{\sol}
\fill[blue] (canvas cs:x=3cm,y={1cm*sin(30)}) circle (2pt)
node[label={[xshift=-0.35cm, yshift=-0.65cm]{\sol}}] {};

\fill[black] (canvas cs:x=3cm,y={5mm+1*sin(30)}) circle (2pt)
node[label={[xshift=0.35cm]{\sol}}] {};

\fill[pink] (canvas cs:x=4cm,y={5mm+1*\sol}) circle (2pt)
node[label={[xshift=0.35cm]{\sol}}] {};
\end{tikzpicture}
\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.