# Graphing sine function in MetaPost

I'm trying to reconstruct this picture in MetaPost: So far came up with this:

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));


Obviously, the sine graph is missing. I tried a few ways, but I couldn't even get it to compile. Can anyone provide some assistance?

Try the following:

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

• +1 for answering the question, using metapost (not Tikz), and not using fancy LaTeX dialects (i.e., you avoided LuaLaTeX). Dec 23, 2012 at 20:17

I know that you said metapost, so I'll probably get voted down for this answer! (In my defence, I suspect that more people are going to be interested in how to draw a sine wave by any means than specifically by metapost, so although this doesn't answer your specific question, it's hopefully close enough to be worth leaving nearby). This is really easy in TikZ: p124 of the manual:

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


draws a sine wave.

More generally, TikZ can interact nicely with gnuplot to draw graphs of functions (see the manual for details). Gnuplot does the computation, TikZ does the rendering.

run it with lualatex which makes the use of MP easier:

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