问题描述
我有一个点列表和一个单纯形列表。我想在给定顶点的情况下以 3D 形式绘制单形。本质上,我正在寻找 3D 中的 segment() 等价物。
示例
Pts<-matrix(c(0,1,1),ncol =3,byrow=TRUE)
Simplex<-c(1,2,3,4)
所以,我正在寻找一种输入 Pts 和 Simplex 并获得四面体图的方法。
我已经尝试过搜索,但到目前为止,唯一的可能性似乎是写出线性空间的函数并绘制它们。任何提示将不胜感激。
解决方法
使用 'rgl' 包:
library(rgl)
vertices <- rbind(
c(0,0),c(1,c(0,1,1)
)
faces <- combn(4,3)
for(f in 1:4){
triangles3d(rbind(
vertices[faces[1,f],],vertices[faces[2,vertices[faces[3,]
),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,]),radius = 0.02,sides = 30),col="yellow")
}
# add vertices as small spheres
spheres3d(vertices,radius= 0.03,color = "yellow")
,
不那么漂亮或灵活,但这里有一个使用 persp
和 segments
的基本 R 版本:
## empty perspective plot
tm <- persp(matrix(rep(0,4),nrow=2),xlim=c(-1,1),ylim=c(-1,zlim=c(-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"))