I have a 3D ellipsoid function:
ellipsoid <- function(center=c(0, 0, 0), radius=1, shape=diag(3),
segments=51) {
angles <- (0:segments)*2*pi/segments
ecoord2 <- function(p) {
c(cos(p[1])*sin(p[2]), sin(p[1])*sin(p[2]), cos(p[2])) }
unit.sphere <- t(apply(expand.grid(angles, angles), 1, ecoord2))
t(center + radius * t(unit.sphere %*% chol(shape)))
}
that makes an ellipsoid with a given center and radius. Then I can draw it using:
q <- quads3d(ellips[,1], ellips[,2], ellips[,3], front="lines",
back="lines", alpha=.5,
lit=FALSE, col=surface.col[1])
But, how can I determine if a point (x,y,z) falls inside this ellipsoid? Specifically, how do I figure out the semiaxes of the ellipsoid?
for instance,
fitsInEllipsoid <- function(ellipsoid, x, y, z) {
#returns true if (x,y,z) fits inside the ellipsoid
}
A point
(x,y,z)is inside ifwhere
aandbare the equatorial radii (along the x and y axes) andcis the polar radius (along the z-axis), i.e. the square root of the diagonal of theshapeparameter.The center of the ellipsoid is denoted by
(x0, y0, z0)(variablecenterin your function).