# 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?

-
By the way, I use tlhiv.org/mppreview for compiling MetaPost. –  rivanvx Jul 28 '10 at 12:02

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). –  Alex Nelson Dec 23 '12 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.

-
Thanks for leaving your answer nearby. –  Heath Hunnicutt Sep 20 '12 at 23:21

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}


-