Skip to content

Instantly share code, notes, and snippets.

@RyanHope
Created October 22, 2014 00:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RyanHope/3f5c66f93844f5167970 to your computer and use it in GitHub Desktop.
Save RyanHope/3f5c66f93844f5167970 to your computer and use it in GitHub Desktop.
require(ggplot2)
require(data.table)
require(parallel)
load_all("~/gazetools")
aTan <- function(x1,y1,x2,y2) {
y = (y2-y1)
x = (x2-x1)
a <- abs(atan(y/x)) * (180/pi)
if (y>=0 && x<0) {
a <- 180 - a
} else if (y<0) {
if (x<=0)
a <- 180 + a
else
a <- 360 - a
}
a
}
#' 21-in. SVGA color monitor (42.67x32.01cm)
#' NOTE: SVGA==800x600
#' New requirements:
#' min_dist=4.31
#' max_dist=8.69
#' End 11saccades or no suitable new
#' view_dist=87cm
bootSim <- function(N=1, a=1.0) {
out <- data.table()
for (i in 1:N) {
saccades=0
x = runif(1,0,800)
y = runif(1,0,600)
repeat {
repeat {
x1 = runif(1,0,800)
y1 = runif(1,0,600)
d = subtended_angle(x,y,x1,y1,800,600,42.67,32.01,87)
if (d>4.31 && d<8.69) break
}
x = c(x, x1)
y = c(y, y1)
saccades = saccades + 1
if (saccades==3) break
}
repeat {
repeat {
lag = sample(1:3,1)
xo = x[4-lag]
yo = y[4-lag]
if (xo!=x[4] && yo!=y[4])
break
}
do = subtended_angle(x[4],y[4],xo,yo,800,600,42.67,32.01,87)
tries = 0
repeat {
tries = tries + 1
xn = runif(1,0,800)
yn = runif(1,0,600)
dn = subtended_angle(x[4],y[4],xn,yn,800,600,42.67,32.01,87)
dno = subtended_angle(xn,yn,xo,yo,800,600,42.67,32.01,87)
if (tries==1000 || (dn>4.31 && dn<8.69 && dno>4.31)) break
}
if (tries==1000) break
a = aTan(x[3],y[3],x[4],y[4])
an = aTan(x[4],y[4],xn,yn)
ao = aTan(x[4],y[4],xo,yo)
ans = "OLD"
x1 = xo
y1 = yo
if (abs(a-an)<abs(a-ao)) {
ans = "NEW"
x1 = xn
y1 = yn
}
out <- rbind(out, data.table(run=i,lag=lag,ans=ans))
x = c(x[-1], x1)
y = c(y[-1], y1)
saccades = saccades + 1
if (saccades==11) break
}
}
out
}
#z <- mclapply(mclapply(1:16,function(x)bootSim(10000),mc.cores=16))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment