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

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

TikZ (\foreach): how to join function points

Post by Sundial »

One more question on \foreach statement of TikZ, described in attachments. Cheers

Code: Select all

% --------- jp.tex --------- Apr.27,2015 ---------
%
\documentclass{article}
\usepackage{tikz}   
% ---
\usepackage[active,tightpage]{preview}
\PreviewEnvironment{center}
\setlength\PreviewBorder{10pt}%
% ---
\tikzstyle{load}   = [thick,-latex]
\tikzstyle{stress} = [-latex]
\tikzstyle{dim}    = [latex-latex]
\tikzstyle{axis}   = [-latex,blue!55]
% ---
\tikzstyle{two}=[x={(1cm,0cm)},y={(0cm,1cm)}] % axes scale, 1cm 


\begin{document}
\begin{center}

% =================== Start Picture =================
  \begin{tikzpicture} [two]

% -------- a) Draw Axes, blue dashed ------
    \coordinate (O) at (0,0);
     \draw[axis,two,dashed] (O) -- ++(7,0) node[right] {$x$};
     \draw[axis,two,dashed] (O) -- ++(0,4) node[above] {$y$};
%
% -------- b) Draw structural elements
    \draw[ultra thick] (0,2) -- ++(4,0);  % beam [starting point to increment]
% -------- b2) Draw grid (if any)
  \draw [step=0.5,blue,dotted] (0,0) grid (4,1.5);
% -------------------------------------------------          
%
% ------------ c2) Concentrated load
\begin{scope}[xshift=2cm, yshift=2cm, rotate=0, scale=0.5]
\draw[load,blue] (0,2) -- ++(0,-2) node[pos=0.0,right,xshift=0cm] {$P$};
\end{scope}

% x-axis dimensions [dx=4.0]
\begin{scope}[xshift=0cm, yshift=-0.5cm, rotate=0, scale=1] 
    \draw[dim] (0,0) -- ++(4,0) node[midway,below] {$\ell$}; % beam dimension
    \draw (4,-0.2) -- ++(0,0.4); 
    \draw (0,-0.2) -- ++(0,0.4); 
\end{scope}    
% -------- f) Draw Function (elastica)
\begin{scope}[xshift=0cm, yshift=1cm, rotate=0, scale=1]
    \foreach \x in {0,0.05,...,2.05} {%     
    \draw [color=magenta!55] (\x,-\x/2+\x*\x*\x/24) -- (\x,0);}
    \draw [color=magenta!55] (0,0) -- (2,0); 
\end{scope}
% Function Captions (2 lines)
 \begin{scope}[xshift=2.1cm, yshift=0.8cm]
 \draw node[right]{$EI\cdot\eta=-\frac{P\ell^2}{16}x + \frac{P}{12}x^3\,(\rm elastica)$};
 \end{scope}
% 
 \begin{scope}[xshift=4cm, yshift=0.3cm]
 \draw node[right]{$0 \leq x \leq \frac{\ell}{2}$};
 \end{scope}
%
\end{tikzpicture} %
% ========================= End Picture =================
% ----------------------------------------------------------
\end{center}
\end{document}
% ---- EOF: jp.tex ----
Attachments
09.png
09.png (19.66 KiB) Viewed 8566 times

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org
LaTeX Beginner's Guide LaTeX Cookbook LaTeX TikZ graphics TikZによるLaTeXグラフィックス
alainremillard
Posts: 45
Joined: Fri Mar 16, 2012 8:22 pm

TikZ (\foreach): how to join function points

Post by alainremillard »

I could think of two solutions. First create a temporary point before the \foreach loop and link it with the current point, as below.

Code: Select all

% -------- f) Draw Function (elastica)
\begin{scope}[xshift=0cm, yshift=1cm, rotate=0, scale=1]
	\coordinate (temp) at (0,0);
    \foreach \x in {0,0.05,...,2.05} {%     
    \draw [color=magenta!55] (\x,-\x/2+\x*\x*\x/24) -- (\x,0);
    \draw (temp) -- (\x,-\x/2+\x*\x*\x/24) coordinate (temp);}
    \draw [color=magenta!55] (0,0) -- (2,0); 
\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.

Code: Select all

% -------- f) Draw Function (elastica)
\begin{scope}[xshift=0cm, yshift=1cm, rotate=0, scale=1]
    \foreach \x in {0,0.05,...,2.05} {%     
    \draw [color=magenta!55] (\x,-\x/2+\x*\x*\x/24) -- (\x,0);}
    \draw [color=magenta!55] (0,0) -- (2,0);
    \draw [domain=0:2.05,samples=50] plot (\x,{-\x/2+\x*\x*\x/24});
\end{scope}
Have a nice day
Alain Rémillard
Sundial
Posts: 57
Joined: Thu Apr 19, 2012 7:28 pm

TikZ (\foreach): how to join function points

Post by Sundial »

So good, Alain! Chosen your second approach

Code: Select all

% -------- f) Draw Function (elastica)
\begin{scope}[xshift=0cm, yshift=1cm, rotate=0, scale=1]
    \foreach \x in {0,0.05,...,2.05} {%     
    \draw [color=magenta!55] (\x,-\x/2+\x*\x*\x/24) -- (\x,0);}
    \draw [color=magenta!55] (0,0) -- (2,0);
    \draw [color=magenta!55,domain=0:2,samples=50] plot (\x,{-\x/2+\x*\x*\x/24});
\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 8525 times
alainremillard
Posts: 45
Joined: Fri Mar 16, 2012 8:22 pm

TikZ (\foreach): how to join function points

Post by alainremillard »

Not sure about what you meant, but could this solve your problem.

Code: Select all

% -------- f) Draw Function (elastica)
\begin{scope}[xshift=0cm, yshift=1cm, rotate=0, scale=1]
    \draw [color=magenta!55,fill=gray!55,domain=0:2,samples=50] plot (\x,{-\x/2+\x*\x*\x/24}) -- (2,0) -- (0,0);
    \foreach \x in {0,0.05,...,2.05} {%    
    \draw [color=magenta!55] (\x,-\x/2+\x*\x*\x/24) -- (\x,0);}
\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

TikZ (\foreach): how to join function points

Post by Sundial »

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.

Code: Select all

% --------- tikz3.tex --------- Apr.29,2015 --------- Ended Apr.29 ---
%  
\documentclass[10pt]{article}
\usepackage{tikz}   
% ---
\usepackage[active,tightpage]{preview}
\PreviewEnvironment{center}
\setlength\PreviewBorder{10pt}%
% ---
\tikzstyle{load}   = [thick,-latex]
\tikzstyle{stress} = [-latex]
\tikzstyle{dim}    = [latex-latex]
\tikzstyle{axis}   = [-latex,blue!55]
% ---
\tikzstyle{two}=[x={(1cm,0cm)},y={(0cm,1cm)}] % axes scale, 1cm 


\begin{document}
\begin{center}

% =================== Start Picture =================
  \begin{tikzpicture} [two]

% ----- a) Macro for Axes Plot, blue dashed, elements and grid (same scale) ------
\begin{scope}[xshift=0cm, yshift=0cm, rotate=0, scale=0.4]
    \coordinate (O) at (0,0);
     \draw[axis,two,dashed] (O) -- ++(17,0) node[right] {$x$};
     \draw[axis,two,dashed] (O) -- ++(0,31.5) node[above] {$y$};
% -------- b) Draw structural elements
   \draw[thick] (0,0) -- ++(0,30);  % 1st column [starting point to increment]
   \draw[thick] (12,0) -- ++(0,30); % 3rd column
   \draw[thick] (5,0) -- ++(0,6); % 2nd column (part a)
   \draw[thick] (5,9) -- ++(0,15); % 2nd column (part b)
   \draw[thick] (5,27) -- ++(0,3); % 2nd column (part c)
   \draw[thick] (14,9) -- ++(0,12); % 4th column (ext.wall)   
%   
   \draw[thick] (0,3) -- ++(12,0); % storey #1
   \draw[thick] (0,6) -- ++(12,0); % storey #2
%   
   \draw[thick] (0,9) -- ++(14,0); % storey #3
   \draw[thick] (0,12) -- ++(14,0); % storey #4
   \draw[thick] (0,15) -- ++(14,0); % storey #5
   \draw[thick] (0,18) -- ++(14,0); % storey #6
   \draw[thick] (0,21) -- ++(14,0); % storey #7
%   
   \draw[thick] (0,24) -- ++(12,0); % storey #8
   \draw[thick] (0,27) -- ++(12,0); % storey #9
   \draw[thick] (0,30) -- ++(12,0); % storey #10
%   
% -------- b2) Draw grid 
  \draw [step=1,blue,dotted] (0,0) grid (14,30);
% -------------------------------------------------          
%
\end{scope}

% ---- Horizontal Loads applied to structure nodes
\begin{scope}[xshift=0cm, yshift=0cm, rotate=0, scale=0.4]
\foreach \z in {3,6,...,27} {%
\draw[load,blue] (-2,\z) -- ++(2,0) node[pos=0.4,above,xshift=0cm] {$0.50\,t$};}
% last load, upper storey
\draw[load,blue] (-2,30) -- ++(2,0) node[pos=0.4,above,xshift=0cm] {$0.25\,t$};
\end{scope}
%

% ---- Vertical Dimensioning --------
% x-axis rotation [dx=3] 
\begin{scope}[xshift=6.40cm, yshift=0cm, rotate=90, scale=0.4] 
   \foreach \z in {0,3,...,27} {%
    \draw[dim] (\z,0)  -- ++(3.0,0) node[sloped,midway,below,rotate=90] {$3.0$}; % col. dimension
    \draw (\z,-0.3) -- ++(0,0.6); 
    }
    \draw (30,-0.3) -- ++(0,0.6); % upper tick
\end{scope}    

% ---- Horizontal Dimensioning --------
\begin{scope}[xshift=0cm, yshift=0cm, rotate=0, scale=0.4] 
%1st span
 \draw[dim] (0,-1)  -- ++(5.0,0) node[sloped,midway,below] {$5.0$};
  \draw (0,-1.3) -- ++(0,0.6);
  \draw (5,-1.3) -- ++(0,0.6); 
%2nd span
  \draw[dim] (5,-1)  -- ++(7.0,0) node[sloped,midway,below] {$7.0$};
  \draw (12,-1.3) -- ++(0,0.6); 
%3sd span
  \draw[dim] (12,-1)  -- ++(2.0,0) node[sloped,midway,below] {$2.0$};
  \draw (14,-1.3) -- ++(0,0.6);
\end{scope}   

% ------ Soil anchors ---------------
% ------- Fixed End - #1
\begin{scope}[xshift=0cm, yshift=0cm, rotate=0, scale=0.4] 
% bar
\draw[thick] (-0.7,0) -- (0.7,0);
% Ticks
 \draw (-0.4,-0.4) -- (0,0);
 \draw (-0.7,-0.4) -- (-0.3,0);
 \draw (-0.1,-0.4) -- (0.3,0);
 \draw (0.7,-0.4) -- (0.3,0);
% 
 \draw (0.4,-0.4) -- (0,0);
 \draw (0,-0.4) -- (-0.4,0);
\end{scope}

% ------- Fixed End - #3
\begin{scope}[xshift=4.8cm, yshift=0cm, rotate=0, scale=0.4]%4.8cm, due to scale factor 0.4 
% bar
\draw[thick] (-0.7,0) -- (0.7,0);
% Ticks
 \draw (-0.4,-0.4) -- (0,0);
 \draw (-0.7,-0.4) -- (-0.3,0);
 \draw (-0.1,-0.4) -- (0.3,0);
 \draw (0.7,-0.4) -- (0.3,0);
% 
 \draw (0.4,-0.4) -- (0,0);
 \draw (0,-0.4) -- (-0.4,0);
\end{scope}
%
% ------- Fixed End - #2
\begin{scope}[xshift=2cm, yshift=0cm, rotate=0, scale=0.4] %2cm, due to scale factor 0.4
% bar
\draw[thick] (-0.7,0) -- (0.7,0);
% Ticks
 \draw (-0.4,-0.4) -- (0,0);
 \draw (-0.7,-0.4) -- (-0.3,0);
 \draw (-0.1,-0.4) -- (0.3,0);
 \draw (0.7,-0.4) -- (0.3,0);
% 
 \draw (0.4,-0.4) -- (0,0);
 \draw (0,-0.4) -- (-0.4,0);
\end{scope}
%

\end{tikzpicture} %
% ========================= End Picture =================
% ----------------------------------------------------------
\end{center}
\end{document}
% ---- EOF: tikz3.tex ----


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

TikZ (\foreach): how to join function points

Post by Sundial »

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: 10290
Joined: Mon Mar 10, 2008 9:44 pm

Re: TikZ (\foreach): how to join function points

Post by Stefan Kottwitz »

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
LaTeX.org admin
Post Reply