[英]Plot simplices in 3D in R
我有一个点列表和一个单纯形列表。 我想 plot 3D 中的单纯形给定它们的顶点。 本质上,我正在寻找 3D 中的 segment() 等价物。
例子
Pts<-matrix(c(0,0,0,1,0,0,0,1,0,0,0,1),ncol =3,byrow=TRUE)
Simplex<-c(1,2,3,4)
因此,我正在寻找一种输入 Pts 和 Simplex 并获得四面体的 plot 的方法。
我试过搜索,但到目前为止,唯一的可能性似乎是写出线性空间和 plot 的函数。 任何提示将不胜感激。
使用“rgl” package:
library(rgl)
vertices <- rbind(
c(0, 0, 0),
c(1, 0, 0),
c(0, 1, 0),
c(0, 0, 1)
)
faces <- combn(4,3)
for(f in 1:4){
triangles3d(rbind(
vertices[faces[1,f],],
vertices[faces[2,f],],
vertices[faces[3,f],]
), color="red", alpha=0.4)
}
您可以添加边和顶点:
# add edges as thin cylinders
edges <- combn(4, 2)
for(e in 1:6){
shade3d(cylinder3d(rbind(vertices[edges[1,e],],vertices[edges[2,e],]),
radius = 0.02, sides = 30), col="yellow")
}
# add vertices as small spheres
spheres3d(vertices, radius= 0.03, color = "yellow")
不那么漂亮或灵活,但这是一个基本的 R 版本,使用persp
和segments
来娱乐:
## empty perspective plot
tm <- persp(matrix(rep(0,4), nrow=2),
xlim=c(-1,1), ylim=c(-1,1), zlim=c(-1,1),
col="#00000000", border=NA, theta=30, phi=50, xlab="x")
## project points into 3d space
tpts <- data.frame(trans3d(pmat=tm, x=Pts[,1], y=Pts[,2], z=Pts[,3]))
## draw each segment
sgs <- combn(seq_len(nrow(tpts)), 2,
FUN=function(r) unlist(tpts[r,]), simplify=FALSE)
lapply(sgs, function(x) segments(x[1], x[3], x[2], x[4], col="red"))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.