簡體   English   中英

Plot 在 R 中簡化為 3D

[英]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 版本,使用perspsegments來娛樂:

## 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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM