2

I use the following code to draw some graphs. In this example, a sine wave.

How can the code be modified to say

(previous x + a value, previous y + a value)

instead of repeating defining the x,y coordinates manually in each location?

\documentclass{article}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}[scale=.5, transform shape]
\draw[ultra thick,red,x=0.5cm,y=1cm] (0,0)
 sin (1,1) cos (2,0) sin (3,-1) cos (4,0)
 sin (5,1) cos (6,0) sin (7,-1) cos (8,0)
 sin (9,1) cos (10,0) sin (11,-1) cos (12,0)
 sin (13,1) cos (14,0) sin (15,-1) cos (16,0)
 sin (17,1) cos (18,0) sin (19,-1) cos (20,0)
 sin (21,1) cos (22,0) sin (23,-1) cos (24,0)
 sin (25,1) cos (26,0) sin (27,-1) cos (28,0)
 sin (29,1) cos (30,0) sin (31,-1) cos (32,0);
\end{tikzpicture}
\vskip 1cm
\begin{tikzpicture}[scale=.5, transform shape]
\draw[ultra thick,blue,x=0.5cm,y=1cm] (0,0)
 sin (1,1) cos (2,0) sin (3,-1) cos (4,0)
 sin (5,2) cos (6,0) sin (7,-2) cos (8,0)
 sin (9,3) cos (10,0) sin (11,-3) cos (12,0)
 sin (13,4) cos (14,0) sin (15,-4) cos (16,0)
 sin (17,5) cos (18,0) sin (19,-5) cos (20,0)
 sin (21,6) cos (22,0) sin (23,-6) cos (24,0)
 sin (25,7) cos (26,0) sin (27,-7) cos (28,0)
 sin (29,8) cos (30,0) sin (31,-8) cos (32,0)
 sin (33,7) cos (34,0) sin (35,-7) cos (36,0)
 sin (37,6) cos (38,0) sin (39,-6) cos (40,0)
 sin (41,5) cos (42,0) sin (43,-5) cos (44,0)
 sin (45,4) cos (46,0) sin (47,-4) cos (48,0)
 sin (49,3) cos (50,0) sin (51,-3) cos (52,0)
 sin (53,2) cos (54,0) sin (55,-2) cos (56,0);
\end{tikzpicture}
\end{document}

enter image description here

8
  • 3
    Use \foreach and relative coordinates: \tikz[scale=.5] \draw[ultra thick,red,x=0.5cm,y=1cm] (0,0) foreach \i in {1, ..., 8}{ sin ++(1,1) cos ++(1,-1) sin ++(1,-1) cos ++(1,1) }; and \tikz[scale=.5] \draw[ultra thick,blue,x=0.5cm] (0,0) foreach \i in {1, ..., 14} { [y=1cm + 1cm*((\i < 9) ? (\i-1) : (15-\i))] sin ++(1,1) cos ++(1,-1) sin ++(1,-1) cos ++(1,1) }; (this one could also be solved by chaing yscale or the y value of each coordinate). Commented Apr 19, 2023 at 13:49
  • @– Qrrbrbirlbel Thank you for your code.
    – Hany
    Commented Apr 20, 2023 at 5:41
  • @Hany Are you asking about the beat phenomena ? en.wikipedia.org/wiki/Beat_(acoustics)
    – Black Mild
    Commented Jun 5, 2023 at 11:18
  • @ Black Mild Is there a TikZ code for it!
    – Hany
    Commented Jun 5, 2023 at 14:03
  • The TikZ code would be to plot the function. Your blue wave would be something like 5sin(x)sin(30x). But you've not helped us really understand what you're doing. You've told us that most of our suggestions won't help for your actual plots, but you've not told us why or what those plots are. And Qrrbrbirlbel's gives your red and blue, but then you completely ignored the foreach in your answer.
    – Teepeemm
    Commented Jun 5, 2023 at 14:44

3 Answers 3

6

enter image description here

Edit:

  • To my opinion, composing (smooth) functions as follows from showed examples, with some universal scalable "building boxes", is dead end ... after few building boxes you will stack with problems, not visible now, for example how to connect them in smooth function.
  • Better is to use plot function, accordingly define function and their domain. For example:
\documentclass{article}
\usepackage{tikz}


\begin{document}
    \begin{tikzpicture}[
trig format=rad,
ultra thick, red, 
domain=-8*pi:8*pi, 
samples=601             ]
%
\draw   plot (\x/4, {sin(2*\x)});
    \end{tikzpicture}

\bigskip
    \begin{tikzpicture}[
trig format=rad,
ultra thick, blue,
domain=-8*pi:0,
samples=801             ]
%
\draw   plot (\x/4,{((8*pi-abs(\x))/(2*pi))*sin(2*\x)});
    \end{tikzpicture}

\bigskip
    \begin{tikzpicture}[
trig format=rad,
ultra thick, blue,
domain=-8*pi:8*pi,
samples=801             ]
%
\draw   plot (\x/4,{(sin(\x/4))*(sin(4*\x))});
    \end{tikzpicture}
\end{document}

Addendum (1):
In the case, that you persist to stick to function chunks ordered in some chain, you can define chunks as scalable "boulding boxes" (BB) and then positioning them in image by use of some loop or chains library.

Is this what you after?

An example of chain of a simple BB ordered by loop:

\documentclass{article}
\usepackage{tikz}

\begin{document}
    \begin{tikzpicture}[
BB/.style = {draw=teal, fill=teal!30, 
             minimum width=1cm, minimum height=\h cm,
            }
                        ]
\foreach \i in {-3, -2,...,3}
{
\pgfmathsetmacro{\h}{int(3-abs(\i))}        % calculation of function
    \node[BB=\h] at (\i,0) {$\i$};
}
    \end{tikzpicture}
\end{document}

enter image description here

Addendum (2):
One more example, by using `chains˛library:

\documentclass{article}
\usepackage{xifthen}
\usepackage{tikz}
\usetikzlibrary{chains}

\begin{document}
    \begin{tikzpicture}[
node distance=0pt,
  start chain = going right,
BB+/.style = {on chain,
             minimum width=1cm, minimum height=\h cm,
    append after command={\pgfextra{\let\LN\tikzlastnode
                          \draw[ultra thick,line join=round,line cap=round,red] 
                                (\LN.west) -- (\LN.north) -- (\LN.east);}% end \pgfextra
                         }% end append after command
            },
BB-/.style = {on chain,
             minimum width=1cm, minimum height=\h cm,
    append after command={\pgfextra{\let\LN\tikzlastnode
                          \draw[ultra thick,line join=round,line cap=roundred,blue] 
                                (\LN.west) -- (\LN.south) -- (\LN.east);}% end \pgfextra
                         }% end append after command
            }
                        ]
\foreach \i in {-3, -2,...,3}
{
\pgfmathsetmacro{\h}{2*int(3-abs(\i))}        % calculation of function
\ifthenelse{\isodd{\i}}
        {\node[BB+] {};}
        {\node[BB-] {};}
}
    \end{tikzpicture}
\end{document}

enter image description here

3
  • Thank you for your answer. As I pointed out before, this is just an example; I can not use plot for my other drawings. So, as I mentioned in my question, I would like to say (previous x + a value, previous y + a value) so that the code can be applied for other drawings.
    – Hany
    Commented Apr 20, 2023 at 5:46
  • 1
    @Hany, you can not have universal/common node for all your diagrams. Using plots and define functions which you like to draw, is a way to go ...
    – Zarko
    Commented Apr 20, 2023 at 5:58
  • @– Zarko Thank you very much for your time.
    – Hany
    Commented Apr 20, 2023 at 8:07
3

I'm no sure what exactly you are doing in your example, but in principle this kind of stuff can be automated using pgf's \foreach (automatically available when using tikz).

However, normally you'd use the \draw plot command to plot stuff inside TikZ or - if you want a "proper" plot - simply skip to using pgfplots in the first place:

\documentclass{minimal}

\usepackage{tikz}
\usepackage{pgfplots}

\begin{document}
    \begin{tikzpicture}[scale=.5, x=0.5cm, y=1cm, ultra thick, red]
        \draw (0,0)
            \foreach \x in {0,4,...,28}{
                sin ({1 + \x}, 1) cos ({2 + \x}, 0) sin ({3 + \x}, -1) cos ({4 + \x},0)
            }
        ;
    \end{tikzpicture}
    \end{tikzpicture}

    \begin{tikzpicture}
        \draw[domain=0:12,samples=100] plot ({\x}, {sin(deg(\x))})     ;
    \end{tikzpicture}

    \begin{tikzpicture}
        \begin{axis}
            \addplot[samples=100] {sin(deg(x))};
        \end{axis}
    \end{tikzpicture}
\end{document}
13
  • Thank you for your answer. As I said, this is just an example to be applied for other drawings. Unfortunately \addplot can not be used in my other drawings. How can I use your \foreach solution to draw the blue wave?
    – Hany
    Commented Apr 19, 2023 at 13:12
  • @Hany what's the definition of the blue wave's shape?
    – Raven
    Commented Apr 19, 2023 at 13:13
  • The blue wave in my question.
    – Hany
    Commented Apr 19, 2023 at 13:14
  • @Hany yeah that much is clear. But you must have had some sort of formula to get those points. what's that formula?
    – Raven
    Commented Apr 19, 2023 at 13:15
  • @– Raven I just want to modify my code for using your solution. Or to use your idea to be applied in other drawings. Thank you.
    – Hany
    Commented Apr 19, 2023 at 13:16
0

Inspired by the code of Qrrbrbirlbel, I came up with this solution; which can be used for scaling the sine wave as wanted.

\documentclass[12pt,a4paper]{article}
\usepackage{tikz}
\begin{document}
\pgfmathsetmacro{\xinc}{1.}
\pgfmathsetmacro{\yinc}{1.5}
\begin{tikzpicture}[scale=.5, transform shape]
\draw[ultra thick,red,x=0.5cm,y=1cm] (0,0)
 sin ++(\xinc,\yinc) cos ++(\xinc,-\yinc) sin ++(\xinc,-\yinc) cos ++(\xinc,\yinc)
 sin ++(\xinc,\yinc) cos ++(\xinc,-\yinc) sin ++(\xinc,-\yinc) cos ++(\xinc,\yinc)
 sin ++(\xinc,\yinc) cos ++(\xinc,-\yinc) sin ++(\xinc,-\yinc) cos ++(\xinc,\yinc)
 sin ++(\xinc,\yinc) cos ++(\xinc,-\yinc) sin ++(\xinc,-\yinc) cos ++(\xinc,\yinc)
 sin ++(\xinc,\yinc) cos ++(\xinc,-\yinc) sin ++(\xinc,-\yinc) cos ++(\xinc,\yinc)
 sin ++(\xinc,\yinc) cos ++(\xinc,-\yinc) sin ++(\xinc,-\yinc) cos ++(\xinc,\yinc)
 sin ++(\xinc,\yinc) cos ++(\xinc,-\yinc) sin ++(\xinc,-\yinc) cos ++(\xinc,\yinc)
 sin ++(\xinc,\yinc) cos ++(\xinc,-\yinc) sin ++(\xinc,-\yinc) cos ++(\xinc,\yinc)
 sin ++(\xinc,\yinc) cos ++(\xinc,-\yinc) sin ++(\xinc,-\yinc) cos ++(\xinc,\yinc)
 sin ++(\xinc,\yinc) cos ++(\xinc,-\yinc) sin ++(\xinc,-\yinc) cos ++(\xinc,\yinc)
 sin ++(\xinc,\yinc) cos ++(\xinc,-\yinc) sin ++(\xinc,-\yinc) cos ++(\xinc,\yinc)
 sin ++(\xinc,\yinc) cos ++(\xinc,-\yinc) sin ++(\xinc,-\yinc) cos ++(\xinc,\yinc)
 sin ++(\xinc,\yinc) cos ++(\xinc,-\yinc) sin ++(\xinc,-\yinc) cos ++(\xinc,\yinc)
 sin ++(\xinc,\yinc) cos ++(\xinc,-\yinc) sin ++(\xinc,-\yinc) cos ++(\xinc,\yinc)
;
\end{tikzpicture}
\vskip 1cm
\begin{tikzpicture}[scale=.5, transform shape]
\draw[ultra thick,blue,x=0.5cm,y=1cm] (0,0)
 sin ++(\xinc,\yinc*1) cos ++(\xinc,-\yinc*1) sin ++(\xinc,-\yinc*1) cos ++(\xinc,\yinc*1)
 sin ++(\xinc,\yinc*2) cos ++(\xinc,-\yinc*2) sin ++(\xinc,-\yinc*2) cos ++(\xinc,\yinc*2)
 sin ++(\xinc,\yinc*3) cos ++(\xinc,-\yinc*3) sin ++(\xinc,-\yinc*3) cos ++(\xinc,\yinc*3)
 sin ++(\xinc,\yinc*4) cos ++(\xinc,-\yinc*4) sin ++(\xinc,-\yinc*4) cos ++(\xinc,\yinc*4)
 sin ++(\xinc,\yinc*5) cos ++(\xinc,-\yinc*5) sin ++(\xinc,-\yinc*5) cos ++(\xinc,\yinc*5)
 sin ++(\xinc,\yinc*6) cos ++(\xinc,-\yinc*6) sin ++(\xinc,-\yinc*6) cos ++(\xinc,\yinc*6)
 sin ++(\xinc,\yinc*7) cos ++(\xinc,-\yinc*7) sin ++(\xinc,-\yinc*7) cos ++(\xinc,\yinc*7)
 sin ++(\xinc,\yinc*8) cos ++(\xinc,-\yinc*8) sin ++(\xinc,-\yinc*8) cos ++(\xinc,\yinc*8)
 sin ++(\xinc,\yinc*7) cos ++(\xinc,-\yinc*7) sin ++(\xinc,-\yinc*7) cos ++(\xinc,\yinc*7)
 sin ++(\xinc,\yinc*6) cos ++(\xinc,-\yinc*6) sin ++(\xinc,-\yinc*6) cos ++(\xinc,\yinc*6)
 sin ++(\xinc,\yinc*5) cos ++(\xinc,-\yinc*5) sin ++(\xinc,-\yinc*5) cos ++(\xinc,\yinc*5)
 sin ++(\xinc,\yinc*4) cos ++(\xinc,-\yinc*4) sin ++(\xinc,-\yinc*4) cos ++(\xinc,\yinc*4)
 sin ++(\xinc,\yinc*3) cos ++(\xinc,-\yinc*3) sin ++(\xinc,-\yinc*3) cos ++(\xinc,\yinc*3)
 sin ++(\xinc,\yinc*2) cos ++(\xinc,-\yinc*2) sin ++(\xinc,-\yinc*2) cos ++(\xinc,\yinc*2)
 sin ++(\xinc,\yinc*1) cos ++(\xinc,-\yinc*1) sin ++(\xinc,-\yinc*1) cos ++(\xinc,\yinc*1)
;
\end{tikzpicture}
\end{document}

enter image description here

4
  • 1
    In both other answers are code for producing showed image significantly shorter, less prone to errors ... Apparently you like to invent a wheel again ...
    – Zarko
    Commented Apr 21, 2023 at 6:11
  • @– Zarko Thank you very much for your addendums. No, I do not like to invent a wheel again. As I said before, I would like to come up with a short code that could be applied for other drawings, as well as for the sine wave; which is only an example for what I have in mind.
    – Hany
    Commented Apr 21, 2023 at 6:18
  • @Hany But you should be able to use your relative coordinates approach with a \foreach loop to greatly simplify things. Or is your actual example too complicated for that. (And if you leave off the - from the @, then SE can properly notify the people. It will offer to autocomplete the name if you're doing it correctly.)
    – Teepeemm
    Commented Apr 21, 2023 at 11:26
  • @Teepeemm Thank you for your comment.
    – Hany
    Commented Apr 21, 2023 at 13:36

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .