快速提问:
\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}
如何在坐标计算中插入三角函数,如最后一行所示?
答案1
\fill[black] (canvas cs:x=4cm,y={5mm+sin(30)}) circle (2pt);
基本上,你只需要将那个{
向左移动一点。TikZ 语法有时可能相当棘手。
编辑:
这些正弦计算的结果似乎y
采用奇怪的单位,至少看起来不是公制单位。我一直认为cm
这将是默认的,但这个假设在这里似乎是错误的。我猜它就像pt
这里一样。因此我建议:
\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*
将其转换为厘米,因此您可以将其更改为您想要的任何单位。
答案2
软件包calculator
似乎使事情变得更容易:
\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}