7

I wrote the following codes. I am trying to obtain helix (as in DNA) type of structure on which everything - intersection points etc - can be seen explicitly. Whatever I do I am getting really ugly shapes. I have played angles, curvatures etc. Any help would be great! Some codes are not active, my apologies for that.

\documentclass{article}
\usepackage{tikz,subfigure}
\usetikzlibrary{intersections,calc}

\newdimen\XCoord
\newdimen\YCoord

\newcommand*{\ExtractCoordinate}[1]{\path (#1); \pgfgetlastxy{\XCoord} {\YCoord};} %

\newcommand*{\LabelCurrentCoordinate}[2]{\fill[#1] ($(\XCoord, \YCoord)$) circle (2pt) node [right] {#2}} %

\begin{figure}
\centering
\begin{tikzpicture}[ultra thick, scale=1]
\draw (0,0) -- +(10,0) node[pos=0,left]{\tt\color{black}1};

\draw (0,-10)-- +(10,0) node[pos=0,left]{\tt\color{black}0};

\draw [red,line width=2pt, name path= curve 1] (0,-10) -- (1,-10) to [out=0,in=180] (3,-8) to[out=0,in=270] (5,-6) to[out=90,in=180] (7,-4) to [out=0,in=180] (9,-2) to [out=0,in=180] (9.8,0) to (10,0);


 \draw [blue, line width=2pt, name path= curve 2] (0,-10) -- (0.2,-10) to [out=0,in=270] (4,-8) to[out=-270, in=180] (6,-6) to [out=0,in=160](8,-2) to[out=-20,in=270] (9.8,0) -- (10,0);

 \fill[name intersections={of=curve 1 and curve 2,total=\t}]
 \foreach \s in {2,...,\t}{(intersection-\s) circle (3pt)
node[above left] {$E$\s}};

\end{tikzpicture}
\end{figure}
\end{document}
  • 1
    Could you please turn the code snippet into a complete minimal example document (starting from \documentclass)? That will save others the work of having to fill in the blanks (what libraries are needed, for example). Also, I think you need to explain in a bit more detail what the desired result will look like: When I compile your code, it looks nothing like a helix, but rather like a plot of the error function. – Jake Dec 4 '13 at 16:09
  • @Jake: Many thanks! I am so tired I couldn't realize that! – user64066 Dec 4 '13 at 16:30
  • Thanks for the edit! It would still be good if you could explain in more detail what your goal is: I still don't understand what you want the image to look like. – Jake Dec 4 '13 at 16:31
  • I have changed the code. If you run it you will see red and blue are intersecting. What I want is a couple of more intersection point. I can do this by writing a couple of more lines of code. However, the shape of figure is getting bad. What I want is a nice figure, better than that more automatic way of doing this. Hope this makes sense! – user64066 Dec 4 '13 at 17:04
  • @user64066: Do the curves have to be irregular as in your example? If not, you could try plotting upward sloping sine and cosine curves, and you can get more or less intersection points by varying their frequencies. – Herr K. Dec 5 '13 at 4:29
5

To get a nice repeating structure (as mentioned in @KevinC's comment), use plot functionality in tikz.

I defined an \ampl command to set the amplitude of the sine waves we'll be plotting (0.5 in this example). You may want to adjust this if you change the scale or number of cycles.

The number of cycles to plot is set using the \cycles command (3 for this example). The total number of intersections (including the first one, which you do not label) will be 2*\cycles + 1.

The option samples=80 tells tikz to plot 80 points for each plot over the domain (domain=0:10). The value of samples can be adjusted to obtain less smoothness if desired.

I also converted your usage of \tt (deprecated) to the current texttt{} syntax.

The Code

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{intersections,calc}

\newcommand\ampl{0.5} % amplitude of sine variation
\newcommand\cycles{3} % number of cycles to plot

\begin{document}
\begin{tikzpicture}[
  line width=2pt,
  domain=0:10, % plot functions from x=0 to x=10
  samples=80,  % sample 80 times over domain (adjust for smoothness)
]

  % draw helix
  \draw[red,name path=curve 1] plot (\x,{\x+\ampl*sin(36*\cycles*\x)});
  \draw[blue,name path=curve 2] plot (\x,{\x-\ampl*sin(36*\cycles*\x)});

  % draw ``axes''
  \draw (0,10)-- +(10,0) node[pos=0,left]{\texttt{1}}; % \tt is deprecated
  \draw (0,0) -- +(10,0) node[pos=0,left]{\texttt{0}}; % use \texttt{<content>} instead

  % draw intersection nodes
  \fill[name intersections={of=curve 1 and curve 2,total=\t}]
  \foreach \s in {2,...,\t}{(intersection-\s) circle (3pt) node[above left] {$E$\s}};
\end{tikzpicture}
\end{document}

The Output

enter image description here

Edit

Another method, using the answer from here. This one eliminates the "skewness" in the sine waves and doesn't depend on the intersections library.

The Code

\documentclass[border=5pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{calc,decorations.pathmorphing}

\newcommand\ampl{0.5cm} % amplitude of sine variation
\newcommand\cycles{7} % number of cycles to plot
\pgfmathsetmacro\finish{2*\cycles+1} % final intersection point

\tikzset{/pgf/decoration/.cd,
    number of sines/.initial=10,
    angle step/.initial=10,
}
\newdimen\tmpdimen
\pgfdeclaredecoration{complete sines}{initial}
{
    \state{initial}[
        width=+0pt,
        next state=move,
        persistent precomputation={
            \pgfmathparse{\pgfkeysvalueof{/pgf/decoration/angle step}}%
            \let\anglestep=\pgfmathresult%
            \let\currentangle=\pgfmathresult%
            \pgfmathsetlengthmacro{\pointsperanglestep}%
                {(\pgfdecoratedremainingdistance/\pgfkeysvalueof{/pgf/decoration/number of sines})/360*\anglestep}%
        }] {}
    \state{move}[width=+\pointsperanglestep, next state=draw]{
        \pgfpathmoveto{\pgfpointorigin}
    }
    \state{draw}[width=+\pointsperanglestep, switch if less than=1.25*\pointsperanglestep to final, % <- bit of a hack
        persistent postcomputation={
        \pgfmathparse{mod(\currentangle+\anglestep, 360)}%
        \let\currentangle=\pgfmathresult%
    }]{%
        \pgfmathsin{+\currentangle}%
        \tmpdimen=\pgfdecorationsegmentamplitude%
        \tmpdimen=\pgfmathresult\tmpdimen%
        \divide\tmpdimen by2\relax%
        \pgfpathlineto{\pgfqpoint{0pt}{\tmpdimen}}%
    }
    \state{final}{
        \ifdim\pgfdecoratedremainingdistance>0pt\relax
            \pgfpathlineto{\pgfpointdecoratedpathlast}
        \fi
   }
}

\begin{document}
\begin{tikzpicture}[
  line width=2pt,
  domain=0:10, % plot functions from x=0 to x=10
  samples=80,  % sample 80 times over domain (adjust for smoothness)
]

  % draw helix
  \draw[red,decorate,decoration={complete sines,number of sines=\cycles,amplitude=\ampl}] (0,0) -- (10,10);
  \draw[blue,decorate,decoration={complete sines,number of sines=\cycles,amplitude=-\ampl}] (0,0) -- (10,10);

  % draw ``axes''
  \draw (0,10)-- +(10,0) node[pos=0,left]{\texttt{1}}; % \tt is deprecated
  \draw (0,0) -- +(10,0) node[pos=0,left]{\texttt{0}}; % use \texttt{<content>} instead

  % draw intersection nodes
  \foreach \s in {2,3,...,\finish} {
    \fill ({10*(\s-1)/(\finish-1)},{10*(\s-1)/(\finish-1)}) circle (3pt) node[above left] {$E$\s};
  }
\end{tikzpicture}
\end{document}

The Output

enter image description here

| improve this answer | |
  • Intersection points can be determined analytically rather than numerically. – kiss my armpit Feb 24 '14 at 5:46
  • @TheLastError thanks for the suggestion, I just used the intersection code as provided in the MWE since (1) I don't know what the OP has planned and (2) the question focused on the curve shapes. – Paul Gessler Feb 24 '14 at 5:49
  • 1
    @TheLastError updated with the second method I edited in. Not as elegant as PSTricks though... :~) – Paul Gessler Feb 24 '14 at 6:38
3

Just for typing exercise with PSTricks.

\documentclass[preview,border=24pt]{standalone}
\usepackage{pst-plot,pst-eucl,pgfmath}

\psset{algebraic,plotpoints=100,PosAngle=135}
\def\f#1{x+sin(2*Pi*x/2+#1*Pi)/3}

\begin{document}
\begin{pspicture}(6,6)
    \psplot[linecolor=red]{0}{6}{\f0}   \psplot[linecolor=blue]{0}{6}{\f1}
    \foreach \x[count=\i from 2] in {1,...,6}{\pstGeonode(\x,\x){E_\i}}
    \psset{yunit=6}
    \multido{\i=0+1}{2}{\uput[180](0,\i){\i}\psline(0,\i)(6,\i)}
\end{pspicture}
\end{document}

enter image description here

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.