Advertisement
matthewrmata

Tex Pitch Simulation with Plane and Movement

Jul 23rd, 2016
338
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Latex 10.09 KB | None | 0 0
  1. \documentclass[12pt]{article}
  2.  
  3. \usepackage[paperwidth=200pt, paperheight=200pt,margin=12pt]{geometry}
  4. \usepackage{color,tikz,ifthen,animate,pgf}
  5.  
  6. \begin{document}
  7.  
  8. % This code will simulate a pitch based on the 9-parameter PITCHf/x model, including the pitch plane and projections for in-plane movement (yellow) and PITCHf/x movement (red).
  9.  
  10. % Note that due to a problem with the 'animate' package and changing opacity, the first frame of the pitch plane may appear solid. To get 55 feet with a see-through plane, change the counter, m, below from 0 to -1 to start back one frame.
  11.  
  12. % Written by Matthew Mata, July 2016.
  13.  
  14. \thispagestyle{empty}
  15.  
  16. % Set the counter
  17. \newcounter{m}
  18. \setcounter{m}{0}
  19.  
  20. % Set the 9 PITCHf/x parameters
  21. \pgfmathsetmacro{\xo}{-2.951}
  22. \pgfmathsetmacro{\yo}{50.0}
  23. \pgfmathsetmacro{\zo}{5.397}
  24. \pgfmathsetmacro{\vxo}{9.649}
  25. \pgfmathsetmacro{\vyo}{-119.85}
  26. \pgfmathsetmacro{\vzo}{-2.381}
  27. \pgfmathsetmacro{\ax}{3.173}
  28. \pgfmathsetmacro{\ay}{22.075}
  29. \pgfmathsetmacro{\az}{-34.452}
  30. \pgfmathsetmacro{\g}{-32.174}
  31.  
  32. % Set the binormal vector
  33. \pgfmathsetmacro{\Bx}{\vyo*\az - \vzo*\ay}
  34. \pgfmathsetmacro{\By}{\vzo*\ax - \vxo*\az}
  35. \pgfmathsetmacro{\Bz}{\vxo*\ay - \vyo*\ax}
  36. \pgfmathsetmacro{\Bnorm}{100*sqrt((0.01*\Bx)*(0.01*\Bx) + (0.01*\By)*(0.01*\By) + (0.01*\Bz)*(0.01*\Bz))}
  37. \pgfmathsetmacro{\Bx}{\Bx/\Bnorm}
  38. \pgfmathsetmacro{\By}{\By/\Bnorm}
  39. \pgfmathsetmacro{\Bz}{\Bz/\Bnorm}
  40.  
  41. % Find the fixed B-coordinate
  42. \pgfmathsetmacro{\Bfixed}{\Bx*\xo + \By*\yo + \Bz*\zo}
  43.  
  44. % Set the vector in the direction of home plate, u
  45. \pgfmathsetmacro{\Ux}{(\Bx/abs(\Bx))*(-\By)/sqrt(\Bx*\Bx + \By*\By)}
  46. \pgfmathsetmacro{\Uy}{abs(\Bx)/sqrt(\Bx*\Bx + \By*\By)}
  47. \pgfmathsetmacro{\Uz}{0}
  48.  
  49. % Set the vector in the direction of movement, w
  50. \pgfmathsetmacro{\signW}{(\Bx*\Uy-\By*\Ux)/abs(\Bx*\Uy-\By*\Ux)}
  51. \pgfmathsetmacro{\Wx}{\signW*(-\Bz*\Uy)}
  52. \pgfmathsetmacro{\Wy}{\signW*(\Bz*\Ux)}
  53. \pgfmathsetmacro{\Wz}{\signW*(\Bx*\Uy-\By*\Ux)}
  54. \pgfmathsetmacro{\Wnorm}{sqrt(\Wx*\Wx + \Wy*\Wy + \Wz*\Wz)}
  55. \pgfmathsetmacro{\Wx}{\Wx/\Wnorm}
  56. \pgfmathsetmacro{\Wy}{\Wy/\Wnorm}
  57. \pgfmathsetmacro{\Wz}{\Wz/\Wnorm}
  58.  
  59. % Find the position, velocity, and acceleration in uw-space
  60. \pgfmathsetmacro{\uo}{\Ux*\xo + \Uy*\yo + \Uz*\zo}
  61. \pgfmathsetmacro{\vuo}{\Ux*\vxo + \Uy*\vyo + \Uz*\vzo}
  62. \pgfmathsetmacro{\au}{\Ux*\ax + \Uy*\ay + \Uz*\az}
  63. \pgfmathsetmacro{\wo}{\Wx*\xo + \Wy*\yo + \Wz*\zo}
  64. \pgfmathsetmacro{\vwo}{\Wx*\vxo + \Wy*\vyo + \Wz*\vzo}
  65. \pgfmathsetmacro{\aw}{\Wx*\ax + \Wy*\ay + \Wz*\az}
  66. \pgfmathsetmacro{\gw}{\Wz*\g}
  67.  
  68. % Set the slope and intercept of the plane's intersection with the front of the strike zone
  69. \pgfmathsetmacro{\slope}{-\Bx/\Bz}
  70. \pgfmathsetmacro{\intercept}{\zo + (\Bx/\Bz)*\xo + (\By/\Bz)*(\yo - (17/12))}
  71.  
  72. % Find the points of intersection with the ground and 5 feet vertically at the front of the strike zone
  73. \pgfmathsetmacro{\topzone}{(5 - \intercept)/\slope}
  74. \pgfmathsetmacro{\bottomzone}{-\intercept/\slope}
  75.  
  76. % Find the position of the plane at 55 feet
  77. \pgfmathsetmacro{\toprelease}{\xo + (\By/\Bx)*(\yo - 55) + (\Bz/\Bx)*(\zo-5)}
  78. \pgfmathsetmacro{\bottomrelease}{\xo + (\By/\Bx)*(\yo - 55) + (\Bz/\Bx)*\zo}
  79.  
  80. % Find the scaled location of the top/back of the plane
  81. \pgfmathsetmacro{\xdistTB}{\toprelease}
  82. \pgfmathsetmacro{\ydistTB}{55 + (73/12)}
  83. \pgfmathsetmacro{\zdistTB}{5 - 2.5}
  84. \pgfmathsetmacro{\radTB}{sqrt(\xdistTB*\xdistTB + \ydistTB*\ydistTB + \zdistTB*\zdistTB)}
  85. \pgfmathsetmacro{\thetaTB}{atan2(\xdistTB,\ydistTB)}
  86. \pgfmathsetmacro{\phiTB}{acos(\zdistTB/\radTB)}
  87. \pgfmathsetmacro{\radscaleTB}{7.5/(sin(\thetaTB)*sin(\phiTB))}
  88. \pgfmathsetmacro{\xTB}{\radscaleTB*cos(\thetaTB)*sin(\phiTB)}
  89. \pgfmathsetmacro{\zTB}{\radscaleTB*cos(\phiTB) + 2.5}
  90.  
  91. % Find the scaled location of the bottom/back of the plane
  92. \pgfmathsetmacro{\xdistBB}{\bottomrelease}
  93. \pgfmathsetmacro{\ydistBB}{55 + (73/12)}
  94. \pgfmathsetmacro{\zdistBB}{0 - 2.5}
  95. \pgfmathsetmacro{\radBB}{sqrt(\xdistBB*\xdistBB + \ydistBB*\ydistBB + \zdistBB*\zdistBB)}
  96. \pgfmathsetmacro{\thetaBB}{atan2(\xdistBB,\ydistBB)}
  97. \pgfmathsetmacro{\phiBB}{acos(\zdistBB/\radBB)}
  98. \pgfmathsetmacro{\radscaleBB}{7.5/(sin(\thetaBB)*sin(\phiBB))}
  99. \pgfmathsetmacro{\xBB}{\radscaleBB*cos(\thetaBB)*sin(\phiBB)}
  100. \pgfmathsetmacro{\zBB}{\radscaleBB*cos(\phiBB) + 2.5}
  101.  
  102. % Set the time to the plate
  103. \pgfmathsetmacro{\r}{\vyo/\ay}
  104. \pgfmathsetmacro{\s}{2*(\yo-(17/12))/\ay}
  105. \pgfmathsetmacro{\tplate}{-\r - sqrt(\r*\r - \s)}
  106.  
  107. % Set time of release
  108. \pgfmathsetmacro{\u}{2*(\yo-55)/\ay}
  109. \pgfmathsetmacro{\trel}{-\r - sqrt(\r*\r - \u)}
  110.  
  111. % Set time from release to plate
  112. \pgfmathsetmacro{\t}{\tplate - \trel}
  113.  
  114. % Set the number of frames
  115. \pgfmathtruncatemacro{\frames}{int(round(\t*60)+1)}
  116.  
  117. % Set the time increment
  118. \pgfmathsetmacro{\dt}{\t/(\frames-1)}
  119.  
  120. % Create the animation
  121. \begin{center}
  122.     \begin{animateinline}[poster=first,loop,controls]{60}
  123.         \whiledo{\them < \frames}{
  124.             \begin{tikzpicture}
  125.                 % Clip off anything out of frame
  126.                 \clip (-3,0) rectangle (3,5);
  127.                 % Sky
  128.                 \shade[top color = black!60!white, bottom color = black!40!white] (-3,5) -- (3,5) -- (3,2.5) -- (-3,2.5) -- cycle;
  129.                 % Grass
  130.                 \shade[top color = green!50!black, bottom color = green!70!black] (-3,0) -- (3,0) -- (3,2.447) to [out=179, in =0] (0,2.454) to [out=180, in=1] (-3,2.447) -- (-3,0);
  131.                 % Batter's Eye
  132.                 \shade[top color = green!60!black, bottom color = green!30!black] (-0.541,3.176) -- (0.541,3.176) -- (0.541,2.5) -- (-0.541,2.5) -- cycle;
  133.                 % Bleachers
  134.                 \draw[fill = blue!75!black] (-3,3.274) to [out=-1, in=180] (-0.541,3.176) -- (-0.541,2.5) -- (-3,2.5) -- cycle;
  135.                 \draw[fill = blue!75!black] (3,3.274) to [out=181, in=0] (0.541,3.176) -- (0.541,2.5) -- (3,2.5) -- cycle;
  136.                 % Outfield Wall
  137.                 \shade[top color = green!40!black, bottom color = green!20!black] (-3,2.447) -- (-3,2.659) to [out=-1, in=180] (0,2.639) to [out=0, in=181] (3,2.659) -- (3,2.447) to [out=179, in =0] (0,2.454) to [out=180, in=1] (-3,2.447);
  138.                 % Yellow Line
  139.                 \draw[yellow] (-3,2.659) to [out=-1, in=180] (0,2.639) to [out=0, in=181] (3,2.659);
  140.                 % Mound
  141.                 \shade[top color = brown!90!black, bottom color = brown!70!black] (-1.014,2.218) to [out=10, in=180] (0,2.312) to [out=0, in=170] (1.014,2.218) to [out=185, in=0] (0,2.174) to [out = 180, in=355] (-1.014,2.218);
  142.                 % Pitching Rubber
  143.                 \draw [white] (-0.113,2.312) -- (0.113,2.312);
  144.                          % Plot the pitch plane
  145.                 \draw[yellow!80!white] (\topzone,5) -- (\bottomzone,0) -- (\xBB,\zBB) -- (\xTB,\zTB) -- cycle;
  146.                 \fill[yellow!80!white, opacity=0.5] (\topzone,5) -- (\bottomzone,0) -- (\xBB,\zBB) -- (\xTB,\zTB) -- cycle;
  147.                 \draw[yellow, fill=yellow] (\topzone,5) circle [radius=0.04];
  148.                 \draw[yellow, fill=yellow] (\bottomzone,0) circle [radius=0.04];
  149.                 \draw[yellow, fill=yellow] (\xBB,\zBB) circle [radius=0.02];
  150.                 \draw[yellow, fill=yellow] (\xTB,\zTB) circle [radius=0.02];
  151.                 % Time increment between pitches
  152.                 \pgfmathsetmacro{\tinc}{\dt*\them + \trel};
  153.                 % Current flight time of the pitch
  154.                 \pgfmathsetmacro{\time}{\dt*\them};
  155.                 % Location of the pitch at the given time
  156.                 \pgfmathsetmacro{\xraw}{0.5*\ax*\tinc*\tinc + \vxo*\tinc + \xo};
  157.                 \pgfmathsetmacro{\yraw}{0.5*\ay*\tinc*\tinc + \vyo*\tinc + \yo};
  158.                 \pgfmathsetmacro{\zraw}{0.5*\az*\tinc*\tinc + \vzo*\tinc + \zo};
  159.                 % Velocity of the pitch at the given time
  160.                 \pgfmathsetmacro{\vx}{\ax*\tinc + \vxo};
  161.                 \pgfmathsetmacro{\vy}{\ay*\tinc + \vyo};
  162.                 \pgfmathsetmacro{\vz}{\az*\tinc + \vzo};
  163.                 \pgfmathsetmacro{\vel}{sqrt((15*\vx/22)*(15*\vx/22) + (15*\vy/22)*(15*\vy/22) + (15*\vz/22)*(15*\vz/22))};
  164.                 % Projection of the pitch into the strike zone
  165.                 \pgfmathsetmacro{\xproj}{\vx*(\tplate - \tinc) + \xraw};
  166.                 \pgfmathsetmacro{\zproj}{0.5*\g*(\tplate - \tinc)*(\tplate - \tinc) + \vz*(\tplate - \tinc) + \zraw};
  167.                 % Projection of the in-plane pitch
  168.                 \pgfmathsetmacro{\uraw}{0.5*\au*\tinc*\tinc + \vuo*\tinc + \uo};
  169.                 \pgfmathsetmacro{\wraw}{0.5*\aw*\tinc*\tinc + \vwo*\tinc + \wo};
  170.                 \pgfmathsetmacro{\vu}{\au*\tinc + \vuo};
  171.                 \pgfmathsetmacro{\vw}{\aw*\tinc + \vwo};
  172.                 \pgfmathsetmacro{\uproj}{0.5*\au*(\tplate - \tinc)*(\tplate - \tinc) + \vu*(\tplate - \tinc) + \uraw};
  173.                 \pgfmathsetmacro{\wproj}{0.5*\gw*(\tplate - \tinc)*(\tplate - \tinc) + \vw*(\tplate - \tinc) + \wraw};
  174.                 \pgfmathsetmacro{\xplane}{\Ux*\uproj + \Wx*\wproj + \Bx*\Bfixed};
  175.                 \pgfmathsetmacro{\zplane}{\Uz*\uproj + \Wz*\wproj + \Bz*\Bfixed};
  176.                 % Distance to the catcher's viewpoint
  177.                 \pgfmathsetmacro{\xdist}{\xraw};
  178.                 \pgfmathsetmacro{\ydist}{\yraw + (73/12)};
  179.                 \pgfmathsetmacro{\zdist}{\zraw - 2.5};
  180.                 % Spherical coordinates of the pitch
  181.                 \pgfmathsetmacro{\rad}{sqrt(\xdist*\xdist + \ydist*\ydist + \zdist*\zdist)};
  182.                 \pgfmathsetmacro{\theta}{atan2(\xdist,\ydist)};
  183.                 \pgfmathsetmacro{\phi}{acos(\zdist/\rad)};
  184.                 \pgfmathsetmacro{\radscale}{7.5/(sin(\theta)*sin(\phi))};
  185.                 % Location of the pitch from the catcher's perspective
  186.                 \pgfmathsetmacro{\xscale}{\radscale*cos(\theta)*sin(\phi)};
  187.                 \pgfmathsetmacro{\zscale}{\radscale*cos(\phi) + 2.5};
  188.                 \pgfmathsetmacro{\rscale}{(7.5*0.125)/\rad};
  189.                 % Plot the pitch
  190.                 \draw[shading = ball, ball color = white] (\xscale,\zscale) circle [radius=\rscale];
  191.                 % Plot the strike zone
  192.                 \draw[black!85!white] (-17/24,1.5) -- (17/24,1.5) -- (17/24,3.5) -- (-17/24,3.5) -- cycle;
  193.                 % Plot both projections
  194.                 \draw[red!80!white] (\xproj,\zproj) circle [radius=0.125];
  195.                 \draw[yellow!80!white] (\xplane,\zplane) circle [radius=0.125];
  196.                 % Display the diagnostics
  197.                 \node[above right] at (-3,4.4) {Dist.: };
  198.                 \node[above left] at (-1,4.4) {\pgfmathprintnumber[precision=1,fixed]{\yraw}};
  199.                 \node[above right] at (-1.2,4.4) {ft};
  200.                 \node[above right] at (0,4.4) {Vel.: };
  201.                 \node[above left] at (2.1,4.4) {\pgfmathprintnumber[precision=1,fixed]{\vel}};
  202.                 \node[above left] at (3,4.325) {mph};
  203.                 \node[above right] at (-3,0) {Time: };
  204.                 \node[above left] at (-0.9,0) {\pgfmathprintnumber[precision=2,fixed]{\time}};
  205.                 \node[above right] at (-1.1,0) {sec};
  206.             \end{tikzpicture}
  207.             \stepcounter{m}
  208.             \ifthenelse{\them < \frames}{
  209.                 \newframe
  210.             }
  211.             {
  212.                 \end{animateinline} \relax
  213.             }
  214.         }
  215. \end{center}
  216.  
  217. \end{document}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement