Skip to content

Instantly share code, notes, and snippets.

@ayota
Created February 1, 2015 20:02
Show Gist options
  • Save ayota/20bc62fb6eb8abc19162 to your computer and use it in GitHub Desktop.
Save ayota/20bc62fb6eb8abc19162 to your computer and use it in GitHub Desktop.
#using distance formula
planesDist <- function(x1, x2, x3, y1, y2, y3) {
dist <- function(s1, s2, s3, s4) {
d <- sqrt(
t(x1 + s1*x2 + s2*x3 - (y1 + s3*y2 + s4*y3)) %*%
(x1 + s1*x2 + s2*x3 - (y1 + s3*y2 + s4*y3)))
return(d)}
start <- cbind(seq(-100,100,.01),seq(-100,100,.01),seq(-100,100,.01),seq(-100,100,.01))
m<-min(mapply(function(s1,s2,s3,s4) nlm(dist,s1,s2,s3,s4)$minimum,start[,1],start[,2],start[,3],start[,4]))
return(m)}
x.1 <- c(-2,1,0,1)
x.2 <- c(1,-1,1,0)
x.3 <- c(0,0,1,0)
y.1 <- c(0,1,0,0)
y.2 <- c(1,0,2,1)
y.3 <- c(0,0,0,0)
planesDist(x.1,x.2,x.3,y.1,y.2,y.3)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment