在r中设置垂直平面

时间:2013-10-04 06:00:20

标签: r 3d plot

我有这个:

    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")

这会创建一条线,用于分隔2D中的两个点云。

我想做同样的事,但是在3D中。 但是我无法生成垂直于线的平面

    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))

我到目前为止。现在我无法生成飞机。

由于

马塞尔

1 个答案:

答案 0 :(得分:1)

飞机的法线是a-b 我们想要c

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

Plane