Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- \documentclass[12pt]{article}
- \usepackage[paperwidth=200pt, paperheight=200pt,margin=12pt]{geometry}
- \usepackage{color,tikz,ifthen,animate}
- \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 9 PITCHf/x parameters
- \pgfmathsetmacro{\xo}{1.958}
- \pgfmathsetmacro{\yo}{50.0}
- \pgfmathsetmacro{\zo}{6.041}
- \pgfmathsetmacro{\vxo}{-0.469}
- \pgfmathsetmacro{\vyo}{-115.739}
- \pgfmathsetmacro{\vzo}{-0.622}
- \pgfmathsetmacro{\ax}{-10.947}
- \pgfmathsetmacro{\ay}{23.455}
- \pgfmathsetmacro{\az}{-32.665}
- % Set gravity
- \pgfmathsetmacro{\g}{-32.174}
- % Set the binormal vector
- \pgfmathsetmacro{\Bx}{\vyo*\az - \vzo*\ay}
- \pgfmathsetmacro{\By}{\vzo*\ax - \vxo*\az}
- \pgfmathsetmacro{\Bz}{\vxo*\ay - \vyo*\ax}
- \pgfmathsetmacro{\Bnorm}{100*sqrt((0.01*\Bx)*(0.01*\Bx) + (0.01*\By)*(0.01*\By) + (0.01*\Bz)*(0.01*\Bz))}
- \pgfmathsetmacro{\Bx}{\Bx/\Bnorm}
- \pgfmathsetmacro{\By}{\By/\Bnorm}
- \pgfmathsetmacro{\Bz}{\Bz/\Bnorm}
- % Find the fixed B-coordinate
- \pgfmathsetmacro{\Bfixed}{\Bx*\xo + \By*\yo + \Bz*\zo}
- % Set the vector in the direction of home plate, u
- \pgfmathsetmacro{\Ux}{(\Bx/abs(\Bx))*(-\By)/sqrt(\Bx*\Bx + \By*\By)}
- \pgfmathsetmacro{\Uy}{abs(\Bx)/sqrt(\Bx*\Bx + \By*\By)}
- \pgfmathsetmacro{\Uz}{0}
- % Set the vector in the direction of movement, w
- \pgfmathsetmacro{\signW}{(\Bx*\Uy-\By*\Ux)/abs(\Bx*\Uy-\By*\Ux)}
- \pgfmathsetmacro{\Wx}{\signW*(-\Bz*\Uy)}
- \pgfmathsetmacro{\Wy}{\signW*(\Bz*\Ux)}
- \pgfmathsetmacro{\Wz}{\signW*(\Bx*\Uy-\By*\Ux)}
- \pgfmathsetmacro{\Wnorm}{sqrt(\Wx*\Wx + \Wy*\Wy + \Wz*\Wz)}
- \pgfmathsetmacro{\Wx}{\Wx/\Wnorm}
- \pgfmathsetmacro{\Wy}{\Wy/\Wnorm}
- \pgfmathsetmacro{\Wz}{\Wz/\Wnorm}
- % Find the position, velocity, and acceleration in uw-space
- \pgfmathsetmacro{\uo}{\Ux*\xo + \Uy*\yo + \Uz*\zo}
- \pgfmathsetmacro{\vuo}{\Ux*\vxo + \Uy*\vyo + \Uz*\vzo}
- \pgfmathsetmacro{\au}{\Ux*\ax + \Uy*\ay + \Uz*\az}
- \pgfmathsetmacro{\wo}{\Wx*\xo + \Wy*\yo + \Wz*\zo}
- \pgfmathsetmacro{\vwo}{\Wx*\vxo + \Wy*\vyo + \Wz*\vzo}
- \pgfmathsetmacro{\aw}{\Wx*\ax + \Wy*\ay + \Wz*\az}
- \pgfmathsetmacro{\gw}{\Wz*\g}
- % 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 to home plate
- \pgfmathsetmacro{\thp}{\tplate - \trel}
- % Set time to catcher's mitt
- \pgfmathsetmacro{\u}{2*(\yo+4)/\ay}
- \pgfmathsetmacro{\tmitt}{-\r - sqrt(\r*\r - \u)}
- % Set time between release and mitt
- \pgfmathsetmacro{\tcm}{\tmitt - \trel}
- % Set the end velocities
- \pgfmathsetmacro{\vxend}{\vxo + \tplate*\ax}
- \pgfmathsetmacro{\vyend}{\vyo + \tplate*\ay}
- \pgfmathsetmacro{\vzend}{\vzo + \tplate*\az}
- % Set the x-location of the pitch at home plate
- \pgfmathsetmacro{\xplate}{0.5*\ax*\tplate*\tplate + \vxo*\tplate + \xo}
- % Set the number of frames
- \pgfmathtruncatemacro{\frames}{int(round(\tcm*60)+1)}
- % Set the number of frames to the plate
- \pgfmathtruncatemacro{\framesplate}{int(round(\thp*60)+1)}
- % Set the time increment
- \pgfmathsetmacro{\dt}{\tcm/(\frames-1)}
- % Create the animation
- \begin{center}
- \begin{animateinline}[poster=first,loop,controls]{60}
- \whiledo{\them < \frames}{
- \begin{tikzpicture}
- % Clip off anything out of frame
- \clip (-2,-2.5) rectangle (4,2.5);
- % Dirt, foul lines, catcher's box, and batter's boxes
- \draw[fill=brown!50!black] (-2,-2.5) -- (4,-2.5) -- (4,2.5) -- (-2,2.5) -- cycle;
- \draw[fill=white!80!brown] (-17/12,-17/24) -- (-17/12,17/24) -- (-8.5/12,17/24) -- (0,0) -- (-8.5/12,-17/24) -- cycle;
- \draw[ultra thick, white!80!brown] (-2,29/24) -- (3,29/24) -- (3,2.5);
- \draw[ultra thick, white!80!brown] (-2,-29/24) -- (3,-29/24) -- (3,-2.5);
- \draw[ultra thick, white!80!brown] (3,43/24) -- (4,43/24);
- \draw[ultra thick, white!80!brown] (3,-43/24) -- (4,-43/24);
- % Time increment between pitches
- \pgfmathsetmacro{\tinc}{\dt*\them + \trel};
- % Location of the pitch at the given time
- \pgfmathsetmacro{\xraw}{0.5*\ax*\tinc*\tinc + \vxo*\tinc + \xo};
- \pgfmathsetmacro{\yraw}{0.5*\ay*\tinc*\tinc + \vyo*\tinc + \yo};
- \pgfmathsetmacro{\zraw}{0.5*\az*\tinc*\tinc + \vzo*\tinc + \zo};
- % 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))};
- % Projection of the in-plane pitch excluding drag
- \pgfmathsetmacro{\uraw}{0.5*\au*\tinc*\tinc + \vuo*\tinc + \uo};
- \pgfmathsetmacro{\wraw}{0.5*\aw*\tinc*\tinc + \vwo*\tinc + \wo};
- \pgfmathsetmacro{\vu}{\au*\tinc + \vuo};
- \pgfmathsetmacro{\vw}{\aw*\tinc + \vwo};
- \pgfmathsetmacro{\uproj}{0.5*\au*(\tplate - \tinc)*(\tplate - \tinc) + \vu*(\tplate - \tinc) + \uraw};
- \pgfmathsetmacro{\wproj}{0.5*\gw*(\tplate - \tinc)*(\tplate - \tinc) + \vw*(\tplate - \tinc) + \wraw};
- \pgfmathsetmacro{\xplane}{\Ux*\uproj + \Wx*\wproj + \Bx*\Bfixed};
- % Calculate the 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)};
- % Contribution of drag in the w-direction
- \pgfmathsetmacro{\awdrag}{\Wx*\axdrag + \Wy*\aydrag + \Wz*\azdrag};
- % Projection of the in-plane pitch into the strike zone including drag
- \pgfmathsetmacro{\wprojdrag}{0.5*\awdrag*(\tplate - \tinc)*(\tplate - \tinc) + \vw*(\tplate - \tinc) + \wraw};
- \pgfmathsetmacro{\xplanedrag}{\Ux*\uproj + \Wx*\wprojdrag + \Bx*\Bfixed};
- % Projection of the pitch to the catcher's mitt
- \pgfmathsetmacro{\xmitt}{\Ux*(0.5*\au*(\tmitt - \tinc)*(\tmitt - \tinc) + \vu*(\tmitt - \tinc) + \uraw) + \Wx*(0.5*\gw*(\tmitt - \tinc)*(\tmitt - \tinc) + \vw*(\tmitt - \tinc) + \wraw) + \Bx*\Bfixed};
- \ifthenelse{\them < \framesplate}{
- % Plot the projection without drag
- \draw[red] ({(-17/12)},\xplane) circle [radius=0.125];
- % Plot the projection with drag
- \draw[blue,dashed] ({(-17/12)},\xplanedrag) circle [radius=0.125];
- }
- {
- \draw[red] ({(-17/12)},\xplate) circle [radius=0.125];
- \draw[blue,dashed] ({(-17/12)},\xplate) circle [radius=0.125];
- }
- % Plot the ball
- \draw[white, ball color=white, shading=ball] ({-\yraw},\xraw) circle [radius=0.125];
- % Plot the pitch path
- \draw[domain={\trel-0.25}:\tinc,smooth,variable=\x,green] plot ({-0.5*\ay*\x*\x - \vyo*\x - \yo},{0.5*\ax*\x*\x + \vxo*\x + \xo});
- \draw[shading = ball, ball color = white] (-\yraw,\xraw) circle [radius=0.125];
- % Catcher's mitt
- \draw[brown!25!black, fill=brown!25!black] (4,\xmitt) ellipse (0.25 and 0.417);
- % Display the metrics
- \node[above right] at (-2,1.9) {Dist.: };
- \node[below left] at (0.1,2.47) {\pgfmathprintnumber[precision=1,fixed]{\yraw}};
- \node[above right] at (-0.15,1.9) {ft};
- \node[above right] at (-2,-2.5) {Vel.: };
- \node[above left] at (0.1,-2.5) {\pgfmathprintnumber[precision=1,fixed]{\vel}};
- \node[below right] at (-0.1,-1.925) {mph};
- \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