要在R語言中畫3D愛心圖,可以使用rgl
包來實現。下面是一個簡單的示例代碼:
# 安裝和加載rgl包
install.packages("rgl")
library(rgl)
# 創建3D畫布
open3d()
# 畫愛心圖
x <- seq(-2*pi, 2*pi, length.out = 100)
y <- seq(-2*pi, 2*pi, length.out = 100)
grid <- expand.grid(x = x, y = y)
z <- with(grid, sin(sqrt(x^2 + y^2)) / sqrt(x^2 + y^2))
z[is.na(z)] <- 1
z <- matrix(z, nrow = length(x), ncol = length(y), byrow = TRUE)
persp3d(x, y, z, col = "red", lit = FALSE, zlim = c(0, 1))
# 關閉3D畫布
rgl.close()
運行上述代碼后,會彈出一個新窗口顯示繪制的3D愛心圖。