Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- \documentclass[12pt]{article}
- \usepackage[paperwidth=215pt, paperheight=215pt,margin=12pt]{geometry}
- \usepackage{color,tikz,ifthen,animate,pgf}
- \begin{document}
- % This code will simulate a pitch based on the 9-parameter PITCHf/x model, including the pitch plane and projections for PITCHf/x movement without (red) and with (blue dashed) accounted for.
- % Written by Matthew Mata, January 2017.
- \thispagestyle{empty}
- % Set the counter
- \newcounter{m}
- \setcounter{m}{0}
- % Set the PITCHf/x parameters
- \pgfmathsetmacro{\xo}{3.841}
- \pgfmathsetmacro{\yo}{50.0}
- \pgfmathsetmacro{\zo}{5.573}
- \pgfmathsetmacro{\vxo}{-17.203}
- \pgfmathsetmacro{\vyo}{-144.942}
- \pgfmathsetmacro{\vzo}{-5.017}
- \pgfmathsetmacro{\ax}{37.647}
- \pgfmathsetmacro{\ay}{49.266}
- \pgfmathsetmacro{\az}{-9.706}
- % Set gravity
- \pgfmathsetmacro{\g}{-32.174}
- % Find the time to home plate from 50 feet
- \pgfmathsetmacro{\r}{\vyo/\ay}
- \pgfmathsetmacro{\s}{2*(\yo-(17/12))/\ay}
- \pgfmathsetmacro{\tplate}{-\r - sqrt(\r*\r - \s)}
- % Find the time of release at 55 feet
- \pgfmathsetmacro{\u}{2*(\yo-55)/\ay}
- \pgfmathsetmacro{\trel}{-\r - sqrt(\r*\r - \u)}
- % Find the flight time of the pitch
- \pgfmathsetmacro{\t}{\tplate - \trel}
- % Set the end velocities
- \pgfmathsetmacro{\vxend}{\vxo + \tplate*\ax}
- \pgfmathsetmacro{\vyend}{\vyo + \tplate*\ay}
- \pgfmathsetmacro{\vzend}{\vzo + \tplate*\az}
- % Set the number of frames
- \pgfmathtruncatemacro{\frames}{int(round(\t*60)+1)}
- % Set the time increment
- \pgfmathsetmacro{\dt}{\t/(\frames-1)}
- \begin{center}
- \begin{animateinline}[poster=first,loop,controls]{60}
- \whiledo{\them < \frames}{
- \begin{tikzpicture}[scale=0.725]
- \clip(-4,-1) rectangle (4,7);
- % Plot the field
- \draw[green!75!black,fill=green!75!black] (-4,7) -- (4,7) -- (4,-1) -- (-4,-1) -- cycle;
- \draw[brown!75!black,fill=brown!75!black] (0,0) circle [radius=1.3];
- \draw[brown!75!black,fill=brown!75!black] (0,6.05) circle [radius=0.9];
- \draw[white!75!brown,fill=white!75!brown] (-0.1,6.05) -- (-0.1,6.1) -- (0.1,6.1) -- (0.1,6.05) -- cycle;
- \draw[thick, white!75!brown] (0.3708,0.3708) -- (6.364,6.364);
- \draw[thick, white!75!brown] (-0.3708,0.3708) -- (-6.364,6.364);
- \draw[white, thick, fill=white] (0,0) -- (0.0708,0.0708) -- (0.0708,0.1416) -- (-0.0708,0.1416) -- (-0.0708,0.0708) -- cycle;
- \draw[white!75!brown, thick] (0.1208,0.3708) -- (0.5208,0.3708) -- (0.5208,-0.2291) -- (0.1208,-0.2291) -- cycle;
- \draw[white!75!brown, thick] (-0.1208,0.3708) -- (-0.5208,0.3708) -- (-0.5208,-0.2291) -- (-0.1208,-0.2291) -- cycle;
- \draw[white!75!brown, thick] (0.179,-0.2291) -- (0.179,-0.8) -- (-0.179,-0.8) -- (-0.179,-0.2291);
- % Calculate the time increment
- \pgfmathsetmacro{\tinc}{\dt*\them + \trel};
- % Current flight time of the pitch
- \pgfmathsetmacro{\time}{\dt*\them};
- % Find the x and y positions of the pitch
- \pgfmathsetmacro{\xraw}{0.5*\ax*\tinc*\tinc + \vxo*\tinc + \xo};
- \pgfmathsetmacro{\yraw}{0.5*\ay*\tinc*\tinc + \vyo*\tinc + \yo};
- % Velocity of the pitch at the given time
- \pgfmathsetmacro{\vx}{\ax*\tinc + \vxo};
- \pgfmathsetmacro{\vy}{\ay*\tinc + \vyo};
- \pgfmathsetmacro{\vz}{\az*\tinc + \vzo};
- \pgfmathsetmacro{\vel}{sqrt((15*\vx/22)*(15*\vx/22) + (15*\vy/22)*(15*\vy/22) + (15*\vz/22)*(15*\vz/22))};
- % Time remaining
- \pgfmathsetmacro{\trem}{\tplate-\tinc};
- % Projection of the pitch into the strike zone excluding drag
- \pgfmathsetmacro{\xproj}{\vx*(\tplate - \tinc) + \xraw};
- % Projection of the pitch into the strike zone including drag
- \pgfmathsetmacro{\vxbar}{(\vx+\vxend)/2};
- \pgfmathsetmacro{\vybar}{(\vy+\vyend)/2};
- \pgfmathsetmacro{\vzbar}{(\vz+\vzend)/2};
- \pgfmathsetmacro{\vbar}{1.25*sqrt((\vxbar/1.25)*(\vxbar/1.25) + (\vybar/1.25)*(\vybar/1.25) + (\vzbar/1.25)*(\vzbar/1.25))};
- \pgfmathsetmacro{\vcoeff}{-abs(\ax*(\vxbar/\vbar) + \ay*(\vybar/\vbar) + (\az-\g)*(\vzbar/\vbar))};
- \pgfmathsetmacro{\axdrag}{\vcoeff*(\vxbar/\vbar)};
- \pgfmathsetmacro{\aydrag}{\vcoeff*(\vybar/\vbar)};
- \pgfmathsetmacro{\azdrag}{\g + \vcoeff*(\vzbar/\vbar)};
- \pgfmathsetmacro{\xprojdrag}{0.5*\axdrag*(\tplate-\tinc)*(\tplate-\tinc) + \vx*(\tplate-\tinc) + \xraw};
- % Plot a straight line from the ball to the projection without drag
- \draw[red] ({0.1*\xraw},{0.1*\yraw}) -- ({0.1*\xproj},{0.1*(17/12)});
- % Plot a curve from the ball to the projection with drag
- \pgfmathsetmacro{\framesadj}{\frames-1}
- \ifthenelse{ \them < \framesadj }{
- \draw[blue, domain=0:\trem, smooth, dashed] plot ({0.1*(0.5*\axdrag*\x*\x + \vx*\x + \xraw)},{0.1*(0.5*\ay*\x*\x + \vy*\x + \yraw)});
- }
- {
- }
- % Plot the projection without drag
- \draw[red, ball color=red, shading=ball] ({0.1*\xproj},{0.1*(17/12)}) circle [radius=0.025];
- % Plot the projection with drag
- \draw[blue, ball color=blue, shading=ball] ({0.1*\xprojdrag},{0.1*(17/12)}) circle [radius=0.025];
- % Plot the ball
- \draw[white, ball color=white, shading=ball] ({0.1*\xraw},{0.1*\yraw}) circle [radius=0.025];
- % Display the metrics
- \node[above right] at (-4,6) {Dist.: };
- \node[above left] at (-1.25,6) {\pgfmathprintnumber[precision=1,fixed]{\yraw}};
- \node[above right] at (-1.5,6) {ft};
- \node[above right] at (0,6) {Vel.: };
- \node[above left] at (2.75,6) {\pgfmathprintnumber[precision=1,fixed]{\vel}};
- \node[above left] at (4,5.9) {mph};
- \node[above right] at (-4,-1) {Time: };
- \node[above left] at (-1,-1) {\pgfmathprintnumber[precision=2,fixed]{\time}};
- \node[above right] at (-1.25,-1) {sec};
- \foreach \n in {0,...,\frames}{
- \ifthenelse{\n < \them}{
- \pgfmathsetmacro{\tshadow}{\dt*\n + \trel};
- \pgfmathsetmacro{\xshadow}{0.5*\ax*\tshadow*\tshadow + \vxo*\tshadow + \xo};
- \pgfmathsetmacro{\yshadow}{0.5*\ay*\tshadow*\tshadow + \vyo*\tshadow + \yo};
- \draw[white, ball color=white, shading=ball, opacity=0.5] ({0.1*\xshadow},{0.1*\yshadow}) circle [radius=0.025];
- }
- }
- \end{tikzpicture}
- \stepcounter{m}
- \ifthenelse{\them < \frames}{
- \newframe
- }
- {
- \end{animateinline} \relax
- }
- }
- \end{center}
- \end{document}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement