Example: Arithmetic mean as center of mass

Published 2014-05-18 | Author: Gonzalo Medina

Given a list of n positive integers the Balance environment produces an illustration of the arithmetic mean of the integers as the center of mass of a system formed by n objects (of equal mass) placed along a solid rod at the positions specified by the list of integers.

This example was created by Gonzalo Medina answering a question of MYaseen208 on TeX.SE.

Download as: [PDF] [TEX]

Arithmetic mean as center of mass

Do you have a question regarding this example, TikZ or LaTeX in general? Just ask in the LaTeX Forum.
Oder frag auf Deutsch auf TeXwelt.de. En français: TeXnique.fr.

% colors for the ruled rectangle
% colors for the fulcrum
% colors for the weights

% A pic path for the weights
  \coordinate (wll) at (-0.4cm,0);
  \coordinate (wlr) at (0.4cm,0);
  \coordinate (wul) at (-0.3cm,0.4cm);
  \coordinate (wur) at (0.3cm,0.4cm);
    (wll) -- (wlr) -- (wur) -- (wul) -- cycle;
    (wur) --
    ++(-\Angle:6pt) --
    ++(-75:0.4cm) --
    (wlr) --

% counters to store the number of weights and the sum of
% the given integers

% to perform a scaling along the x-axis (1cm=no scaling)

% the height of the rectangle
% the angle for the 3D effect
% lengths controlling the help lines separation

% the main environment
    \foreach \Valor in {#1}
      \pic at (\Valor,\RectHt) {Peso};

% The ruled rectangle
    \coordinate (ll) at (0,0);%\node at ([xshift=-10pt]ll) {\FulcrumCoord};
    \coordinate (ur) at (\RectWd,\RectHt);
      (ll) rectangle (ur);
      (ur) -- 
      ++(-\Angle:6pt) -- 
      ++(0,-\RectHt) -- 
      ++(-\RectWd,0) -- 
      (ll) --
      ++(\RectWd,0) --
    \foreach \Valor in {1,...,\Steps}
      \draw (\Valor,1) -- ++(0pt,-5pt) node[below] {\Valor};  
    \draw (\RectWd,0) -- ++(-\Angle:6pt);

% The fulcrum
      (\FulcrumCoord,0) --
      ++(240:1) --
      ++(1,0) coordinate (aux) --
      (\FulcrumCoord,0) --
      ++(-\Angle:6pt) --
      ++(-60:1) --
      ++(-1,0) --
      ++(180-\Angle:6pt) --
      ++(1,0) --
    \draw (aux) -- ++(-\Angle:6pt);
    \coordinate (cotaFulc) 
      at (\FulcrumCoord,\RectHt);
      (\FulcrumCoord,0) --
        node[above=-2pt] {\pgfmathprintnumber[precision=1]{\FulcrumCoord}};

% the command to draw help lines
  (\FulcrumCoord,\RectHt+#1\Corr+\number\numexpr#1-1\relax\Gap) -- 
  \foreach \Valor in {#2}
        (\Valor,\RectHt+#1\Corr+\number\numexpr#1-1\relax\Gap) -- 
          coordinate (cota\Valor) 
      \draw[<->,>=latex,help lines] 
        (cotaFulc|-cota\Valor) -- 
          node[fill=white] {\footnotesize$\pgfmathparse{\Valor-\FulcrumCoord}%




Adding comments is currently not enabled.