Advertisement
karstenw

xkcd 135 #2

Jan 26th, 2016
358
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
R 2.42 KB | None | 0 0
  1. solve_xkcd135b <- function(h=0.001) {
  2.     # start positions
  3.     start_rtop <- c(0, sqrt(20^2-10^2))
  4.     start_rright <- c(10, 0)
  5.     start_rleft <- c(-10,0)
  6.     start_me <- c(0, sqrt(3)/6*20)
  7.  
  8.     # survival time, given angle phi and stepsize h
  9.     pos_me <- function(t, phi) start_me + c(sin(phi)*6*t, cos(phi)*6*t)
  10.     velo_rtop <- function(t) pmin(4*t, 10) # wounded top raptor
  11.     velo_rapt <- function(t) pmin(4*t, 25) # left & right
  12.     dist_rapt <- function(from, to, velo) integrate(velo, from, to)$value
  13.     surv_time <- function(phi, h, pos0, velo) {
  14.         curr <- pos0
  15.         t <- 0
  16.         repeat {
  17.             t <- t + h
  18.             old <- curr
  19.             direction <- c(pos_me(t-h, phi)-old)
  20.             len <- dist_rapt(t-h, t, velo)
  21.             if(dist(rbind(pos_me(t, phi), old))<len) return(t) # gotcha
  22.             curr <- old + direction*len/sqrt(sum(direction^2))
  23.         }
  24.         return(curr)
  25.     }
  26.     surv3_time <- function(phi, h) min(c(
  27.         surv_time(phi, h, start_rtop, velo_rtop),
  28.         surv_time(phi, h, start_rleft, velo_rapt),
  29.         surv_time(phi, h, start_rright, velo_rapt)
  30.     ))
  31.            
  32.     # find the best angle
  33.     res <- optimize(surv3_time, c(0, 2*pi), h=h, maximum=TRUE)
  34.     res_angle <- res$maximum
  35.     res_time <- res$objective
  36.    
  37.     # plot
  38.     plot(1, col="white", xlim=c(-17, 11), ylim=c(-1, sqrt(20^2-10^2)+1), axes=FALSE, xlab="", ylab="")
  39.     me <- Vectorize(pos_me, "t")(seq(0, res_time, length.out=100), res_angle)
  40.     lines(me[1,], me[2,], col="red", lwd=2)
  41.    
  42.     pos_rapt <- function(t, phi, h, pos0, velo) {
  43.         pos <- matrix(0, nrow=100, ncol=2)
  44.         pos[1,] <- pos0
  45.         t_vec <- seq(0, t, length.out=100)
  46.         # browser(skipCalls=2)
  47.         for (i in 2:100) {
  48.             direction <- pos_me(t_vec[i-1], phi)-pos[i-1,]
  49.             len <- dist_rapt(t_vec[i-1], t_vec[i], velo)
  50.             pos[i,] <- pos[i-1,] + direction*len/sqrt(sum(direction^2))            
  51.         }
  52.         return(pos)
  53.     }
  54.     rtop <- pos_rapt(res_time*0.97, res_angle, h, start_rtop, velo_rtop)
  55.     rright <- pos_rapt(res_time*0.97, res_angle, h, start_rright, velo_rapt)
  56.     rleft <- pos_rapt(res_time*0.97, res_angle, h, start_rleft, velo_rapt)
  57.    
  58.     lines(rtop[,1], rtop[,2], lwd=2, col="blue")
  59.     lines(rright[,1], rright[,2], lwd=2, col="gray")
  60.     lines(rleft[,1], rleft[,2], lwd=2, col="black")
  61.  
  62.     # answer
  63.     return(res_angle)    
  64. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement