LaTeX forum ⇒ Graphics, Figures & TablesTikZ (\foreach): how to join function points

Information and discussion about graphics, figures & tables in LaTeX documents.
Sundial
Posts: 57
Joined: Thu Apr 19, 2012 7:28 pm

TikZ (\foreach): how to join function points

Postby Sundial » Mon Apr 27, 2015 7:54 pm

One more question on \foreach statement of TikZ, described in attachments. Cheers
  1. % --------- jp.tex --------- Apr.27,2015 ---------
  2. %
  3. \documentclass{article}
  4. \usepackage{tikz}
  5. % ---
  6. \usepackage[active,tightpage]{preview}
  7. \PreviewEnvironment{center}
  8. \setlength\PreviewBorder{10pt}%
  9. % ---
  10. \tikzstyle{load} = [thick,-latex]
  11. \tikzstyle{stress} = [-latex]
  12. \tikzstyle{dim} = [latex-latex]
  13. \tikzstyle{axis} = [-latex,blue!55]
  14. % ---
  15. \tikzstyle{two}=[x={(1cm,0cm)},y={(0cm,1cm)}] % axes scale, 1cm
  16.  
  17.  
  18. \begin{document}
  19. \begin{center}
  20.  
  21. % =================== Start Picture =================
  22. \begin{tikzpicture} [two]
  23.  
  24. % -------- a) Draw Axes, blue dashed ------
  25. \coordinate (O) at (0,0);
  26. \draw[axis,two,dashed] (O) -- ++(7,0) node[right] {$x$};
  27. \draw[axis,two,dashed] (O) -- ++(0,4) node[above] {$y$};
  28. %
  29. % -------- b) Draw structural elements
  30. \draw[ultra thick] (0,2) -- ++(4,0); % beam [starting point to increment]
  31. % -------- b2) Draw grid (if any)
  32. \draw [step=0.5,blue,dotted] (0,0) grid (4,1.5);
  33. % -------------------------------------------------
  34. %
  35. % ------------ c2) Concentrated load
  36. \begin{scope}[xshift=2cm, yshift=2cm, rotate=0, scale=0.5]
  37. \draw[load,blue] (0,2) -- ++(0,-2) node[pos=0.0,right,xshift=0cm] {$P$};
  38. \end{scope}
  39.  
  40. % x-axis dimensions [dx=4.0]
  41. \begin{scope}[xshift=0cm, yshift=-0.5cm, rotate=0, scale=1]
  42. \draw[dim] (0,0) -- ++(4,0) node[midway,below] {$\ell$}; % beam dimension
  43. \draw (4,-0.2) -- ++(0,0.4);
  44. \draw (0,-0.2) -- ++(0,0.4);
  45. \end{scope}
  46. % -------- f) Draw Function (elastica)
  47. \begin{scope}[xshift=0cm, yshift=1cm, rotate=0, scale=1]
  48. \foreach \x in {0,0.05,...,2.05} {%
  49. \draw [color=magenta!55] (\x,-\x/2+\x*\x*\x/24) -- (\x,0);}
  50. \draw [color=magenta!55] (0,0) -- (2,0);
  51. \end{scope}
  52. % Function Captions (2 lines)
  53. \begin{scope}[xshift=2.1cm, yshift=0.8cm]
  54. \draw node[right]{$EI\cdot\eta=-\frac{P\ell^2}{16}x + \frac{P}{12}x^3\,(\rm elastica)$};
  55. \end{scope}
  56. %
  57. \begin{scope}[xshift=4cm, yshift=0.3cm]
  58. \draw node[right]{$0 \leq x \leq \frac{\ell}{2}$};
  59. \end{scope}
  60. %
  61. \end{tikzpicture} %
  62. % ========================= End Picture =================
  63. % ----------------------------------------------------------
  64. \end{center}
  65. \end{document}
  66. % ---- EOF: jp.tex ----
Attachments
09.png
09.png (19.66 KiB) Viewed 2309 times

Tags:

alainremillard
Posts: 45
Joined: Fri Mar 16, 2012 8:22 pm

Postby alainremillard » Tue Apr 28, 2015 2:06 am

I could think of two solutions. First create a temporary point before the \foreach loop and link it with the current point, as below.
  1. % -------- f) Draw Function (elastica)
  2. \begin{scope}[xshift=0cm, yshift=1cm, rotate=0, scale=1]
  3. \coordinate (temp) at (0,0);
  4. \foreach \x in {0,0.05,...,2.05} {%
  5. \draw [color=magenta!55] (\x,-\x/2+\x*\x*\x/24) -- (\x,0);
  6. \draw (temp) -- (\x,-\x/2+\x*\x*\x/24) coordinate (temp);}
  7. \draw [color=magenta!55] (0,0) -- (2,0);
  8. \end{scope}

Second use the plot function since you know the equation. The plot function draw a curve point by point, the samples option controls the number of points used.
  1. % -------- f) Draw Function (elastica)
  2. \begin{scope}[xshift=0cm, yshift=1cm, rotate=0, scale=1]
  3. \foreach \x in {0,0.05,...,2.05} {%
  4. \draw [color=magenta!55] (\x,-\x/2+\x*\x*\x/24) -- (\x,0);}
  5. \draw [color=magenta!55] (0,0) -- (2,0);
  6. \draw [domain=0:2.05,samples=50] plot (\x,{-\x/2+\x*\x*\x/24});
  7. \end{scope}

Have a nice day
Alain Rémillard

Sundial
Posts: 57
Joined: Thu Apr 19, 2012 7:28 pm

Postby Sundial » Tue Apr 28, 2015 8:31 am

So good, Alain! Chosen your second approach
  1. % -------- f) Draw Function (elastica)
  2. \begin{scope}[xshift=0cm, yshift=1cm, rotate=0, scale=1]
  3. \foreach \x in {0,0.05,...,2.05} {%
  4. \draw [color=magenta!55] (\x,-\x/2+\x*\x*\x/24) -- (\x,0);}
  5. \draw [color=magenta!55] (0,0) -- (2,0);
  6. \draw [color=magenta!55,domain=0:2,samples=50] plot (\x,{-\x/2+\x*\x*\x/24});
  7. \end{scope}
and the result (see att'd screenshot )matches what I was looking for. Thank you very much.
A slight more drift, please. May I have the chance to fill, i.e. in gray!55, all trapezoidal pieces? Cheers
Attachments
11.png
11.png (8.97 KiB) Viewed 2268 times

alainremillard
Posts: 45
Joined: Fri Mar 16, 2012 8:22 pm

Postby alainremillard » Wed Apr 29, 2015 5:46 pm

Not sure about what you meant, but could this solve your problem.
  1. % -------- f) Draw Function (elastica)
  2. \begin{scope}[xshift=0cm, yshift=1cm, rotate=0, scale=1]
  3. \draw [color=magenta!55,fill=gray!55,domain=0:2,samples=50] plot (\x,{-\x/2+\x*\x*\x/24}) -- (2,0) -- (0,0);
  4. \foreach \x in {0,0.05,...,2.05} {%
  5. \draw [color=magenta!55] (\x,-\x/2+\x*\x*\x/24) -- (\x,0);}
  6. \end{scope}

You can use the plot function as a side of a polygon. The foreach loop need to be after so it will be drawn over the polygon.

Have a nice day
Alain Rémillard

Sundial
Posts: 57
Joined: Thu Apr 19, 2012 7:28 pm

Postby Sundial » Wed Apr 29, 2015 7:47 pm

Fantastic drift, Alain!
Maybe you are minding why I'm so unskilled in TikZ statements, the reason is time of learning. Yes, just faced the magic world of \foreach command only a dozen of days ago.
Please, look at next application carried out today [two more utilizations of \foreach, dimensioning columns and horizontal loads, recursive routine applied] and, if you wish, let me know how to obtain better refinements. Thanks a lot and cheers.

  1. % --------- tikz3.tex --------- Apr.29,2015 --------- Ended Apr.29 ---
  2. %
  3. \documentclass[10pt]{article}
  4. \usepackage{tikz}
  5. % ---
  6. \usepackage[active,tightpage]{preview}
  7. \PreviewEnvironment{center}
  8. \setlength\PreviewBorder{10pt}%
  9. % ---
  10. \tikzstyle{load} = [thick,-latex]
  11. \tikzstyle{stress} = [-latex]
  12. \tikzstyle{dim} = [latex-latex]
  13. \tikzstyle{axis} = [-latex,blue!55]
  14. % ---
  15. \tikzstyle{two}=[x={(1cm,0cm)},y={(0cm,1cm)}] % axes scale, 1cm
  16.  
  17.  
  18. \begin{document}
  19. \begin{center}
  20.  
  21. % =================== Start Picture =================
  22. \begin{tikzpicture} [two]
  23.  
  24. % ----- a) Macro for Axes Plot, blue dashed, elements and grid (same scale) ------
  25. \begin{scope}[xshift=0cm, yshift=0cm, rotate=0, scale=0.4]
  26. \coordinate (O) at (0,0);
  27. \draw[axis,two,dashed] (O) -- ++(17,0) node[right] {$x$};
  28. \draw[axis,two,dashed] (O) -- ++(0,31.5) node[above] {$y$};
  29. % -------- b) Draw structural elements
  30. \draw[thick] (0,0) -- ++(0,30); % 1st column [starting point to increment]
  31. \draw[thick] (12,0) -- ++(0,30); % 3rd column
  32. \draw[thick] (5,0) -- ++(0,6); % 2nd column (part a)
  33. \draw[thick] (5,9) -- ++(0,15); % 2nd column (part b)
  34. \draw[thick] (5,27) -- ++(0,3); % 2nd column (part c)
  35. \draw[thick] (14,9) -- ++(0,12); % 4th column (ext.wall)
  36. %
  37. \draw[thick] (0,3) -- ++(12,0); % storey #1
  38. \draw[thick] (0,6) -- ++(12,0); % storey #2
  39. %
  40. \draw[thick] (0,9) -- ++(14,0); % storey #3
  41. \draw[thick] (0,12) -- ++(14,0); % storey #4
  42. \draw[thick] (0,15) -- ++(14,0); % storey #5
  43. \draw[thick] (0,18) -- ++(14,0); % storey #6
  44. \draw[thick] (0,21) -- ++(14,0); % storey #7
  45. %
  46. \draw[thick] (0,24) -- ++(12,0); % storey #8
  47. \draw[thick] (0,27) -- ++(12,0); % storey #9
  48. \draw[thick] (0,30) -- ++(12,0); % storey #10
  49. %
  50. % -------- b2) Draw grid
  51. \draw [step=1,blue,dotted] (0,0) grid (14,30);
  52. % -------------------------------------------------
  53. %
  54. \end{scope}
  55.  
  56. % ---- Horizontal Loads applied to structure nodes
  57. \begin{scope}[xshift=0cm, yshift=0cm, rotate=0, scale=0.4]
  58. \foreach \z in {3,6,...,27} {%
  59. \draw[load,blue] (-2,\z) -- ++(2,0) node[pos=0.4,above,xshift=0cm] {$0.50\,t$};}
  60. % last load, upper storey
  61. \draw[load,blue] (-2,30) -- ++(2,0) node[pos=0.4,above,xshift=0cm] {$0.25\,t$};
  62. \end{scope}
  63. %
  64.  
  65. % ---- Vertical Dimensioning --------
  66. % x-axis rotation [dx=3]
  67. \begin{scope}[xshift=6.40cm, yshift=0cm, rotate=90, scale=0.4]
  68. \foreach \z in {0,3,...,27} {%
  69. \draw[dim] (\z,0) -- ++(3.0,0) node[sloped,midway,below,rotate=90] {$3.0$}; % col. dimension
  70. \draw (\z,-0.3) -- ++(0,0.6);
  71. }
  72. \draw (30,-0.3) -- ++(0,0.6); % upper tick
  73. \end{scope}
  74.  
  75. % ---- Horizontal Dimensioning --------
  76. \begin{scope}[xshift=0cm, yshift=0cm, rotate=0, scale=0.4]
  77. %1st span
  78. \draw[dim] (0,-1) -- ++(5.0,0) node[sloped,midway,below] {$5.0$};
  79. \draw (0,-1.3) -- ++(0,0.6);
  80. \draw (5,-1.3) -- ++(0,0.6);
  81. %2nd span
  82. \draw[dim] (5,-1) -- ++(7.0,0) node[sloped,midway,below] {$7.0$};
  83. \draw (12,-1.3) -- ++(0,0.6);
  84. %3sd span
  85. \draw[dim] (12,-1) -- ++(2.0,0) node[sloped,midway,below] {$2.0$};
  86. \draw (14,-1.3) -- ++(0,0.6);
  87. \end{scope}
  88.  
  89. % ------ Soil anchors ---------------
  90. % ------- Fixed End - #1
  91. \begin{scope}[xshift=0cm, yshift=0cm, rotate=0, scale=0.4]
  92. % bar
  93. \draw[thick] (-0.7,0) -- (0.7,0);
  94. % Ticks
  95. \draw (-0.4,-0.4) -- (0,0);
  96. \draw (-0.7,-0.4) -- (-0.3,0);
  97. \draw (-0.1,-0.4) -- (0.3,0);
  98. \draw (0.7,-0.4) -- (0.3,0);
  99. %
  100. \draw (0.4,-0.4) -- (0,0);
  101. \draw (0,-0.4) -- (-0.4,0);
  102. \end{scope}
  103.  
  104. % ------- Fixed End - #3
  105. \begin{scope}[xshift=4.8cm, yshift=0cm, rotate=0, scale=0.4]%4.8cm, due to scale factor 0.4
  106. % bar
  107. \draw[thick] (-0.7,0) -- (0.7,0);
  108. % Ticks
  109. \draw (-0.4,-0.4) -- (0,0);
  110. \draw (-0.7,-0.4) -- (-0.3,0);
  111. \draw (-0.1,-0.4) -- (0.3,0);
  112. \draw (0.7,-0.4) -- (0.3,0);
  113. %
  114. \draw (0.4,-0.4) -- (0,0);
  115. \draw (0,-0.4) -- (-0.4,0);
  116. \end{scope}
  117. %
  118. % ------- Fixed End - #2
  119. \begin{scope}[xshift=2cm, yshift=0cm, rotate=0, scale=0.4] %2cm, due to scale factor 0.4
  120. % bar
  121. \draw[thick] (-0.7,0) -- (0.7,0);
  122. % Ticks
  123. \draw (-0.4,-0.4) -- (0,0);
  124. \draw (-0.7,-0.4) -- (-0.3,0);
  125. \draw (-0.1,-0.4) -- (0.3,0);
  126. \draw (0.7,-0.4) -- (0.3,0);
  127. %
  128. \draw (0.4,-0.4) -- (0,0);
  129. \draw (0,-0.4) -- (-0.4,0);
  130. \end{scope}
  131. %
  132.  
  133. \end{tikzpicture} %
  134. % ========================= End Picture =================
  135. % ----------------------------------------------------------
  136. \end{center}
  137. \end{document}
  138. % ---- EOF: tikz3.tex ----
  139.  
  140.  

Sundial
Posts: 57
Joined: Thu Apr 19, 2012 7:28 pm

Postby Sundial » Sun Jul 05, 2015 10:15 am

Off Topic.
I've just posted (a couple of days ago) one contribution regarding this subject to TEXample.net, but my suspicion is that the platform has been closed since longtime, being last uploading dated July 2014, one year ago. Isn't? Cheers

User avatar
Stefan Kottwitz
Site Admin
Posts: 8596
Joined: Mon Mar 10, 2008 9:44 pm
Location: Hamburg, Germany
Contact:

Postby Stefan Kottwitz » Sun Jul 05, 2015 2:29 pm

The TeXample.net site is not closed. It's just that I did not have enough time last months. Main reason are my kids, work, and finally the LaTeX book I currently write, so my free time is a bit limited. I spend the remaining time on web forums like here, not yet again on (static) galleries, but I will do. I don't get many examples, it was often the case that I chose examples seen in web forums. I check the googlemail address from time to time, but not often, as it's not my primary one and I get a lot of spam there. Sorry that I did not respond yet.

Stefan
Site admin


Return to “Graphics, Figures & Tables”

Who is online

Users browsing this forum: No registered users and 4 guests