Advertisement
matthewrmata

GIF Frames for Swing/Take in Groups

Jan 25th, 2017
205
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
R 21.20 KB | None | 0 0
  1. ############################################################################################################
  2. # Generate images for a GIF for the movement of the pitch based on swing/take with labels for grouped data #
  3. ############################################################################################################
  4.  
  5. #############
  6. # LIBRARIES #
  7. #############
  8.  
  9. library(RODBC);
  10. library(fields);
  11. library(diagram);
  12. library(calibrate);
  13.  
  14. ####################
  15. # ADDITIONAL FILES #
  16. ####################
  17.  
  18. source("Movement Scripts/Mvt_Fcns.r");
  19.  
  20. ########
  21. # MAIN #
  22. ########
  23.  
  24. # Set the pitcher name
  25. first <- 'Julio';
  26. last <- 'Urias';
  27.  
  28. # Set the pitch type based on the two-letter designation
  29. pitch_type <- 'CU';
  30.  
  31. # Set the handedness of the batter
  32. stand <- 'R';
  33.  
  34. # Set the number of frames
  35. Ny <- 30;
  36.  
  37. # Set the increment in the y-direction
  38. y_seq <- seq(55,17/12,length=Ny);
  39.  
  40. # Set the movement type
  41. # 1 <- PITCHf/x movement
  42. # 2 <- PITCHf/x movement without drag
  43. # 3 <- planar movement
  44. # 4 <- planar movement without drag
  45. mvt_type <- 1;
  46.  
  47. # Set the descriptor based on the type of movement
  48. mvt_desc <- switch(mvt_type,'PFX','PFX D','W','W D');
  49.  
  50. # Set the quadrilateral core of the strike zone
  51. if (stand == 'L'){
  52.     UL <- c(-0.47,2.85);
  53.     UR <- c(0.22,2.87);
  54.     LL <- c(-0.5,2.04);
  55.     LR <- c(0.24,2.16);
  56. } else {
  57.     UL <- c(-0.38,2.88);
  58.     UR <- c(0.35,2.81);
  59.     LL <- c(-0.46,2.1);
  60.     LR <- c(0.35,2.09);
  61. }
  62. QUAD <- list( UL = UL, UR = UR, LL = LL, LR = LR );
  63.        
  64. # Associate the pitch type with its full name
  65. pitch_name <- switch(pitch_type,
  66.        FA = 'Fastballs',
  67.        FF = 'Four-Seam Fastballs',
  68.        FT = 'Two-Seam Fastballs',
  69.        FC = 'Cut Fastballs',
  70.        FS = 'Split-Finger Fastballs',
  71.        FO = 'Forkballs',
  72.        SI = 'Sinkers',
  73.        SL = 'Sliders',
  74.        CU = 'Curveballs',
  75.        KC = 'Knuckle Curves',
  76.        EP = 'Ephuuses',
  77.        CH = 'Changeups',
  78.        SC = 'Screwballs',
  79.        KN = 'Knuckleballs',
  80.        UN = 'Unknown Pitches');
  81.  
  82. # Set the cases where a batter has swung at a pitch
  83. swing_cases <- "(des = 'In play, no out' OR
  84.                  des = 'In play, out(s)' OR
  85.                  des = 'In play, run(s)' OR
  86.                  des = 'Foul' OR
  87.                  des = 'Foul (Runner Going)' OR
  88.                  des = 'Foul Bunt' OR
  89.                  des = 'Foul Tip' OR
  90.                  des = 'Swinging Strike' OR
  91.                  des = 'Swinging Pitchout' OR
  92.                  des = 'Missed Bunt' OR
  93.                  des = 'Swinging Strike (Blocked)')";
  94.  
  95. # Set the cases where a batter has taken a pitch
  96. take_cases <- "(des = 'Called Strike' OR
  97.                 des = 'Ball' OR
  98.                 des = 'Ball in Dirt' OR
  99.                 des = 'Hit By Pitch')";
  100.                  
  101. # Open the channel
  102. channel <- odbcConnect('MLB2016');
  103.  
  104. # Set the query for swings
  105. Swing_Query = paste("SELECT x0, y0, z0, vx0, vy0, vz0, ax, ay, az
  106.                      FROM pitches WHERE pitch_type = '",pitch_type,"' AND
  107.                      ",swing_cases," AND ab_id IN
  108.                      (SELECT ab_id FROM atbats WHERE
  109.                      pitcher = (SELECT eliasid FROM players WHERE first = '",
  110.                      first,"' AND last = '",last,"') AND stand = '",stand,"')
  111.                      ",sep="");
  112.  
  113. # Download the swing data
  114. Swing <- sqlQuery(channel,Swing_Query);
  115.  
  116. # Close the channel
  117. close(channel);
  118.  
  119. # Project the swing data and find the direction of the movement
  120. if (mvt_type == 1){
  121.     Swing_xz <- Pitch_Project_Dir_PFX(Swing,QUAD,Ny,stand);
  122. } else if (mvt_type == 2){
  123.     Swing_xz <- Pitch_Project_Dir_PFX_Drag(Swing,QUAD,Ny,stand);
  124. } else if (mvt_type == 3){
  125.     Swing_xz <- Pitch_Project_Dir_W(Swing,QUAD,Ny,stand);
  126. } else {
  127.     Swing_xz <- Pitch_Project_Dir_W_Drag(Swing,QUAD,Ny,stand);
  128. }
  129.  
  130. # Set the x and z coordinates of the swing projections and the movement directions
  131. x_S <- Swing_xz$x_PL;
  132. z_S <- Swing_xz$z_PL;
  133. x_Dir_S <- Swing_xz$x_Dir;
  134. z_Dir_S <- Swing_xz$z_Dir;
  135. Zone_S <- Swing_xz$Zone;
  136.  
  137. # Open the channel
  138. channel <- odbcConnect('MLB2016');
  139.  
  140. # Set the query for taken pitches
  141. Take_Query = paste("SELECT x0, y0, z0, vx0, vy0, vz0, ax, ay, az  
  142.                     FROM pitches WHERE pitch_type = '",pitch_type,"' AND
  143.                     ",take_cases," AND ab_id IN
  144.                     (SELECT ab_id FROM atbats WHERE
  145.                     pitcher = (SELECT eliasid FROM players WHERE first = '",
  146.                     first,"' AND last = '",last,"') AND stand = '",stand,"')
  147.                     ",sep="");
  148.                    
  149. # Download the take data
  150. Take <- sqlQuery(channel,Take_Query);
  151.  
  152. # Close the channel
  153. close(channel);
  154.  
  155. # Project the swing data and find the direction of the movement
  156. if (mvt_type == 1){
  157.     Take_xz <- Pitch_Project_Dir_PFX(Take,QUAD,Ny,stand);
  158. } else if (mvt_type == 2){
  159.     Take_xz <- Pitch_Project_Dir_PFX_Drag(Take,QUAD,Ny,stand);
  160. } else if (mvt_type == 3){
  161.     Take_xz <- Pitch_Project_Dir_W(Take,QUAD,Ny,stand);
  162. } else {
  163.     Take_xz <- Pitch_Project_Dir_W_Drag(Take,QUAD,Ny,stand);
  164. }
  165.  
  166. # Set the x and z coordinates of the swing projections and the movement directions
  167. x_T <- Take_xz$x_PL;
  168. z_T <- Take_xz$z_PL;
  169. x_Dir_T <- Take_xz$x_Dir;
  170. z_Dir_T <- Take_xz$z_Dir;
  171. Zone_T <- Take_xz$Zone;
  172.  
  173. # Find the ball/strike indices
  174. Swing_Ball_Str <- ball_strike_prob(Swing,stand);
  175. Take_Ball_Str <- ball_strike_prob(Take,stand);
  176.  
  177. # Parse the indices
  178. S_Str_ind <- Swing_Ball_Str$S;
  179. S_Ball_ind <- Swing_Ball_Str$B;
  180. T_Str_ind <- Take_Ball_Str$S;
  181. T_Ball_ind <- Take_Ball_Str$B;
  182.  
  183. # Parse the data based on balls and strikes for the projected locations
  184. x_S_Str <- x_S[S_Str_ind,];
  185. z_S_Str <- z_S[S_Str_ind,];
  186. x_S_Ball <- x_S[S_Ball_ind,];
  187. z_S_Ball <- z_S[S_Ball_ind,];
  188. x_T_Str <- x_T[T_Str_ind,];
  189. z_T_Str <- z_T[T_Str_ind,];
  190. x_T_Ball <- x_T[T_Ball_ind,];
  191. z_T_Ball <- z_T[T_Ball_ind,];
  192.  
  193. # Parse the data based on balls and strikes for the projected directions
  194. x_Dir_S_Str <- x_Dir_S[S_Str_ind,];
  195. z_Dir_S_Str <- z_Dir_S[S_Str_ind,];
  196. x_Dir_S_Ball <- x_Dir_S[S_Ball_ind,];
  197. z_Dir_S_Ball <- z_Dir_S[S_Ball_ind,];
  198. x_Dir_T_Str <- x_Dir_T[T_Str_ind,];
  199. z_Dir_T_Str <- z_Dir_T[T_Str_ind,];
  200. x_Dir_T_Ball <- x_Dir_T[T_Ball_ind,];
  201. z_Dir_T_Ball <- z_Dir_T[T_Ball_ind,];
  202.  
  203. # Separate the zones based on cases
  204. Zone_S_Str <- Zone_S[S_Str_ind];
  205. Zone_S_Ball <- Zone_S[S_Ball_ind];
  206. Zone_T_Str <- Zone_T[T_Str_ind];
  207. Zone_T_Ball <- Zone_T[T_Ball_ind];
  208.  
  209. # Zone Population Counters
  210. Zone_Count <- array(0,dim=c(4,9));
  211.  
  212. # Zone 1
  213. S_Ball_1 <- which(Zone_S_Ball == 1);
  214. Zone_Count[2,1] <- length(S_Ball_1);
  215. T_Ball_1 <- which(Zone_T_Ball == 1);
  216. Zone_Count[4,1] <- length(T_Ball_1);
  217.  
  218. # Zone 2
  219. S_Ball_2 <- which(Zone_S_Ball == 2);
  220. Zone_Count[2,2] <- length(S_Ball_2);
  221. T_Ball_2 <- which(Zone_T_Ball == 2);
  222. Zone_Count[4,2] <- length(T_Ball_2);
  223.  
  224. # Zone 3
  225. S_Ball_3 <- which(Zone_S_Ball == 3);
  226. Zone_Count[2,3] <- length(S_Ball_3);
  227. T_Ball_3 <- which(Zone_T_Ball == 3);
  228. Zone_Count[4,3] <- length(T_Ball_3);
  229.  
  230. # Zone 4
  231. S_Ball_4 <- which(Zone_S_Ball == 4);
  232. Zone_Count[2,4] <- length(S_Ball_4);
  233. T_Ball_4 <- which(Zone_T_Ball == 4);
  234. Zone_Count[4,4] <- length(T_Ball_4);
  235.  
  236. # Zone 5
  237. S_Str_5 <- which(Zone_S_Str == 5);
  238. Zone_Count[1,5] <- length(S_Str_5);
  239. T_Str_5 <- which(Zone_T_Str == 5);
  240. Zone_Count[3,5] <- length(T_Str_5);
  241.  
  242. # Zone 6
  243. S_Ball_6 <- which(Zone_S_Ball == 6);
  244. Zone_Count[2,6] <- length(S_Ball_6);
  245. T_Ball_6 <- which(Zone_T_Ball == 6);
  246. Zone_Count[4,6] <- length(T_Ball_6);
  247.  
  248. # Zone 7
  249. S_Ball_7 <- which(Zone_S_Ball == 7);
  250. Zone_Count[2,7] <- length(S_Ball_7);
  251. T_Ball_7 <- which(Zone_T_Ball == 7);
  252. Zone_Count[4,7] <- length(T_Ball_7);
  253.  
  254. # Zone 8
  255. S_Ball_8 <- which(Zone_S_Ball == 8);
  256. Zone_Count[2,8] <- length(S_Ball_8);
  257. T_Ball_8 <- which(Zone_T_Ball == 8);
  258. Zone_Count[4,8] <- length(T_Ball_8);
  259.  
  260. # Zone 9
  261. S_Ball_9 <- which(Zone_S_Ball == 9);
  262. Zone_Count[2,9] <- length(S_Ball_9);
  263. T_Ball_9 <- which(Zone_T_Ball == 9);
  264. Zone_Count[4,9] <- length(T_Ball_9);
  265.  
  266. # Zone 1 Percent
  267. Z_total <- Zone_Count[2,1] + Zone_Count[4,1];
  268. if (Z_total != 0){
  269.     print(paste("Zone 1 Percent: ",100*round(Zone_Count[2,1]/Z_total,digits=3),sep=""));
  270. }
  271.  
  272. # Zone 2 Percent
  273. Z_total <- Zone_Count[2,2] + Zone_Count[4,2];
  274. if (Z_total != 0){
  275.     print(paste("Zone 2 Percent: ",100*round(Zone_Count[2,2]/Z_total,digits=3),sep=""));
  276. }
  277.  
  278. # Zone 3 Percent
  279. Z_total <- Zone_Count[2,3] + Zone_Count[4,3];
  280. if (Z_total != 0){
  281.     print(paste("Zone 3 Percent: ",100*round(Zone_Count[2,3]/Z_total,digits=3),sep=""));
  282. }
  283.  
  284. # Zone 4 Percent
  285. Z_total <- Zone_Count[2,4] + Zone_Count[4,4];
  286. if (Z_total != 0){
  287.     print(paste("Zone 4 Percent: ",100*round(Zone_Count[2,4]/Z_total,digits=3),sep=""));
  288. }
  289.  
  290. # Zone 5 Percent
  291. Z_total <- Zone_Count[1,5] + Zone_Count[3,5];
  292. if (Z_total != 0){
  293.     print(paste("Zone 5 Percent: ",100*round(Zone_Count[1,5]/Z_total,digits=3),sep=""));
  294. }
  295.  
  296. # Zone 6 Percent
  297. Z_total <- Zone_Count[2,6] + Zone_Count[4,6];
  298. if (Z_total != 0){
  299.     print(paste("Zone 6 Percent: ",100*round(Zone_Count[2,6]/Z_total,digits=3),sep=""));
  300. }
  301.  
  302. # Zone 7 Percent
  303. Z_total <- Zone_Count[2,7] + Zone_Count[4,7];
  304. if (Z_total != 0){
  305.     print(paste("Zone 7 Percent: ",100*round(Zone_Count[2,7]/Z_total,digits=3),sep=""));
  306. }
  307.  
  308. # Zone 8 Percent
  309. Z_total <- Zone_Count[2,8] + Zone_Count[4,8];
  310. if (Z_total != 0){
  311.     print(paste("Zone 8 Percent: ",100*round(Zone_Count[2,8]/Z_total,digits=3),sep=""));
  312. }
  313.  
  314. # Zone 9 Percent
  315. Z_total <- Zone_Count[2,9] + Zone_Count[4,9];
  316. if (Z_total != 0){
  317.     print(paste("Zone 9 Percent: ",100*round(Zone_Count[2,9]/Z_total,digits=3),sep=""));
  318. }
  319.  
  320. # Set up an array to draw a strike zone contour
  321. dx <- 0.005;
  322. X <- seq(-3+dx/2,3-dx/2,length=1200);
  323. Y <- seq(dx/2,6-dx/2,length=1200);
  324. k_sz <- 4;
  325. Str_Prb_Array <- array(0,dim=c(1200,1200));
  326. for (i in 1:1200) {
  327.     for (j in 1:1200){
  328.         P <- c(X[i],Y[j]);
  329.         d_sz <- Min_Dist_Quad(P,UL,UR,LL,LR);
  330.         Str_Prb_Array[i,j] <- exp(-k_sz*d_sz^4);
  331.     }
  332. }
  333.  
  334. # Lower the opacity
  335. red_white <- intpalette(c('white','red'),n=11);
  336. orange_white <- intpalette(c('white','darkorange'),n=11);
  337. green_white <- intpalette(c('white','green'),n=11);
  338. blue_white <- intpalette(c('white','blue'),n=11);
  339. fade_ind <- 3;
  340. no_s <- 0.1;
  341.  
  342. # Generate the images
  343. for (j in 1:Ny){
  344.     if (j < 10){
  345.         filename <- paste(last,"_",pitch_type,"_ST_",stand,"_GP_0",j,".jpg",sep="");
  346.     }
  347.     else {
  348.         filename <- paste(last,"_",pitch_type,"_ST_",stand,"_GP_",j,".jpg",sep="");
  349.     }
  350.     jpeg(filename);
  351.     plot(x_S[S_Str_ind,j],z_S[S_Str_ind,j],pch=20,col=green_white[fade_ind],xlim=c(-3,3),ylim=c(0,6),
  352.          xlab='Horizontal Location (Feet)',ylab='Vertical Location (Feet)',
  353.          main=paste(first," ",last,": ",pitch_name," v. ",stand,"HB at ",round(y_seq[j],digits=2)," Feet (",mvt_desc,")",sep=""),
  354.          xaxs='i',yaxs='i');
  355.     par(new=TRUE);
  356.     plot(x_S[S_Ball_ind,j],z_S[S_Ball_ind,j],pch=20,col=blue_white[fade_ind],xlim=c(-3,3),ylim=c(0,6),
  357.          xlab='',ylab='',xaxs='i',yaxs='i');
  358.     par(new=TRUE);
  359.     plot(x_T[T_Str_ind,j],z_T[T_Str_ind,j],pch=20,col=red_white[fade_ind],xlim=c(-3,3),ylim=c(0,6),
  360.          xlab='',ylab='',xaxs='i',yaxs='i');
  361.     par(new=TRUE);
  362.     plot(x_T[T_Ball_ind,j],z_T[T_Ball_ind,j],pch=20,col=orange_white[fade_ind],xlim=c(-3,3),ylim=c(0,6),
  363.          xlab='',ylab='',xaxs='i',yaxs='i');
  364.     par(new=TRUE);
  365.     # Zone 1
  366.     if (Zone_Count[2,1] > 0){
  367.         x_mean <- mean(x_S_Ball[S_Ball_1,j]);
  368.         z_mean <- mean(z_S_Ball[S_Ball_1,j]);
  369.         theta <- mean_angle(x_Dir_S_Ball[S_Ball_1,j],z_Dir_S_Ball[S_Ball_1,j],Zone_Count[2,1]);
  370.         if (x_mean >= -3 && x_mean <= 3 && z_mean >= 0 && z_mean <= 6){
  371.             arrow.plot(x_mean,z_mean,cos(theta),sin(theta),arrow.ex=.05,length=.1,col='blue',lwd=2,xlim=c(-3,3),
  372.                        ylim=c(0,6),xaxs='i',yaxs='i');
  373.             par(new=TRUE);
  374.             textxy(x_mean-no_s*cos(theta),z_mean-no_s*sin(theta),toString(Zone_Count[2,1]),col='blue',cex=1,
  375.                    offset=0,m=c(-3,0));
  376.         }
  377.     }
  378.     if (Zone_Count[4,1] > 0){
  379.         x_mean <- mean(x_T_Ball[T_Ball_1,j]);
  380.         z_mean <- mean(z_T_Ball[T_Ball_1,j]);
  381.         theta <- mean_angle(x_Dir_T_Ball[T_Ball_1,j],z_Dir_T_Ball[T_Ball_1,j],Zone_Count[4,1]);
  382.         if (x_mean >= -3 && x_mean <= 3 && z_mean >= 0 && z_mean <= 6){
  383.             arrow.plot(x_mean,z_mean,cos(theta),sin(theta),arrow.ex=.05,length=.1,col='darkorange',lwd=2,xlim=c(-3,3),
  384.                        ylim=c(0,6),xaxs='i',yaxs='i');
  385.             par(new=TRUE);
  386.             textxy(x_mean-no_s*cos(theta),z_mean-no_s*sin(theta),toString(Zone_Count[4,1]),col='darkorange',cex=1,
  387.                    offset=0,m=c(-3,0));
  388.         }
  389.     }
  390.     # Zone 2
  391.     if (Zone_Count[2,2] > 0){
  392.         x_mean <- mean(x_S_Ball[S_Ball_2,j]);
  393.         z_mean <- mean(z_S_Ball[S_Ball_2,j]);
  394.         theta <- mean_angle(x_Dir_S_Ball[S_Ball_2,j],z_Dir_S_Ball[S_Ball_2,j],Zone_Count[2,2]);
  395.         if (x_mean >= -3 && x_mean <= 3 && z_mean >= 0 && z_mean <= 6){
  396.             arrow.plot(x_mean,z_mean,cos(theta),sin(theta),arrow.ex=.05,length=.1,col='blue',lwd=2,xlim=c(-3,3),
  397.                        ylim=c(0,6),xaxs='i',yaxs='i');
  398.             par(new=TRUE);
  399.             textxy(x_mean-no_s*cos(theta),z_mean-no_s*sin(theta),toString(Zone_Count[2,2]),col='blue',cex=1,
  400.                    offset=0,m=c(-3,0));
  401.         }
  402.     }
  403.     if (Zone_Count[4,2] > 0){
  404.         x_mean <- mean(x_T_Ball[T_Ball_2,j]);
  405.         z_mean <- mean(z_T_Ball[T_Ball_2,j]);
  406.         theta <- mean_angle(x_Dir_T_Ball[T_Ball_2,j],z_Dir_T_Ball[T_Ball_2,j],Zone_Count[4,2]);
  407.         if (x_mean >= -3 && x_mean <= 3 && z_mean >= 0 && z_mean <= 6){
  408.             arrow.plot(x_mean,z_mean,cos(theta),sin(theta),arrow.ex=.05,length=.1,col='darkorange',lwd=2,xlim=c(-3,3),
  409.                        ylim=c(0,6),xaxs='i',yaxs='i');
  410.             par(new=TRUE);
  411.             textxy(x_mean-no_s*cos(theta),z_mean-no_s*sin(theta),toString(Zone_Count[4,2]),col='darkorange',cex=1,
  412.                    offset=0,m=c(-3,0));
  413.         }
  414.     }
  415.     # Zone 3
  416.     if (Zone_Count[2,3] > 0){
  417.         x_mean <- mean(x_S_Ball[S_Ball_3,j]);
  418.         z_mean <- mean(z_S_Ball[S_Ball_3,j]);
  419.         theta <- mean_angle(x_Dir_S_Ball[S_Ball_3,j],z_Dir_S_Ball[S_Ball_3,j],Zone_Count[2,3]);
  420.         if (x_mean >= -3 && x_mean <= 3 && z_mean >= 0 && z_mean <= 6){
  421.             arrow.plot(x_mean,z_mean,cos(theta),sin(theta),arrow.ex=.05,length=.1,col='blue',lwd=2,xlim=c(-3,3),
  422.                        ylim=c(0,6),xaxs='i',yaxs='i');
  423.             par(new=TRUE);
  424.             textxy(x_mean-no_s*cos(theta),z_mean-no_s*sin(theta),toString(Zone_Count[2,3]),col='blue',cex=1,
  425.                    offset=0,m=c(-3,0));
  426.         }
  427.     }
  428.     if (Zone_Count[4,3] > 0){
  429.         x_mean <- mean(x_T_Ball[T_Ball_3,j]);
  430.         z_mean <- mean(z_T_Ball[T_Ball_3,j]);
  431.         theta <- mean_angle(x_Dir_T_Ball[T_Ball_3,j],z_Dir_T_Ball[T_Ball_3,j],Zone_Count[4,3]);
  432.         if (x_mean >= -3 && x_mean <= 3 && z_mean >= 0 && z_mean <= 6){
  433.             arrow.plot(x_mean,z_mean,cos(theta),sin(theta),arrow.ex=.05,length=.1,col='darkorange',lwd=2,xlim=c(-3,3),
  434.                        ylim=c(0,6),xaxs='i',yaxs='i');
  435.             par(new=TRUE);
  436.             textxy(x_mean-no_s*cos(theta),z_mean-no_s*sin(theta),toString(Zone_Count[4,3]),col='darkorange',cex=1,
  437.                    offset=0,m=c(-3,0));
  438.         }
  439.     }
  440.     # Zone 4
  441.     if (Zone_Count[2,4] > 0){
  442.         x_mean <- mean(x_S_Ball[S_Ball_4,j]);
  443.         z_mean <- mean(z_S_Ball[S_Ball_4,j]);
  444.         theta <- mean_angle(x_Dir_S_Ball[S_Ball_4,j],z_Dir_S_Ball[S_Ball_4,j],Zone_Count[2,4]);
  445.         if (x_mean >= -3 && x_mean <= 3 && z_mean >= 0 && z_mean <= 6){
  446.             arrow.plot(x_mean,z_mean,cos(theta),sin(theta),arrow.ex=.05,length=.1,col='blue',lwd=2,xlim=c(-3,3),
  447.                        ylim=c(0,6),xaxs='i',yaxs='i');
  448.             par(new=TRUE);
  449.             textxy(x_mean-no_s*cos(theta),z_mean-no_s*sin(theta),toString(Zone_Count[2,4]),col='blue',cex=1,
  450.                    offset=0,m=c(-3,0));
  451.         }
  452.     }
  453.     if (Zone_Count[4,4] > 0){
  454.         x_mean <- mean(x_T_Ball[T_Ball_4,j]);
  455.         z_mean <- mean(z_T_Ball[T_Ball_4,j]);
  456.         theta <- mean_angle(x_Dir_T_Ball[T_Ball_4,j],z_Dir_T_Ball[T_Ball_4,j],Zone_Count[4,4]);
  457.         if (x_mean >= -3 && x_mean <= 3 && z_mean >= 0 && z_mean <= 6){
  458.             arrow.plot(x_mean,z_mean,cos(theta),sin(theta),arrow.ex=.05,length=.1,col='darkorange',lwd=2,xlim=c(-3,3),
  459.                        ylim=c(0,6),xaxs='i',yaxs='i');
  460.             par(new=TRUE);
  461.             textxy(x_mean-no_s*cos(theta),z_mean-no_s*sin(theta),toString(Zone_Count[4,4]),col='darkorange',cex=1,
  462.                    offset=0,m=c(-3,0));
  463.         }
  464.     }
  465.         # Zone 5
  466.     if (Zone_Count[1,5] > 0){
  467.         x_mean <- mean(x_S_Str[S_Str_5,j]);
  468.         z_mean <- mean(z_S_Str[S_Str_5,j]);
  469.         theta <- mean_angle(x_Dir_S_Str[S_Str_5,j],z_Dir_S_Str[S_Str_5,j],Zone_Count[1,5]);
  470.         if (x_mean >= -3 && x_mean <= 3 && z_mean >= 0 && z_mean <= 6){
  471.             arrow.plot(x_mean,z_mean,cos(theta),sin(theta),arrow.ex=.05,length=.1,col='green',lwd=2,xlim=c(-3,3),
  472.                        ylim=c(0,6),xaxs='i',yaxs='i');
  473.             par(new=TRUE);
  474.             textxy(x_mean-no_s*cos(theta),z_mean-no_s*sin(theta),toString(Zone_Count[1,5]),col='green',cex=1,
  475.                    offset=0,m=c(-3,0));
  476.         }
  477.     }
  478.     if (Zone_Count[3,5] > 0){
  479.         x_mean <- mean(x_T_Str[T_Str_5,j]);
  480.         z_mean <- mean(z_T_Str[T_Str_5,j]);
  481.         theta <- mean_angle(x_Dir_T_Str[T_Str_5,j],z_Dir_T_Str[T_Str_5,j],Zone_Count[3,5]);
  482.         if (x_mean >= -3 && x_mean <= 3 && z_mean >= 0 && z_mean <= 6){
  483.             arrow.plot(x_mean,z_mean,cos(theta),sin(theta),arrow.ex=.05,length=.1,col='red',lwd=2,xlim=c(-3,3),
  484.                        ylim=c(0,6),xaxs='i',yaxs='i');
  485.             par(new=TRUE);
  486.             textxy(x_mean-no_s*cos(theta),z_mean-no_s*sin(theta),toString(Zone_Count[3,5]),col='red',cex=1,
  487.                    offset=0,m=c(-3,0));
  488.         }
  489.     }
  490.     # Zone 6
  491.     if (Zone_Count[2,6] > 0){
  492.         x_mean <- mean(x_S_Ball[S_Ball_6,j]);
  493.         z_mean <- mean(z_S_Ball[S_Ball_6,j]);
  494.         theta <- mean_angle(x_Dir_S_Ball[S_Ball_6,j],z_Dir_S_Ball[S_Ball_6,j],Zone_Count[2,6]);
  495.         if (x_mean >= -3 && x_mean <= 3 && z_mean >= 0 && z_mean <= 6){
  496.             arrow.plot(x_mean,z_mean,cos(theta),sin(theta),arrow.ex=.05,length=.1,col='blue',lwd=2,xlim=c(-3,3),
  497.                        ylim=c(0,6),xaxs='i',yaxs='i');
  498.             par(new=TRUE);
  499.             textxy(x_mean-no_s*cos(theta),z_mean-no_s*sin(theta),toString(Zone_Count[2,6]),col='blue',cex=1,
  500.                    offset=0,m=c(-3,0));
  501.         }
  502.     }
  503.     if (Zone_Count[4,6] > 0){
  504.         x_mean <- mean(x_T_Ball[T_Ball_6,j]);
  505.         z_mean <- mean(z_T_Ball[T_Ball_6,j]);
  506.         theta <- mean_angle(x_Dir_T_Ball[T_Ball_6,j],z_Dir_T_Ball[T_Ball_6,j],Zone_Count[4,6]);
  507.         if (x_mean >= -3 && x_mean <= 3 && z_mean >= 0 && z_mean <= 6){
  508.             arrow.plot(x_mean,z_mean,cos(theta),sin(theta),arrow.ex=.05,length=.1,col='darkorange',lwd=2,xlim=c(-3,3),
  509.                        ylim=c(0,6),xaxs='i',yaxs='i');
  510.             par(new=TRUE);
  511.             textxy(x_mean-no_s*cos(theta),z_mean-no_s*sin(theta),toString(Zone_Count[4,6]),col='darkorange',cex=1,
  512.                    offset=0,m=c(-3,0));
  513.         }
  514.     }
  515.     # Zone 7
  516.     if (Zone_Count[2,7] > 0){
  517.         x_mean <- mean(x_S_Ball[S_Ball_7,j]);
  518.         z_mean <- mean(z_S_Ball[S_Ball_7,j]);
  519.         theta <- mean_angle(x_Dir_S_Ball[S_Ball_7,j],z_Dir_S_Ball[S_Ball_7,j],Zone_Count[2,7]);
  520.         if (x_mean >= -3 && x_mean <= 3 && z_mean >= 0 && z_mean <= 6){
  521.             arrow.plot(x_mean,z_mean,cos(theta),sin(theta),arrow.ex=.05,length=.1,col='blue',lwd=2,xlim=c(-3,3),
  522.                        ylim=c(0,6),xaxs='i',yaxs='i');
  523.             par(new=TRUE);
  524.             textxy(x_mean-no_s*cos(theta),z_mean-no_s*sin(theta),toString(Zone_Count[2,7]),col='blue',cex=1,
  525.                    offset=0,m=c(-3,0));
  526.         }
  527.     }
  528.     if (Zone_Count[4,7] > 0){
  529.         x_mean <- mean(x_T_Ball[T_Ball_7,j]);
  530.         z_mean <- mean(z_T_Ball[T_Ball_7,j]);
  531.         theta <- mean_angle(x_Dir_T_Ball[T_Ball_7,j],z_Dir_T_Ball[T_Ball_7,j],Zone_Count[4,7]);
  532.         if (x_mean >= -3 && x_mean <= 3 && z_mean >= 0 && z_mean <= 6){
  533.             arrow.plot(x_mean,z_mean,cos(theta),sin(theta),arrow.ex=.05,length=.1,col='darkorange',lwd=2,xlim=c(-3,3),
  534.                        ylim=c(0,6),xaxs='i',yaxs='i');
  535.             par(new=TRUE);
  536.             textxy(x_mean-no_s*cos(theta),z_mean-no_s*sin(theta),toString(Zone_Count[4,7]),col='darkorange',cex=1,
  537.                    offset=0,m=c(-3,0));
  538.         }
  539.     }
  540.     # Zone 8
  541.     if (Zone_Count[2,8] > 0){
  542.         x_mean <- mean(x_S_Ball[S_Ball_8,j]);
  543.         z_mean <- mean(z_S_Ball[S_Ball_8,j]);
  544.         theta <- mean_angle(x_Dir_S_Ball[S_Ball_8,j],z_Dir_S_Ball[S_Ball_8,j],Zone_Count[2,8]);
  545.         if (x_mean >= -3 && x_mean <= 3 && z_mean >= 0 && z_mean <= 6){
  546.             arrow.plot(x_mean,z_mean,cos(theta),sin(theta),arrow.ex=.05,length=.1,col='blue',lwd=2,xlim=c(-3,3),
  547.                        ylim=c(0,6),xaxs='i',yaxs='i');
  548.             par(new=TRUE);
  549.             textxy(x_mean-no_s*cos(theta),z_mean-no_s*sin(theta),toString(Zone_Count[2,8]),col='blue',cex=1,
  550.                    offset=0,m=c(-3,0));
  551.         }
  552.     }
  553.     if (Zone_Count[4,8] > 0){
  554.         x_mean <- mean(x_T_Ball[T_Ball_8,j]);
  555.         z_mean <- mean(z_T_Ball[T_Ball_8,j]);
  556.         theta <- mean_angle(x_Dir_T_Ball[T_Ball_8,j],z_Dir_T_Ball[T_Ball_8,j],Zone_Count[4,8]);
  557.         if (x_mean >= -3 && x_mean <= 3 && z_mean >= 0 && z_mean <= 6){
  558.             arrow.plot(x_mean,z_mean,cos(theta),sin(theta),arrow.ex=.05,length=.1,col='darkorange',lwd=2,xlim=c(-3,3),
  559.                        ylim=c(0,6),xaxs='i',yaxs='i');
  560.             par(new=TRUE);
  561.             textxy(x_mean-no_s*cos(theta),z_mean-no_s*sin(theta),toString(Zone_Count[4,8]),col='darkorange',cex=1,
  562.                    offset=0,m=c(-3,0));
  563.         }
  564.     }
  565.     # Zone 9
  566.     if (Zone_Count[2,9] > 0){
  567.         x_mean <- mean(x_S_Ball[S_Ball_9,j]);
  568.         z_mean <- mean(z_S_Ball[S_Ball_9,j]);
  569.         theta <- mean_angle(x_Dir_S_Ball[S_Ball_9,j],z_Dir_S_Ball[S_Ball_9,j],Zone_Count[2,9]);
  570.         if (x_mean >= -3 && x_mean <= 3 && z_mean >= 0 && z_mean <= 6){
  571.             arrow.plot(x_mean,z_mean,cos(theta),sin(theta),arrow.ex=.05,length=.1,col='blue',lwd=2,xlim=c(-3,3),
  572.                        ylim=c(0,6),xaxs='i',yaxs='i');
  573.             par(new=TRUE);
  574.             textxy(x_mean-no_s*cos(theta),z_mean-no_s*sin(theta),toString(Zone_Count[2,9]),col='blue',cex=1,
  575.                    offset=0,m=c(-3,0));
  576.         }
  577.     }
  578.     if (Zone_Count[4,9] > 0){
  579.         x_mean <- mean(x_T_Ball[T_Ball_9,j]);
  580.         z_mean <- mean(z_T_Ball[T_Ball_9,j]);
  581.         theta <- mean_angle(x_Dir_T_Ball[T_Ball_9,j],z_Dir_T_Ball[T_Ball_9,j],Zone_Count[4,9]);
  582.         if (x_mean >= -3 && x_mean <= 3 && z_mean >= 0 && z_mean <= 6){
  583.             arrow.plot(x_mean,z_mean,cos(theta),sin(theta),arrow.ex=.05,length=.1,col='darkorange',lwd=2,xlim=c(-3,3),
  584.                        ylim=c(0,6),xaxs='i',yaxs='i');
  585.             par(new=TRUE);
  586.             textxy(x_mean-no_s*cos(theta),z_mean-no_s*sin(theta),toString(Zone_Count[4,9]),col='darkorange',cex=1,
  587.                    offset=0,m=c(-3,0));
  588.         }
  589.     }
  590.     par(new=TRUE);
  591.     contour(X,Y,Str_Prb_Array,levels=c(0.5),drawlabels=FALSE,col='black',xlim=c(-3,3),ylim=c(0,6),xaxs='i',yaxs='i',lwd=2);
  592.     par(new=FALSE);
  593.     dev.off();
  594. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement