tikz:重心平均值

tikz:重心平均值

期望输出

在此处输入图片描述

我的MWE

代码借自这里

\documentclass[tikz,border=1cm]{standalone}
\usepackage{tikz}

\begin{document}
        \begin{tikzpicture}[rotate=-30,transform shape]
            \draw (-0.2,0) rectangle (15.5,1);
            %% lower divisions
            \foreach \x in {0,1,...,15}{
            \draw (\x,0) -- (\x,0.2)node[above,scale=0.4]{\x};
            }
            \foreach \x in {0.1,0.2,...,14.9}{
            \draw (\x,0) -- (\x,0.075);
            }
            \foreach \x in {0.5,1,...,14.5}{
            \draw (\x,0) -- (\x,0.15);
            }
            % Upper divisions
            \foreach \x in {0,1,...,6}{
            \draw (\x in,1) -- (\x in,0.8)node[below,scale=0.4]{\x};
            }
            \foreach \x in {0.1,0.2,...,5.9}{
            \draw (\x in,1) -- (\x in,0.925);
            }
            \foreach \x in {0.5,1,...,5.5}{
            \draw (\x in,1) -- (\x in,0.85);
            }
        \end{tikzpicture}
\end{document}

输出

在此处输入图片描述

任何帮助都将不胜感激。谢谢

答案1

改良版:

在此处输入图片描述

Balance现在,只需使用环境(强制参数是放置权重的逗号分隔列表)和命令\Cotas(用于帮助行)即可完成所有操作;例如,上面的图像只需通过以下方式生成

\begin{Balance}{1,2,3,5,7,9}
\end{Balance}

\begin{Balance}{3,4,8}
\Cotas{4,8}
\Cotas[2]{3}
\end{Balance}

\begin{Balance}{2,4,6,8,10}
\Cotas{4,8}
\Cotas[2]{2,10}
\end{Balance}

\begin{Balance}{1,4,5,8,10,12}
\Cotas{5,8}
\Cotas[2]{4,10}
\Cotas[3]{1,12}
\end{Balance}

代码:

\documentclass{article}
\usepackage[]{geometry}
\usepackage{tikz} 

\definecolor{color1}{RGB}{249,255,195}
\definecolor{color2}{RGB}{251,245,160}
\definecolor{color3}{RGB}{239,178,107}
\definecolor{color4}{RGB}{214,96,0}
\definecolor{color5}{RGB}{191,203,224}
\definecolor{color6}{RGB}{105,103,161}

% A pic path for the weights
\tikzset{
Peso/.pic={
  \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);
  \draw[fill=color5] 
    (wll) -- (wlr) -- (wur) -- (wul) -- cycle;
  \draw[fill=color6]
    (wur) --
    ++(-\Angle:6pt) --
    ++(-75:0.4cm) --
    (wlr) --
    cycle;  
  }
}

\newcounter{Number}
\newcounter{Suma}
\newlength\XFactor
\setlength\XFactor{1cm}

\def\RectHt{1cm}
\def\Angle{20}
\def\Corr{0pt}
\def\Gap{6pt}


\newenvironment{Balance}[1]
{%
  \medskip\par\noindent
  \setcounter{Number}{0}%
  \setcounter{Suma}{0}%
  \begin{tikzpicture}[x=\XFactor]
    \foreach \Valor in {#1}
    {
      \pic at (\Valor,\RectHt) {Peso};
      \stepcounter{Number}%
      \addtocounter{Suma}{\Valor}
      \xdef\Steps{\Valor}
    }
    \def\RectWd{\numexpr\Steps+1\relax*\XFactor}%
    \pgfmathsetmacro{\FulcrumCoord}{\theSuma/\theNumber}

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

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

\newcommand\Cotas[2][2]{
\draw 
  (\FulcrumCoord,\RectHt+#1\Corr+\number\numexpr#1-1\relax\Gap) -- 
  ++(0,10pt);
  \foreach \Valor in {#2}
    {
      \draw 
        (\Valor,\RectHt+#1\Corr+\number\numexpr#1-1\relax\Gap) -- 
          coordinate (cota\Valor) 
        ++(0,10pt);
      \draw[<->,>=latex,help lines] 
        (cotaFulc|-cota\Valor) -- 
          node[fill=white] {$\pgfmathparse{\Valor-\FulcrumCoord}\pgfmathprintnumber[precision=1]{\pgfmathresult}$} 
        (cota\Valor);
    }
}

\begin{document}

\begin{Balance}{1,2,3,5,7,9}
\end{Balance}

\begin{Balance}{3,4,8}
\Cotas{4,8}
\Cotas[2]{3}
\end{Balance}

\begin{Balance}{2,4,6,8,10}
\Cotas{4,8}
\Cotas[2]{2,10}
\end{Balance}

\begin{Balance}{1,4,5,8,10,12}
\Cotas{5,8}
\Cotas[2]{4,10}
\Cotas[3]{1,12}
\end{Balance}

\end{document}

第一个版本:

有一种可能性是:

\documentclass{standalone}
\usepackage{tikz} 

\definecolor{color1}{RGB}{249,255,195}
\definecolor{color2}{RGB}{251,245,160}
\definecolor{color3}{RGB}{239,178,107}
\definecolor{color4}{RGB}{214,96,0}
\definecolor{color5}{RGB}{191,203,224}
\definecolor{color6}{RGB}{105,103,161}

\tikzset{
Peso/.pic={
  \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);
  \draw[fill=color5] 
    (wll) -- (wlr) -- (wur) -- (wul) -- cycle;
  \draw[fill=color6]
    (wur) --
    ++(-\Angle:6pt) --
    ++(-75:0.4cm) --
    (wlr) --
    cycle;  
  }
}

\begin{document}

\begin{tikzpicture}[x=1.3cm]
\def\RectWd{13cm}
\def\RectHt{1cm}
\def\Angle{20}

% The weights
\foreach \Valor in {3,4,8}
  \pic at (\Valor,\RectHt) {Peso};

% The ruled rectangle
\coordinate (ll) at (0,0);
\coordinate (ur) at (\RectWd,\RectHt);

\draw[fill=color1]
  (ll) rectangle (ur);
\draw[fill=color2] 
  (ur) -- 
  ++(-\Angle:6pt) -- 
  ++(0,-\RectHt) -- 
  ++(-\RectWd,0) -- 
  (ll) --
  ++(\RectWd,0) --
  cycle;   
\foreach \Valor in {1,...,9}
  \draw (\Valor,1) -- ++(0pt,-5pt) node[below] {\Valor};  
\draw (\RectWd,0) -- ++(-\Angle:6pt);

% The fulcrum
\draw[fill=color3] 
  (5,0) --
  ++(240:1) --
  ++(1,0) coordinate (aux) --
  cycle;
\draw[fill=color4]  
  (5,0) --
  ++(-\Angle:6pt) --
  ++(-60:1) --
  ++(-1,0) --
  ++(180-\Angle:6pt) --
  ++(1,0) --
  cycle;
\draw (aux) -- ++(-\Angle:6pt);

% the helping lines
\begin{scope}[
>=latex,
help lines
]
\foreach \Valor in {4,5,8}
  \draw (\Valor,1.5) -- coordinate (cota\Valor) ++(0,14pt);
\draw[<->] (cota4) -- node[fill=white] {$-1$} (cota5);
\draw[<->] (cota5) -- node[fill=white] {$3$} (cota8);
\foreach \Valor in {3,5}
  \draw (\Valor,2.2) -- coordinate (cota\Valor) ++(0,14pt);
\draw[<->] (cota3) -- node[fill=white] {$-2$} (cota5);
\end{scope}
\end{tikzpicture}

\end{document}

在此处输入图片描述

并且人们可以很容易地为系统做出不同的安排:

\documentclass{standalone}
\usepackage{tikz} 

\definecolor{color1}{RGB}{249,255,195}
\definecolor{color2}{RGB}{251,245,160}
\definecolor{color3}{RGB}{239,178,107}
\definecolor{color4}{RGB}{214,96,0}
\definecolor{color5}{RGB}{191,203,224}
\definecolor{color6}{RGB}{105,103,161}

\tikzset{
Peso/.pic={
  \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);
  \draw[fill=color5] 
    (wll) -- (wlr) -- (wur) -- (wul) -- cycle;
  \draw[fill=color6]
    (wur) --
    ++(-\Angle:6pt) --
    ++(-75:0.4cm) --
    (wlr) --
    cycle;  
  }
}

\begin{document}

\begin{tikzpicture}[x=1.3cm]
\def\RectWd{14.3cm}
\def\RectHt{1cm}
\def\Angle{20}

% The weights
\foreach \Valor in {2,4,6,8,10}
  \pic at (\Valor,\RectHt) {Peso};

% The ruled rectangle
\coordinate (ll) at (0,0);
\coordinate (ur) at (\RectWd,\RectHt);

\draw[fill=color1]
  (ll) rectangle (ur);
\draw[fill=color2] 
  (ur) -- 
  ++(-\Angle:6pt) -- 
  ++(0,-\RectHt) -- 
  ++(-\RectWd,0) -- 
  (ll) --
  ++(\RectWd,0) --
  cycle;   
\foreach \Valor in {1,...,10}
  \draw (\Valor,1) -- ++(0pt,-5pt) node[below] {\Valor};  
\draw (\RectWd,0) -- ++(-\Angle:6pt);

% The fulcrum
\draw[fill=color3] 
  (6,0) --
  ++(240:1) --
  ++(1,0) coordinate (aux) --
  cycle;
\draw[fill=color4]  
  (6,0) --
  ++(-\Angle:6pt) --
  ++(-60:1) --
  ++(-1,0) --
  ++(180-\Angle:6pt) --
  ++(1,0) --
  cycle;
\draw (aux) -- ++(-\Angle:6pt);

% the helping lines
\begin{scope}[
>=latex,
help lines
]
\foreach \Valor in {4,6,8}
  \draw (\Valor,1.5) -- coordinate (cota\Valor) ++(0,14pt);
\draw[<->] (cota4) -- node[fill=white] {$-2$} (cota6);
\draw[<->] (cota6) -- node[fill=white] {$2$} (cota8);
\foreach \Valor in {2,6,10}
  \draw (\Valor,2.2) -- coordinate (cota\Valor) ++(0,14pt);
\draw[<->] (cota2) -- node[fill=white] {$-4$} (cota6);
\draw[<->] (cota10) -- node[fill=white] {$4$} (cota6);
\end{scope}
\end{tikzpicture}

\end{document}

在此处输入图片描述

相关内容