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}{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
- \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 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};
- % Plot a straight line from the ball to the projection without drag
- \draw[red] ({0.1*\xraw},{0.1*\yraw}) -- ({0.1*\xplane},{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*\Ux*(0.5*\au*\x*\x + \vu*\x + \uraw) + 0.1*\Wx*(0.5*\awdrag*\x*\x + \vw*\x + \wraw) + 0.1*\Bx*\Bfixed},{0.1*(0.5*\ay*\x*\x + \vy*\x + \yraw)});
- }
- {
- }
- % Plot the projection without drag
- \draw[red, ball color=red, shading=ball] ({0.1*\xplane},{0.1*(17/12)}) circle [radius=0.0125];
- % Plot the projection with drag
- \draw[blue, ball color=blue, shading=ball] ({0.1*\xplanedrag},{0.1*(17/12)}) circle [radius=0.0125];
- % Plot the ball
- \draw[white, ball color=white, shading=ball] ({0.1*\xraw},{0.1*\yraw}) circle [radius=0.0125];
- % 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