以下 PSTricks 代码在 TikZ 中的对应代码是什么?绘制自由落体图

以下 PSTricks 代码在 TikZ 中的对应代码是什么?绘制自由落体图

我想使用“通过示例学习”的方法来学习 TikZ,因为这种方式可以帮助我跳过不必要的概念,从而节省时间。我做了一个例子,它是 PSTricks 中的自由落体图,如下所示。

在此处输入图片描述

\documentclass[pstricks,border=12pt]{standalone}
\usepackage{multido}
\usepackage[nomessages]{fp}
\def\LoadConstants{}
\newcommand\const[3][3]{%
    \edef\temporary{round(#3}%
    \expandafter\FPeval\csname#2\expandafter\endcsname
        \expandafter{\temporary:#1)}%
        \edef\LoadConstants{\LoadConstants
            \noexpand\pstVerb{/#2 \csname#2\endcsname\space def}}%
}


\const[1]{G}{9.8}
\const[1]{Tfinal}{2.0}
\def\y(#1){-G/2*#1^2}
\const[1]{Yfinal}{\y(Tfinal)}

\SpecialCoor
\usepackage{siunitx}
\begin{document}
\begin{pspicture}[showgrid=false](3.5,\Yfinal)
    \LoadConstants
    \psline(1.5,0)(1.5,\Yfinal)
    \multido{\n=0.0+0.5}{5}
    {
        \const[1]{Yt}{\y(\n)}%
        \rput[r](*1.25 {\y(\n)}){$\SI{\Yt}{\meter}$}
        \psline(1.4,\Yt)(1.6,\Yt)
        \rput[l](*1.75 {\y(\n)}){$t=\SI{\n}{\second}$}
        \pscircle*(*3.5 {\y(\n)}){5pt}
    }
\end{pspicture}
\end{document}

我在评估代数表达式并在 TikZ 中打印其值时遇到问题。这是我的尝试。

\documentclass[tikz,border=12pt]{standalone}


\def\G{9.8}
\def\Tfinal{2.0}
\def\y(#1){-\G/2*#1^2}
\def\Yfinal{\y(\Tfinal)}


\usepackage{siunitx}
\begin{document}
\begin{tikzpicture}
    \draw (1.5,0) -- (1.5,\Yfinal);
    \foreach \n in {0.0,0.5,...,2.0}
    {
        \draw ({1.25},{\y(\n)}) node {$\SI{\y(\n)}{\meter}$};
        \draw ({1.4},{\y(\n)}) -- ({1.6},{\y(\n)});
        \draw ({1.75},{\y(\n)}) node {$t=\SI{\n}{\second}$};
        \draw[fill=black] ({3.5},{\y(\n)}) circle (5pt);
    }
\end{tikzpicture}
\end{document}

答案1

我的建议。首先,没有必要将斧头放在 1.5 处。您可以使用 0,如果您需要添加其他对象,那么您可以使用范围进行移动。我曾经\sisetup得到一个灯光代码。如您所见,您可以删除\Yfinal。节点 tmp 具有相同的宽度,因此可以相对于 tmp.east 放置圆圈。通过这种方式可以缩放图片。我个人更喜欢\node at (x,y)而不是\draw (x,y) node

更新

\documentclass[tikz,border=12pt]{standalone}
\usepackage{siunitx}
\sisetup{round-integer-to-decimal,
         round-mode = places,
         round-precision = 1}% possible numprint
\begin{document}

% constants
\def\G{9.8}
\def\Tfinal{2.0}
\def\y(#1){-\G/2*#1^2}

\begin{tikzpicture}% [scale=.5] possible with the next code
    \draw (0,0) -- (0,{\y(\Tfinal)}); % you don't nedd to use \Yfinal
    \foreach \n in {0.0,0.5,...,\Tfinal}
    {       
     \draw (-0.1,{\y(\n)}) -- (0.1,{\y(\n)});
     \node[left] at (-0.25,{\y(\n)}) {\pgfmathparse{\y(\n)}\SI{\pgfmathresult}{\meter}};
     \node[right] (tmp) at (0.25,{\y(\n)}) {$t=\SI{\n}{\second}$};
     \fill ([xshift=.25 cm]tmp.east) circle (5pt);
    }
\end{tikzpicture}
\end{document}

在此处输入图片描述

答案2

在此处输入图片描述

Asymptote如果有人也想学习,请freefall.asy

unitsize(5mm);
texpreamble("\usepackage["
  +"rm={oldstyle=true,tabular=true},"
  +"]{cfr-lm}");

real g=9.81;        // g constant
int n=5;            // number of time points
real dt=0.5;        // time interval
real tmax=(n-1)*dt; 

real h(real t){return t^2*g/2;};  // h(t) function

pair top=(0,0);
pair bottom=(0,-h(tmax));

real dx=0.6;                        // half of the tick width
guide tickMark=((-dx,0)--(dx,0));   // tick mark line

pair pos;
Label L;
real ballX=5;                       // x- coordinate of the ball
real ballR=0.5;                     // ball radius
path ball=scale(ballR)*unitcircle;  // the ball outline

pen startColor=darkblue;            
pen finalColor=orange;

pen ballColor(int i, int n){  // interpolates the color at i-th time reading
  return (n-1.0-i)/(n-1.0)*startColor+i/(n-1.0)*finalColor;
};

guide shadeScale=scale(0.6,1)*box((-dx,0),(dx,-h(tmax)));   // shade scale outline
axialshade(shadeScale,          // axial shading of the shade scale outline
  startColor+0.3*white, top,    // start color & position
  finalColor+0.3*white, bottom  // final color & position
);

transform toBallPos;
real t=0.0;

for(int i=0;i<n;++i){
  pos=(0,-h(t));
//  draw(shift(pos)*tickMark,white+1.6pt);
  draw(shift(pos)*tickMark,ballColor(i,n)+1.2pt);
  L=Label("$t=$"+format("%#5.1f",t)+"\,s");
  label(L,pos+(dx,0),E);
  label(((h(t)!=0)?"$-$":"")+format("%#7.2f",h(t))+"\,m",pos-(dx,0),W);
  toBallPos=shift(pos+(ballX,0));
  radialshade(toBallPos*ball,  // transform is applied by "*" on the left
    white,toBallPos*(0,0),0.07*ballR
   ,ballColor(i,n),toBallPos*(0,0),ballR); 
  t+=dt;
}

要获得独立版本freefall.pdf,请运行asy -f pdf freefall.asy

答案3

\documentclass[tikz,border=12pt]{standalone}

\def\G{9.8}
\def\Tfinal{2.0}
\def\y(#1){-\G/2*#1^2}
\pgfmathparse{\y(\Tfinal)}
\edef\Yfinal{\pgfmathresult}

\usepackage[nomessages]{fp}
\usepackage{siunitx}
\begin{document}
\begin{tikzpicture}
    \draw (1.5,0) -- (1.5,\Yfinal);
    \foreach \n in {0.0,0.5,...,\Tfinal}
    {       
        \draw ({1.25},{\y(\n)}) node[anchor=east] {\pgfmathparse{\y(\n)}\FPeval\temp{round(\pgfmathresult:1)}$\SI{\temp}{\meter}$};
        \draw ({1.4},{\y(\n)}) -- ({1.6},{\y(\n)});
        \draw ({1.75},{\y(\n)}) node[anchor=west] {\pgfmathparse{\n}\FPeval\temp{round(\pgfmathresult:1)}$t=\SI{\temp}{\second}$};
        \draw[fill=black] ({3.5},{\y(\n)}) circle (5pt);
    }
\end{tikzpicture}
\end{document}

由于 SI[round-mode=places,round-precision=1]...更改并产生与现在不兼容的数字格式,我将其用作0.0后备。0\pgfmathprintnumberto[precision=1]{\pgfmathresult}{\temp}\SI\FPeval

相关内容