Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ############################################################################################################
- # Generate images for a GIF for the movement of the pitch based on swing/take with labels for grouped data #
- ############################################################################################################
- #############
- # LIBRARIES #
- #############
- library(RODBC);
- library(fields);
- library(diagram);
- library(calibrate);
- ####################
- # ADDITIONAL FILES #
- ####################
- source("Movement Scripts/Mvt_Fcns.r");
- ########
- # MAIN #
- ########
- # Set the pitcher name
- first <- 'Julio';
- last <- 'Urias';
- # Set the pitch type based on the two-letter designation
- pitch_type <- 'CU';
- # Set the handedness of the batter
- stand <- 'R';
- # Set the number of frames
- Ny <- 30;
- # Set the increment in the y-direction
- y_seq <- seq(55,17/12,length=Ny);
- # Set the movement type
- # 1 <- PITCHf/x movement
- # 2 <- PITCHf/x movement without drag
- # 3 <- planar movement
- # 4 <- planar movement without drag
- mvt_type <- 1;
- # Set the descriptor based on the type of movement
- mvt_desc <- switch(mvt_type,'PFX','PFX D','W','W D');
- # Set the quadrilateral core of the strike zone
- if (stand == 'L'){
- UL <- c(-0.47,2.85);
- UR <- c(0.22,2.87);
- LL <- c(-0.5,2.04);
- LR <- c(0.24,2.16);
- } else {
- UL <- c(-0.38,2.88);
- UR <- c(0.35,2.81);
- LL <- c(-0.46,2.1);
- LR <- c(0.35,2.09);
- }
- QUAD <- list( UL = UL, UR = UR, LL = LL, LR = LR );
- # Associate the pitch type with its full name
- pitch_name <- switch(pitch_type,
- FA = 'Fastballs',
- FF = 'Four-Seam Fastballs',
- FT = 'Two-Seam Fastballs',
- FC = 'Cut Fastballs',
- FS = 'Split-Finger Fastballs',
- FO = 'Forkballs',
- SI = 'Sinkers',
- SL = 'Sliders',
- CU = 'Curveballs',
- KC = 'Knuckle Curves',
- EP = 'Ephuuses',
- CH = 'Changeups',
- SC = 'Screwballs',
- KN = 'Knuckleballs',
- UN = 'Unknown Pitches');
- # Set the cases where a batter has swung at a pitch
- swing_cases <- "(des = 'In play, no out' OR
- des = 'In play, out(s)' OR
- des = 'In play, run(s)' OR
- des = 'Foul' OR
- des = 'Foul (Runner Going)' OR
- des = 'Foul Bunt' OR
- des = 'Foul Tip' OR
- des = 'Swinging Strike' OR
- des = 'Swinging Pitchout' OR
- des = 'Missed Bunt' OR
- des = 'Swinging Strike (Blocked)')";
- # Set the cases where a batter has taken a pitch
- take_cases <- "(des = 'Called Strike' OR
- des = 'Ball' OR
- des = 'Ball in Dirt' OR
- des = 'Hit By Pitch')";
- # Open the channel
- channel <- odbcConnect('MLB2016');
- # Set the query for swings
- Swing_Query = paste("SELECT x0, y0, z0, vx0, vy0, vz0, ax, ay, az
- FROM pitches WHERE pitch_type = '",pitch_type,"' AND
- ",swing_cases," AND ab_id IN
- (SELECT ab_id FROM atbats WHERE
- pitcher = (SELECT eliasid FROM players WHERE first = '",
- first,"' AND last = '",last,"') AND stand = '",stand,"')
- ",sep="");
- # Download the swing data
- Swing <- sqlQuery(channel,Swing_Query);
- # Close the channel
- close(channel);
- # Project the swing data and find the direction of the movement
- if (mvt_type == 1){
- Swing_xz <- Pitch_Project_Dir_PFX(Swing,QUAD,Ny,stand);
- } else if (mvt_type == 2){
- Swing_xz <- Pitch_Project_Dir_PFX_Drag(Swing,QUAD,Ny,stand);
- } else if (mvt_type == 3){
- Swing_xz <- Pitch_Project_Dir_W(Swing,QUAD,Ny,stand);
- } else {
- Swing_xz <- Pitch_Project_Dir_W_Drag(Swing,QUAD,Ny,stand);
- }
- # Set the x and z coordinates of the swing projections and the movement directions
- x_S <- Swing_xz$x_PL;
- z_S <- Swing_xz$z_PL;
- x_Dir_S <- Swing_xz$x_Dir;
- z_Dir_S <- Swing_xz$z_Dir;
- Zone_S <- Swing_xz$Zone;
- # Open the channel
- channel <- odbcConnect('MLB2016');
- # Set the query for taken pitches
- Take_Query = paste("SELECT x0, y0, z0, vx0, vy0, vz0, ax, ay, az
- FROM pitches WHERE pitch_type = '",pitch_type,"' AND
- ",take_cases," AND ab_id IN
- (SELECT ab_id FROM atbats WHERE
- pitcher = (SELECT eliasid FROM players WHERE first = '",
- first,"' AND last = '",last,"') AND stand = '",stand,"')
- ",sep="");
- # Download the take data
- Take <- sqlQuery(channel,Take_Query);
- # Close the channel
- close(channel);
- # Project the swing data and find the direction of the movement
- if (mvt_type == 1){
- Take_xz <- Pitch_Project_Dir_PFX(Take,QUAD,Ny,stand);
- } else if (mvt_type == 2){
- Take_xz <- Pitch_Project_Dir_PFX_Drag(Take,QUAD,Ny,stand);
- } else if (mvt_type == 3){
- Take_xz <- Pitch_Project_Dir_W(Take,QUAD,Ny,stand);
- } else {
- Take_xz <- Pitch_Project_Dir_W_Drag(Take,QUAD,Ny,stand);
- }
- # Set the x and z coordinates of the swing projections and the movement directions
- x_T <- Take_xz$x_PL;
- z_T <- Take_xz$z_PL;
- x_Dir_T <- Take_xz$x_Dir;
- z_Dir_T <- Take_xz$z_Dir;
- Zone_T <- Take_xz$Zone;
- # Find the ball/strike indices
- Swing_Ball_Str <- ball_strike_prob(Swing,stand);
- Take_Ball_Str <- ball_strike_prob(Take,stand);
- # Parse the indices
- S_Str_ind <- Swing_Ball_Str$S;
- S_Ball_ind <- Swing_Ball_Str$B;
- T_Str_ind <- Take_Ball_Str$S;
- T_Ball_ind <- Take_Ball_Str$B;
- # Parse the data based on balls and strikes for the projected locations
- x_S_Str <- x_S[S_Str_ind,];
- z_S_Str <- z_S[S_Str_ind,];
- x_S_Ball <- x_S[S_Ball_ind,];
- z_S_Ball <- z_S[S_Ball_ind,];
- x_T_Str <- x_T[T_Str_ind,];
- z_T_Str <- z_T[T_Str_ind,];
- x_T_Ball <- x_T[T_Ball_ind,];
- z_T_Ball <- z_T[T_Ball_ind,];
- # Parse the data based on balls and strikes for the projected directions
- x_Dir_S_Str <- x_Dir_S[S_Str_ind,];
- z_Dir_S_Str <- z_Dir_S[S_Str_ind,];
- x_Dir_S_Ball <- x_Dir_S[S_Ball_ind,];
- z_Dir_S_Ball <- z_Dir_S[S_Ball_ind,];
- x_Dir_T_Str <- x_Dir_T[T_Str_ind,];
- z_Dir_T_Str <- z_Dir_T[T_Str_ind,];
- x_Dir_T_Ball <- x_Dir_T[T_Ball_ind,];
- z_Dir_T_Ball <- z_Dir_T[T_Ball_ind,];
- # Separate the zones based on cases
- Zone_S_Str <- Zone_S[S_Str_ind];
- Zone_S_Ball <- Zone_S[S_Ball_ind];
- Zone_T_Str <- Zone_T[T_Str_ind];
- Zone_T_Ball <- Zone_T[T_Ball_ind];
- # Zone Population Counters
- Zone_Count <- array(0,dim=c(4,9));
- # Zone 1
- S_Ball_1 <- which(Zone_S_Ball == 1);
- Zone_Count[2,1] <- length(S_Ball_1);
- T_Ball_1 <- which(Zone_T_Ball == 1);
- Zone_Count[4,1] <- length(T_Ball_1);
- # Zone 2
- S_Ball_2 <- which(Zone_S_Ball == 2);
- Zone_Count[2,2] <- length(S_Ball_2);
- T_Ball_2 <- which(Zone_T_Ball == 2);
- Zone_Count[4,2] <- length(T_Ball_2);
- # Zone 3
- S_Ball_3 <- which(Zone_S_Ball == 3);
- Zone_Count[2,3] <- length(S_Ball_3);
- T_Ball_3 <- which(Zone_T_Ball == 3);
- Zone_Count[4,3] <- length(T_Ball_3);
- # Zone 4
- S_Ball_4 <- which(Zone_S_Ball == 4);
- Zone_Count[2,4] <- length(S_Ball_4);
- T_Ball_4 <- which(Zone_T_Ball == 4);
- Zone_Count[4,4] <- length(T_Ball_4);
- # Zone 5
- S_Str_5 <- which(Zone_S_Str == 5);
- Zone_Count[1,5] <- length(S_Str_5);
- T_Str_5 <- which(Zone_T_Str == 5);
- Zone_Count[3,5] <- length(T_Str_5);
- # Zone 6
- S_Ball_6 <- which(Zone_S_Ball == 6);
- Zone_Count[2,6] <- length(S_Ball_6);
- T_Ball_6 <- which(Zone_T_Ball == 6);
- Zone_Count[4,6] <- length(T_Ball_6);
- # Zone 7
- S_Ball_7 <- which(Zone_S_Ball == 7);
- Zone_Count[2,7] <- length(S_Ball_7);
- T_Ball_7 <- which(Zone_T_Ball == 7);
- Zone_Count[4,7] <- length(T_Ball_7);
- # Zone 8
- S_Ball_8 <- which(Zone_S_Ball == 8);
- Zone_Count[2,8] <- length(S_Ball_8);
- T_Ball_8 <- which(Zone_T_Ball == 8);
- Zone_Count[4,8] <- length(T_Ball_8);
- # Zone 9
- S_Ball_9 <- which(Zone_S_Ball == 9);
- Zone_Count[2,9] <- length(S_Ball_9);
- T_Ball_9 <- which(Zone_T_Ball == 9);
- Zone_Count[4,9] <- length(T_Ball_9);
- # Zone 1 Percent
- Z_total <- Zone_Count[2,1] + Zone_Count[4,1];
- if (Z_total != 0){
- print(paste("Zone 1 Percent: ",100*round(Zone_Count[2,1]/Z_total,digits=3),sep=""));
- }
- # Zone 2 Percent
- Z_total <- Zone_Count[2,2] + Zone_Count[4,2];
- if (Z_total != 0){
- print(paste("Zone 2 Percent: ",100*round(Zone_Count[2,2]/Z_total,digits=3),sep=""));
- }
- # Zone 3 Percent
- Z_total <- Zone_Count[2,3] + Zone_Count[4,3];
- if (Z_total != 0){
- print(paste("Zone 3 Percent: ",100*round(Zone_Count[2,3]/Z_total,digits=3),sep=""));
- }
- # Zone 4 Percent
- Z_total <- Zone_Count[2,4] + Zone_Count[4,4];
- if (Z_total != 0){
- print(paste("Zone 4 Percent: ",100*round(Zone_Count[2,4]/Z_total,digits=3),sep=""));
- }
- # Zone 5 Percent
- Z_total <- Zone_Count[1,5] + Zone_Count[3,5];
- if (Z_total != 0){
- print(paste("Zone 5 Percent: ",100*round(Zone_Count[1,5]/Z_total,digits=3),sep=""));
- }
- # Zone 6 Percent
- Z_total <- Zone_Count[2,6] + Zone_Count[4,6];
- if (Z_total != 0){
- print(paste("Zone 6 Percent: ",100*round(Zone_Count[2,6]/Z_total,digits=3),sep=""));
- }
- # Zone 7 Percent
- Z_total <- Zone_Count[2,7] + Zone_Count[4,7];
- if (Z_total != 0){
- print(paste("Zone 7 Percent: ",100*round(Zone_Count[2,7]/Z_total,digits=3),sep=""));
- }
- # Zone 8 Percent
- Z_total <- Zone_Count[2,8] + Zone_Count[4,8];
- if (Z_total != 0){
- print(paste("Zone 8 Percent: ",100*round(Zone_Count[2,8]/Z_total,digits=3),sep=""));
- }
- # Zone 9 Percent
- Z_total <- Zone_Count[2,9] + Zone_Count[4,9];
- if (Z_total != 0){
- print(paste("Zone 9 Percent: ",100*round(Zone_Count[2,9]/Z_total,digits=3),sep=""));
- }
- # Set up an array to draw a strike zone contour
- dx <- 0.005;
- X <- seq(-3+dx/2,3-dx/2,length=1200);
- Y <- seq(dx/2,6-dx/2,length=1200);
- k_sz <- 4;
- Str_Prb_Array <- array(0,dim=c(1200,1200));
- for (i in 1:1200) {
- for (j in 1:1200){
- P <- c(X[i],Y[j]);
- d_sz <- Min_Dist_Quad(P,UL,UR,LL,LR);
- Str_Prb_Array[i,j] <- exp(-k_sz*d_sz^4);
- }
- }
- # Lower the opacity
- red_white <- intpalette(c('white','red'),n=11);
- orange_white <- intpalette(c('white','darkorange'),n=11);
- green_white <- intpalette(c('white','green'),n=11);
- blue_white <- intpalette(c('white','blue'),n=11);
- fade_ind <- 3;
- no_s <- 0.1;
- # Generate the images
- for (j in 1:Ny){
- if (j < 10){
- filename <- paste(last,"_",pitch_type,"_ST_",stand,"_GP_0",j,".jpg",sep="");
- }
- else {
- filename <- paste(last,"_",pitch_type,"_ST_",stand,"_GP_",j,".jpg",sep="");
- }
- jpeg(filename);
- 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),
- xlab='Horizontal Location (Feet)',ylab='Vertical Location (Feet)',
- main=paste(first," ",last,": ",pitch_name," v. ",stand,"HB at ",round(y_seq[j],digits=2)," Feet (",mvt_desc,")",sep=""),
- xaxs='i',yaxs='i');
- par(new=TRUE);
- 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),
- xlab='',ylab='',xaxs='i',yaxs='i');
- par(new=TRUE);
- 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),
- xlab='',ylab='',xaxs='i',yaxs='i');
- par(new=TRUE);
- 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),
- xlab='',ylab='',xaxs='i',yaxs='i');
- par(new=TRUE);
- # Zone 1
- if (Zone_Count[2,1] > 0){
- x_mean <- mean(x_S_Ball[S_Ball_1,j]);
- z_mean <- mean(z_S_Ball[S_Ball_1,j]);
- theta <- mean_angle(x_Dir_S_Ball[S_Ball_1,j],z_Dir_S_Ball[S_Ball_1,j],Zone_Count[2,1]);
- if (x_mean >= -3 && x_mean <= 3 && z_mean >= 0 && z_mean <= 6){
- arrow.plot(x_mean,z_mean,cos(theta),sin(theta),arrow.ex=.05,length=.1,col='blue',lwd=2,xlim=c(-3,3),
- ylim=c(0,6),xaxs='i',yaxs='i');
- par(new=TRUE);
- textxy(x_mean-no_s*cos(theta),z_mean-no_s*sin(theta),toString(Zone_Count[2,1]),col='blue',cex=1,
- offset=0,m=c(-3,0));
- }
- }
- if (Zone_Count[4,1] > 0){
- x_mean <- mean(x_T_Ball[T_Ball_1,j]);
- z_mean <- mean(z_T_Ball[T_Ball_1,j]);
- theta <- mean_angle(x_Dir_T_Ball[T_Ball_1,j],z_Dir_T_Ball[T_Ball_1,j],Zone_Count[4,1]);
- if (x_mean >= -3 && x_mean <= 3 && z_mean >= 0 && z_mean <= 6){
- arrow.plot(x_mean,z_mean,cos(theta),sin(theta),arrow.ex=.05,length=.1,col='darkorange',lwd=2,xlim=c(-3,3),
- ylim=c(0,6),xaxs='i',yaxs='i');
- par(new=TRUE);
- textxy(x_mean-no_s*cos(theta),z_mean-no_s*sin(theta),toString(Zone_Count[4,1]),col='darkorange',cex=1,
- offset=0,m=c(-3,0));
- }
- }
- # Zone 2
- if (Zone_Count[2,2] > 0){
- x_mean <- mean(x_S_Ball[S_Ball_2,j]);
- z_mean <- mean(z_S_Ball[S_Ball_2,j]);
- theta <- mean_angle(x_Dir_S_Ball[S_Ball_2,j],z_Dir_S_Ball[S_Ball_2,j],Zone_Count[2,2]);
- if (x_mean >= -3 && x_mean <= 3 && z_mean >= 0 && z_mean <= 6){
- arrow.plot(x_mean,z_mean,cos(theta),sin(theta),arrow.ex=.05,length=.1,col='blue',lwd=2,xlim=c(-3,3),
- ylim=c(0,6),xaxs='i',yaxs='i');
- par(new=TRUE);
- textxy(x_mean-no_s*cos(theta),z_mean-no_s*sin(theta),toString(Zone_Count[2,2]),col='blue',cex=1,
- offset=0,m=c(-3,0));
- }
- }
- if (Zone_Count[4,2] > 0){
- x_mean <- mean(x_T_Ball[T_Ball_2,j]);
- z_mean <- mean(z_T_Ball[T_Ball_2,j]);
- theta <- mean_angle(x_Dir_T_Ball[T_Ball_2,j],z_Dir_T_Ball[T_Ball_2,j],Zone_Count[4,2]);
- if (x_mean >= -3 && x_mean <= 3 && z_mean >= 0 && z_mean <= 6){
- arrow.plot(x_mean,z_mean,cos(theta),sin(theta),arrow.ex=.05,length=.1,col='darkorange',lwd=2,xlim=c(-3,3),
- ylim=c(0,6),xaxs='i',yaxs='i');
- par(new=TRUE);
- textxy(x_mean-no_s*cos(theta),z_mean-no_s*sin(theta),toString(Zone_Count[4,2]),col='darkorange',cex=1,
- offset=0,m=c(-3,0));
- }
- }
- # Zone 3
- if (Zone_Count[2,3] > 0){
- x_mean <- mean(x_S_Ball[S_Ball_3,j]);
- z_mean <- mean(z_S_Ball[S_Ball_3,j]);
- theta <- mean_angle(x_Dir_S_Ball[S_Ball_3,j],z_Dir_S_Ball[S_Ball_3,j],Zone_Count[2,3]);
- if (x_mean >= -3 && x_mean <= 3 && z_mean >= 0 && z_mean <= 6){
- arrow.plot(x_mean,z_mean,cos(theta),sin(theta),arrow.ex=.05,length=.1,col='blue',lwd=2,xlim=c(-3,3),
- ylim=c(0,6),xaxs='i',yaxs='i');
- par(new=TRUE);
- textxy(x_mean-no_s*cos(theta),z_mean-no_s*sin(theta),toString(Zone_Count[2,3]),col='blue',cex=1,
- offset=0,m=c(-3,0));
- }
- }
- if (Zone_Count[4,3] > 0){
- x_mean <- mean(x_T_Ball[T_Ball_3,j]);
- z_mean <- mean(z_T_Ball[T_Ball_3,j]);
- theta <- mean_angle(x_Dir_T_Ball[T_Ball_3,j],z_Dir_T_Ball[T_Ball_3,j],Zone_Count[4,3]);
- if (x_mean >= -3 && x_mean <= 3 && z_mean >= 0 && z_mean <= 6){
- arrow.plot(x_mean,z_mean,cos(theta),sin(theta),arrow.ex=.05,length=.1,col='darkorange',lwd=2,xlim=c(-3,3),
- ylim=c(0,6),xaxs='i',yaxs='i');
- par(new=TRUE);
- textxy(x_mean-no_s*cos(theta),z_mean-no_s*sin(theta),toString(Zone_Count[4,3]),col='darkorange',cex=1,
- offset=0,m=c(-3,0));
- }
- }
- # Zone 4
- if (Zone_Count[2,4] > 0){
- x_mean <- mean(x_S_Ball[S_Ball_4,j]);
- z_mean <- mean(z_S_Ball[S_Ball_4,j]);
- theta <- mean_angle(x_Dir_S_Ball[S_Ball_4,j],z_Dir_S_Ball[S_Ball_4,j],Zone_Count[2,4]);
- if (x_mean >= -3 && x_mean <= 3 && z_mean >= 0 && z_mean <= 6){
- arrow.plot(x_mean,z_mean,cos(theta),sin(theta),arrow.ex=.05,length=.1,col='blue',lwd=2,xlim=c(-3,3),
- ylim=c(0,6),xaxs='i',yaxs='i');
- par(new=TRUE);
- textxy(x_mean-no_s*cos(theta),z_mean-no_s*sin(theta),toString(Zone_Count[2,4]),col='blue',cex=1,
- offset=0,m=c(-3,0));
- }
- }
- if (Zone_Count[4,4] > 0){
- x_mean <- mean(x_T_Ball[T_Ball_4,j]);
- z_mean <- mean(z_T_Ball[T_Ball_4,j]);
- theta <- mean_angle(x_Dir_T_Ball[T_Ball_4,j],z_Dir_T_Ball[T_Ball_4,j],Zone_Count[4,4]);
- if (x_mean >= -3 && x_mean <= 3 && z_mean >= 0 && z_mean <= 6){
- arrow.plot(x_mean,z_mean,cos(theta),sin(theta),arrow.ex=.05,length=.1,col='darkorange',lwd=2,xlim=c(-3,3),
- ylim=c(0,6),xaxs='i',yaxs='i');
- par(new=TRUE);
- textxy(x_mean-no_s*cos(theta),z_mean-no_s*sin(theta),toString(Zone_Count[4,4]),col='darkorange',cex=1,
- offset=0,m=c(-3,0));
- }
- }
- # Zone 5
- if (Zone_Count[1,5] > 0){
- x_mean <- mean(x_S_Str[S_Str_5,j]);
- z_mean <- mean(z_S_Str[S_Str_5,j]);
- theta <- mean_angle(x_Dir_S_Str[S_Str_5,j],z_Dir_S_Str[S_Str_5,j],Zone_Count[1,5]);
- if (x_mean >= -3 && x_mean <= 3 && z_mean >= 0 && z_mean <= 6){
- arrow.plot(x_mean,z_mean,cos(theta),sin(theta),arrow.ex=.05,length=.1,col='green',lwd=2,xlim=c(-3,3),
- ylim=c(0,6),xaxs='i',yaxs='i');
- par(new=TRUE);
- textxy(x_mean-no_s*cos(theta),z_mean-no_s*sin(theta),toString(Zone_Count[1,5]),col='green',cex=1,
- offset=0,m=c(-3,0));
- }
- }
- if (Zone_Count[3,5] > 0){
- x_mean <- mean(x_T_Str[T_Str_5,j]);
- z_mean <- mean(z_T_Str[T_Str_5,j]);
- theta <- mean_angle(x_Dir_T_Str[T_Str_5,j],z_Dir_T_Str[T_Str_5,j],Zone_Count[3,5]);
- if (x_mean >= -3 && x_mean <= 3 && z_mean >= 0 && z_mean <= 6){
- arrow.plot(x_mean,z_mean,cos(theta),sin(theta),arrow.ex=.05,length=.1,col='red',lwd=2,xlim=c(-3,3),
- ylim=c(0,6),xaxs='i',yaxs='i');
- par(new=TRUE);
- textxy(x_mean-no_s*cos(theta),z_mean-no_s*sin(theta),toString(Zone_Count[3,5]),col='red',cex=1,
- offset=0,m=c(-3,0));
- }
- }
- # Zone 6
- if (Zone_Count[2,6] > 0){
- x_mean <- mean(x_S_Ball[S_Ball_6,j]);
- z_mean <- mean(z_S_Ball[S_Ball_6,j]);
- theta <- mean_angle(x_Dir_S_Ball[S_Ball_6,j],z_Dir_S_Ball[S_Ball_6,j],Zone_Count[2,6]);
- if (x_mean >= -3 && x_mean <= 3 && z_mean >= 0 && z_mean <= 6){
- arrow.plot(x_mean,z_mean,cos(theta),sin(theta),arrow.ex=.05,length=.1,col='blue',lwd=2,xlim=c(-3,3),
- ylim=c(0,6),xaxs='i',yaxs='i');
- par(new=TRUE);
- textxy(x_mean-no_s*cos(theta),z_mean-no_s*sin(theta),toString(Zone_Count[2,6]),col='blue',cex=1,
- offset=0,m=c(-3,0));
- }
- }
- if (Zone_Count[4,6] > 0){
- x_mean <- mean(x_T_Ball[T_Ball_6,j]);
- z_mean <- mean(z_T_Ball[T_Ball_6,j]);
- theta <- mean_angle(x_Dir_T_Ball[T_Ball_6,j],z_Dir_T_Ball[T_Ball_6,j],Zone_Count[4,6]);
- if (x_mean >= -3 && x_mean <= 3 && z_mean >= 0 && z_mean <= 6){
- arrow.plot(x_mean,z_mean,cos(theta),sin(theta),arrow.ex=.05,length=.1,col='darkorange',lwd=2,xlim=c(-3,3),
- ylim=c(0,6),xaxs='i',yaxs='i');
- par(new=TRUE);
- textxy(x_mean-no_s*cos(theta),z_mean-no_s*sin(theta),toString(Zone_Count[4,6]),col='darkorange',cex=1,
- offset=0,m=c(-3,0));
- }
- }
- # Zone 7
- if (Zone_Count[2,7] > 0){
- x_mean <- mean(x_S_Ball[S_Ball_7,j]);
- z_mean <- mean(z_S_Ball[S_Ball_7,j]);
- theta <- mean_angle(x_Dir_S_Ball[S_Ball_7,j],z_Dir_S_Ball[S_Ball_7,j],Zone_Count[2,7]);
- if (x_mean >= -3 && x_mean <= 3 && z_mean >= 0 && z_mean <= 6){
- arrow.plot(x_mean,z_mean,cos(theta),sin(theta),arrow.ex=.05,length=.1,col='blue',lwd=2,xlim=c(-3,3),
- ylim=c(0,6),xaxs='i',yaxs='i');
- par(new=TRUE);
- textxy(x_mean-no_s*cos(theta),z_mean-no_s*sin(theta),toString(Zone_Count[2,7]),col='blue',cex=1,
- offset=0,m=c(-3,0));
- }
- }
- if (Zone_Count[4,7] > 0){
- x_mean <- mean(x_T_Ball[T_Ball_7,j]);
- z_mean <- mean(z_T_Ball[T_Ball_7,j]);
- theta <- mean_angle(x_Dir_T_Ball[T_Ball_7,j],z_Dir_T_Ball[T_Ball_7,j],Zone_Count[4,7]);
- if (x_mean >= -3 && x_mean <= 3 && z_mean >= 0 && z_mean <= 6){
- arrow.plot(x_mean,z_mean,cos(theta),sin(theta),arrow.ex=.05,length=.1,col='darkorange',lwd=2,xlim=c(-3,3),
- ylim=c(0,6),xaxs='i',yaxs='i');
- par(new=TRUE);
- textxy(x_mean-no_s*cos(theta),z_mean-no_s*sin(theta),toString(Zone_Count[4,7]),col='darkorange',cex=1,
- offset=0,m=c(-3,0));
- }
- }
- # Zone 8
- if (Zone_Count[2,8] > 0){
- x_mean <- mean(x_S_Ball[S_Ball_8,j]);
- z_mean <- mean(z_S_Ball[S_Ball_8,j]);
- theta <- mean_angle(x_Dir_S_Ball[S_Ball_8,j],z_Dir_S_Ball[S_Ball_8,j],Zone_Count[2,8]);
- if (x_mean >= -3 && x_mean <= 3 && z_mean >= 0 && z_mean <= 6){
- arrow.plot(x_mean,z_mean,cos(theta),sin(theta),arrow.ex=.05,length=.1,col='blue',lwd=2,xlim=c(-3,3),
- ylim=c(0,6),xaxs='i',yaxs='i');
- par(new=TRUE);
- textxy(x_mean-no_s*cos(theta),z_mean-no_s*sin(theta),toString(Zone_Count[2,8]),col='blue',cex=1,
- offset=0,m=c(-3,0));
- }
- }
- if (Zone_Count[4,8] > 0){
- x_mean <- mean(x_T_Ball[T_Ball_8,j]);
- z_mean <- mean(z_T_Ball[T_Ball_8,j]);
- theta <- mean_angle(x_Dir_T_Ball[T_Ball_8,j],z_Dir_T_Ball[T_Ball_8,j],Zone_Count[4,8]);
- if (x_mean >= -3 && x_mean <= 3 && z_mean >= 0 && z_mean <= 6){
- arrow.plot(x_mean,z_mean,cos(theta),sin(theta),arrow.ex=.05,length=.1,col='darkorange',lwd=2,xlim=c(-3,3),
- ylim=c(0,6),xaxs='i',yaxs='i');
- par(new=TRUE);
- textxy(x_mean-no_s*cos(theta),z_mean-no_s*sin(theta),toString(Zone_Count[4,8]),col='darkorange',cex=1,
- offset=0,m=c(-3,0));
- }
- }
- # Zone 9
- if (Zone_Count[2,9] > 0){
- x_mean <- mean(x_S_Ball[S_Ball_9,j]);
- z_mean <- mean(z_S_Ball[S_Ball_9,j]);
- theta <- mean_angle(x_Dir_S_Ball[S_Ball_9,j],z_Dir_S_Ball[S_Ball_9,j],Zone_Count[2,9]);
- if (x_mean >= -3 && x_mean <= 3 && z_mean >= 0 && z_mean <= 6){
- arrow.plot(x_mean,z_mean,cos(theta),sin(theta),arrow.ex=.05,length=.1,col='blue',lwd=2,xlim=c(-3,3),
- ylim=c(0,6),xaxs='i',yaxs='i');
- par(new=TRUE);
- textxy(x_mean-no_s*cos(theta),z_mean-no_s*sin(theta),toString(Zone_Count[2,9]),col='blue',cex=1,
- offset=0,m=c(-3,0));
- }
- }
- if (Zone_Count[4,9] > 0){
- x_mean <- mean(x_T_Ball[T_Ball_9,j]);
- z_mean <- mean(z_T_Ball[T_Ball_9,j]);
- theta <- mean_angle(x_Dir_T_Ball[T_Ball_9,j],z_Dir_T_Ball[T_Ball_9,j],Zone_Count[4,9]);
- if (x_mean >= -3 && x_mean <= 3 && z_mean >= 0 && z_mean <= 6){
- arrow.plot(x_mean,z_mean,cos(theta),sin(theta),arrow.ex=.05,length=.1,col='darkorange',lwd=2,xlim=c(-3,3),
- ylim=c(0,6),xaxs='i',yaxs='i');
- par(new=TRUE);
- textxy(x_mean-no_s*cos(theta),z_mean-no_s*sin(theta),toString(Zone_Count[4,9]),col='darkorange',cex=1,
- offset=0,m=c(-3,0));
- }
- }
- par(new=TRUE);
- 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);
- par(new=FALSE);
- dev.off();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement