在 MetaPost 中绘制正弦函数图形

在 MetaPost 中绘制正弦函数图形

我正在尝试重建这张照片在 MetaPost 中:

在此处输入图片描述

到目前为止想到的是这个:

draw (-1cm,0)--(3cm,0);
draw (0,1cm)--(0,-1cm);
draw fullcircle scaled 1.8cm;
label(btex $y$ etex,(0,1.2cm));
label(btex $x$ etex,(3.2cm,0));
label(btex \textsf{SVEU\v CILI\v STE U RIJECI} etex,(1cm,1.7cm));
label(btex \textsf{ODJEL ZA MATEMATIKU} etex,(1cm,-1.5cm));

显然,正弦图缺失了。我尝试了几种方法,但甚至无法编译。有人能提供一些帮助吗?

答案1

请尝试以下操作:

draw (-90/360*1.5cm,-0.9cm) for i = -90 upto 630: .. (i/360*1.5cm,0.9cm*(sind(i))) endfor;

答案2

我知道你说过元帖子,所以我可能会因为这个答案而被否决!(为了辩护,我怀疑会有更多人对如何绘制正弦波感兴趣以任何方式而不是通过 metapost 专门介绍的,所以虽然这不能回答你的具体问题,但希望它足够接近,值得留在附近)。这是真的很容易在 TikZ: 手册第 124 页:

\begin{tikzpicture}
\draw (0,0) sin (1,1) cos (2,0) sin (3,-1) cos (4,0) sin (5,1);
\end{tikzpicture}

绘制正弦波。

更一般地,TikZ 可以与 gnuplot 很好地交互来绘制函数图(有关详细信息,请参阅手册)。Gnuplot 进行计算,TikZ 进行渲染。

答案3

运行它,lualatex这使得 MP 的使用更容易:

\documentclass{article}
\usepackage{fontspec}
\usepackage{luamplib}
\parindent=0pt
\begin{document}

\large\textsf{SVEU\v CILI\v STE U RIJECI}

\begin{mplibcode}
beginfig(1);
def compute_curve(suffix f)(expr xmin,xmax,xinc) = ( (xmin,f(xmin))
    for x=xmin+xinc step xinc until xmax: ..(x,f(x)) endfor )
enddef;   
vardef f(expr x)=sind(x*180/3.14) enddef;
path g;
g:=compute_curve(f,-1.57,9.44,0.1);
draw g xscaled 8pt yscaled 0.9cm withpen pencircle scaled 2bp;
draw (-1cm,0)--(3cm,0);
draw (0,1cm)--(0,-1cm);
draw fullcircle scaled 1.8cm;
label("y",(0,1.2cm));
label("x",(3.2cm,0));
endfig;
\end{mplibcode}

\textsf{ODJEL ZA MATEMATIKU}
\end{document} 

在此处输入图片描述

答案4

十周年解答....

在此处输入图片描述

\documentclass[border=5mm]{standalone}
\usepackage{fontspec}
\setmainfont[LetterSpace=28, Scale=1.414]{TeX Gyre Heros}
\usepackage{luamplib}
\begin{document}
\mplibtextextlabel{enable}
\begin{mplibcode}
beginfig(1);
    numeric u, pi; u = 60; pi = 3.141592653589793;
    path base, wave, xx, yy;
    base = fullcircle scaled 2u;
    wave = (-90, sind(-90)) for t = -82 step 8 until 630: .. (t, sind(t)) endfor;
    wave := wave xscaled (pi / 720) scaled u;  % scale to 1/4 radians on the x-axis...

    draw base withpen pencircle scaled 1;
    cutdraw wave withpen pencircle scaled 3/2 withcolor 5/8 blue;

    xx = (xpart llcorner currentpicture - 5, 0) -- (xpart urcorner currentpicture + 5, 0);
    yy = (0, ypart llcorner currentpicture - 5) -- (0, ypart urcorner currentpicture + 5);

    draw xx; draw yy;
    label.rt("$x$", point 1 of xx);
    label.top("$y$", point 1 of yy);

    label.top("SVEUČILIŠTE U RIJECI", point 5/2 of bbox currentpicture shifted 13 up);
    label.bot("ODJEL ZA MATEMATIKU", point 1/2 of bbox currentpicture shifted 13 down);
    
endfig;
\end{mplibcode}
\end{document}

相关内容