knitr::opts_chunk$set(echo = TRUE)


library( package = "ggplot2" )
library( package = "maptools" )
## Loading required package: sp
## Checking rgeos availability: FALSE
##      Note: when rgeos is not available, polygon geometry     computations in maptools depend on gpclib,
##      which has a restricted licence. It is disabled by default;
##      to enable gpclib, type gpclibPermit()
  wd <- getwd()
 # setwd("desktop/all2018.csv")

From retrosheet: Projection for where ball was hit

figure1

figure1

# Shape
library(ggplot2)
df <- data.frame(x = c(1, 0, -1, 0), y = c(0, 1, 0, -1))
ggplot(df) + geom_polygon(aes(x = x, y = y))

#Found GeomMLBStadiums package

#devtools::install_github("bdilday/GeomMLBStadiums")
#library(GeomMLBStadiums)
#library(ggplot2)
#library(dplyr)


#ggplot() + 
 # geom_mlb_stadium(stadium_segments = "all") + 
 # facet_wrap(~team) + 
 # coord_fixed() + 
 # theme_void()
# Stadium specs
x_hp   = 125       # X coordinate of the Homeplate.
y_hp   = 43        # Y coordinate of the Homeplate.
f_len  = 150       # Length of the foul line.
b_len  = 45        # Length between bases.
y_max  = 250       # Max y value.
f_angl = sqrt(2)/2 # Angle for foul lines and bases

# Starting and ending coordinates of foul lines.
d_bounds <- data.frame( x    = rep( x = x_hp, 2 )
                      , y    = rep( x = y_hp, 2 )
                      , xend = c( x_hp + f_angl * f_len, x_hp - f_angl * f_len )
                      , yend = c( y_hp + f_angl * f_len, y_hp + f_angl * f_len )
                      )

# Starting and ending coordinates of foul lines.
d_bases <- data.frame( x    = c( x_hp, x_hp + f_angl * b_len
                               , x_hp, x_hp - f_angl * b_len
                               , x_hp 
                               )
                     , y    = c( y_hp, y_hp + f_angl * b_len
                               , y_hp + sqrt( 2 * b_len^2)
                               , y_hp + f_angl * b_len
                               , y_hp 
                               ) )

d_bases
##           x         y
## 1 125.00000  43.00000
## 2 156.81981  74.81981
## 3 125.00000 106.63961
## 4  93.18019  74.81981
## 5 125.00000  43.00000
## Need to use rshiny for animation

Use in football project/ pitch viz

require(ggplot2)
grass_colour <- "#775D6A"
line_colour <- "#F4828C"
background_colour <- "#775D6A"
goal_colour <- "#7E3C5D"

theme_blankPitch = function(size=12) { 
  theme(
    #axis.line=element_blank(), 
    axis.text.x=element_blank(), 
    axis.text.y=element_blank(), 
    #axis.ticks.y=element_text(size=size),
    #   axis.ticks=element_blank(),
    axis.ticks.length=unit(0, "lines"), 
    #axis.ticks.margin=unit(0, "lines"), 
    axis.title.x=element_blank(), 
    axis.title.y=element_blank(), 
    legend.background=element_rect(fill=background_colour, colour=NA), 
    legend.key=element_rect(colour=background_colour,fill=background_colour), 
    legend.key.size=unit(1.2, "lines"), 
    legend.text=element_text(size=size), 
    legend.title=element_text(size=size, face="bold",hjust=0),
    strip.background = element_rect(colour = background_colour, fill = background_colour, size = .5),
    panel.background=element_rect(fill=background_colour,colour=background_colour), 
    #       panel.border=element_blank(), 
    panel.grid.major=element_blank(), 
    panel.grid.minor=element_blank(), 
    panel.spacing=element_blank(), 
    plot.background=element_blank(), 
    plot.margin=unit(c(0, 0, 0, 0), "lines"), 
    plot.title=element_text(size=size*1.2), 
    strip.text.y=element_text(colour=background_colour,size=size,angle=270),
    strip.text.x=element_text(size=size*1))}


ymin <- 0 
ymax <- 7040
xmin <- 0 
xmax <- 10600

GoalWidth <- 732
penspot <- 1100
boxedgeW <- 4032
boxedgeL <- 1650
box6yardW <- 1832
box6yardL <- 550

## dimensions calculations 
# The 18 Yard Box
TheBoxWidth <- c(((ymax / 2) + (boxedgeW / 2)),((ymax / 2) - (boxedgeW / 2)))
TheBoxHeight <- c(boxedgeL,xmax-boxedgeL)
GoalPosts <- c(((ymax / 2) + (GoalWidth / 2)),((ymax / 2) - (GoalWidth / 2)))
  
# The 6 Yard Box
box6yardWidth <- c(((ymax / 2) + (box6yardW / 2)),((ymax / 2) - (box6yardW / 2)))
box6yardHeight <- c(box6yardL,xmax-box6yardL)


## Centre circle dimensions 
centreCirle_d <- 1830

## define the circle function
circleFun <- function(center = c(0,0),diameter = 1, npoints = 100){
    r = diameter / 2
    tt <- seq(0,2*pi,length.out = npoints)
    xx <- center[1] + r * cos(tt)
    yy <- center[2] + r * sin(tt)
    return(data.frame(x = xx, y = yy))
}

#### create leftD arc ####
Dleft <- circleFun(c((penspot),(ymax/2)),centreCirle_d,npoints = 1000)
## remove part that is in the box
Dleft <- Dleft[which(Dleft$x >= (boxedgeL)),]

## create rightD arc  ####
Dright <- circleFun(c((xmax-(penspot)),(ymax/2)),centreCirle_d,npoints = 1000)
## remove part that is in the box
Dright <- Dright[which(Dright$x <= (xmax-(boxedgeL))),]

#### create center circle ####
center_circle <- circleFun(c((xmax/2),(ymax/2)),centreCirle_d,npoints = 100)

## create corner flag radius ####
TopLeftCorner <- circleFun(c(xmin,ymax),200,npoints = 1000)
TopRightCorner <- circleFun(c(xmax,ymax),200,npoints = 1000)
BottomLeftCorner <- circleFun(c(xmin,ymin),200,npoints = 1000)
BottomRightCorner <- circleFun(c(xmax,ymin),200,npoints = 1000)

## initiate the plot and set its boundaries 
ggplot() + xlim(c(-10,xmax+10)) + ylim(c(-10,ymax+10))

## initiate the plot and set its boundaries 
ggplot() + xlim(c(-10,xmax+10)) + ylim(c(-10,ymax+10))+
# add the theme 
theme_blankPitch() 

## initiate the plot and set its boundaries 
ggplot() + xlim(c(-10,xmax+10)) + ylim(c(-10,ymax+10))+
# add the theme 
theme_blankPitch() +
# add the base rectangle of the pitch 
geom_rect(aes(xmin=0, xmax=xmax, ymin=0, ymax=ymax), fill = grass_colour, colour = line_colour)

# defining
grass_colour <- "#775D6A"
line_colour <- "#F4828C"
background_colour <- "#775D6A"
goal_colour <- "#7E3C5D"
ymax <- 7040
xmax <- 10600

#createPitch(xmax, ymax, grass_colour, line_colour, background_colour, goal_colour)
## green 
#createPitch(10600, 7040, "#8dde02", "#ffffff", "#8dde02", "#000000")