Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

What is the easiest way to sketch a sinusoidal wave in PGF/Tikz? I have tried this:

\begin{tikzpicture}
  \draw[loosely dotted] (0,0) grid (4,2);
  \draw[x=0.5cm,y=1cm, ultra thick, red]
    (0,1) cos (1,0) sin (2,-1) cos (3,0) sin (4,1) cos (5,0) sin (6,-1);
\end{tikzpicture}

How do I get the grid to cover all the wave? Is there a better way of sketching (not plotting) functions/waves?

share|improve this question
    
If you want the grid to cover the curve, change the coordinates in the grid line to the corners of a rectangle that encloses the curve. Can you explain more what you mean by "sketch" vs. "plot"? Do you want something that looks hand drawn? –  Matthew Leingang Apr 18 '13 at 10:24
    
If you want more hand drawn, see tex.stackexchange.com/questions/74878/… –  Torbjørn T. Apr 18 '13 at 10:28
    
@MatthewLeingang: I can't get it to work, how would I change the coordinates in this instance? By sketch I mean that I don't get Tikz to plot it for me, I put in the points it should connect and with what shape. –  user18290 Apr 18 '13 at 10:43
    
try \draw[loosely dotted] (0,-2) grid (4,2) for example. –  Thruston Apr 18 '13 at 10:55
add comment

2 Answers

up vote 4 down vote accepted

As I said in my comment, you can adjust the grid by choosing the coordinates on the line with grid in it:

\documentclass{standalone}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
  \draw[loosely dotted] (0,-1) grid (3,1);
  \draw[x=0.5cm,y=1cm, ultra thick, red]
    (0,1) cos (1,0) sin (2,-1) cos (3,0) sin (4,1) cos (5,0) sin (6,-1);
\end{tikzpicture}
\end{document}

Your second \draw line changes the scale of the x axis to 0.5cm instead of 1cm as is the default. So (3,1) on the first line is the same point as (6,1) on the second line.

sample code output

If you just want to interpolate points on a curve smoothly, you can use the plot command of tikz like so:

\draw[x=0.5cm,y=1cm, ultra thick, red]
    plot[smooth] coordinates {(0,1) (1,0) (2,-1) (3,0) (4,1) (5,0) (6,-1)};

There is a tension key that adjusts the curviness of the smoothing but frankly I couldn't get anything that looked better to me than the default.

sample code output

Another option would be to use Bézier curves. However, in specifying a Bézier curve between two points, two additional points are needed. These two describe the velocity vector coming out of and into each point.

\draw[x=0.5cm,y=1cm, ultra thick, red]  (0,1) 
  .. controls ([xshift=\dx]0,1)  and ([xshift=-\dx]2,-1)  .. (2,-1) 
  .. controls ([xshift=\dx]2,-1) and ([xshift=-\dx]4,1)   .. (4,1) 
  .. controls ([xshift=\dx]4,1)  and ([xshift=-\dx]6,-1)  .. (6,-1) ;

sample code output

You'll notice I took advantage of the symmetry to eliminate the coordinates along the $x$-axis.

share|improve this answer
add comment

enter image description here

The trembling module of the Asymptote offers an object of class tremble to be set up, to apply a trembling transform on an arbitrary curve. sketch.asy:

import graph; // for cos
import trembling;
import math; // for the grid

size(8cm);

real xmin=0, xmax=1.5pi;
real ymin=-1, ymax=1;

tremble tr=tremble(angle=20,frequency=0.6,random=50,fuzz=1);
path sinesketch=tr.deform(graph(cos,xmin,xmax)); 

int rows=2, cols=3;
add(
  shift(xmin,ymin)*scale((xmax-xmin)/cols,(ymax-ymin)/rows)
  *grid(cols,rows, red+dotted+1.2pt)
);
draw(sinesketch,darkblue+1.2pt);

Run asy -f pdf sketch.asy to get a standalone sketch.pdf.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.