user2698600
user2698600

Reputation: 37

Set a perpendicular plane in r

I have this:

    install.packages("mnormt")
    library(mnormt)


    ma = c(1,5)
    Sa = matrix(c(2,0,0,0.5),2,2)
    A = rmnorm(100, ma, Sa)


    mb = c(5,7)
    Sb = matrix(c(1,0.5,0.5,0.5),2,2)
    B = rmnorm(200, mb, Sb)


    plot(rbind(A,B), type = "n")
    points(A, col = "red")
    points(B, col = "blue")


    points(ma[1], ma[2], pch = 20)
    points(mb[1], mb[2], pch = 20)


    lines(ma, mb)
    centroreta = c((ma[1] + mb[1])/2,(ma[2] + mb[2])/2)
    points(centroreta[1], centroreta[2], pch = 20, col = "green")


    cangular = (mb[2]-ma[2]) / (mb[1]-ma[1])
    cangular = -1/cangular

    clinear = centroreta[2]-cangular*centroreta[1]
    abline(clinear,cangular, col = "green")

This creates a line that separates the two clouds of points in 2D.

I wanted to do the same, but in 3D. but I can not generate the plane perpendicular to the line

    cloud1 <- rmnorm(100,mean=c(1,1,1),varcov=diag(.25,3)) 
    cloud2 <- rmnorm(75, mean=c(3,3,3),varcov=diag(.25,3))

    install.packages("rgl")
    library(rgl)
    plot3d(cloud1,box=F)
    points3d(cloud2,col="red")

    a <- matrix(c(1,1,1),1,3)
    b <- matrix(c(3,3,3),1,3)
    c <- (a+b)/2

    points3d(a,col="blue")
    points3d(b,col="blue")
    points3d(c,col="blue")

    segments3d(rbind(a,b))

I got this far. Now I can not generate the plane.

Thanks

Marcel

Upvotes: 1

Views: 215

Answers (1)

Vincent Zoonekynd
Vincent Zoonekynd

Reputation: 32351

The normal to the plane is a-b and we want c to be on it.

normal <- a-b
offset <- - sum( normal * c )
planes3d( normal[1], normal[2], normal[3], offset, alpha = .2 )

Plane

Upvotes: 2

Related Questions