set.seed(1)

library(ggplot2)
## Warning: Paket 'ggplot2' wurde unter R Version 4.1.3 erstellt
library(rgl)
## Warning: Paket 'rgl' wurde unter R Version 4.1.3 erstellt
# Set up the data for the globe
theta <- seq(0, 2 * pi, length.out = 101)
phi <- seq(-pi/2, pi/2, length.out = 101)
globe <- expand.grid(theta = theta, phi = phi)
globe$x <- 15 * cos(globe$theta) * cos(globe$phi)
globe$y <- 15 * sin(globe$theta) * cos(globe$phi)
globe$z <- 15 * sin(globe$phi)

# Add random noise to the globe
noise_level_1 <- 0.06 # adjust this value to control the amount of noise
noise_level_2 <- 0.01 # adjust this value to control the amount of noise
noise_level_3 <- 1 # adjust this value to control the amount of noise

globe$x <- globe$x + rnorm(nrow(globe), mean = 0, sd = noise_level_1)
globe$y <- globe$y + rnorm(nrow(globe), mean = 0, sd = noise_level_2)
globe$z <- globe$z + rnorm(nrow(globe), mean = 0, sd = noise_level_3)

# Calculate the distance of each point from the center of the globe
distance <- sqrt(globe$x^2 + globe$y^2 + globe$z^2)

# Set up a color palette that goes from light to dark red
colors <- colorRampPalette(c("#FFCCCC", "#FF9999", "#FF0000"))

# Convert the input data to matrix format
xmat <- matrix(globe$x, nrow = length(unique(globe$phi)), ncol = length(unique(globe$theta)), byrow = TRUE)
ymat <- matrix(globe$y, nrow = length(unique(globe$phi)), ncol = length(unique(globe$theta)), byrow = TRUE)
zmat <- matrix(globe$z, nrow = length(unique(globe$phi)), ncol = length(unique(globe$theta)), byrow = TRUE)

# Plot the globe using rgl, with colors based on distance from the center
open3d()
## wgl 
##   1
bg3d(color = "black")
surface3d(x = xmat, y = ymat, z = zmat, color = colors(distance/5))
## Warning in seq.int(0, 1, length.out = n): das erste Element des 'length.out'
## Arguments benutzt
rglwidget()
set.seed(1)
library(rgl)

# Set up the data for the globe
theta <- seq(0, 2 * pi, length.out = 101)
phi <- seq(-pi/2, pi/2, length.out = 101)
globe <- expand.grid(theta = theta, phi = phi)
globe$x <- 15 * cos(globe$theta) * cos(globe$phi)
globe$y <- 15 * sin(globe$theta) * cos(globe$phi)
globe$z <- 15 * sin(globe$phi)

# Add noise to the globe
noise_level <- 0.1

globe$x <- globe$x + rnorm(nrow(globe), sd = noise_level)
globe$y <- globe$y + rnorm(nrow(globe), sd = noise_level)
globe$z <- globe$z + rnorm(nrow(globe), sd = noise_level)

# Convert the input data to matrix format
xmat <- matrix(globe$x, nrow = length(unique(globe$phi)), ncol = length(unique(globe$theta)), byrow = TRUE)
ymat <- matrix(globe$y, nrow = length(unique(globe$phi)), ncol = length(unique(globe$theta)), byrow = TRUE)
zmat <- matrix(globe$z, nrow = length(unique(globe$phi)), ncol = length(unique(globe$theta)), byrow = TRUE)

# Plot the globe using rgl
open3d()
## wgl 
##   2
bg3d(color = "black")
surface3d(x = xmat, y = ymat, z = zmat, color = "white")
rglwidget()
set.seed(1)
library(rgl)

# Set up the data for the globe
theta <- seq(0, 2 * pi, length.out = 101)
phi <- seq(-pi/2, pi/2, length.out = 101)
globe <- expand.grid(theta = theta, phi = phi)
globe$x <- 15 * cos(globe$theta) * cos(globe$phi)
globe$y <- 15 * sin(globe$theta) * cos(globe$phi)
globe$z <- 15 * sin(globe$phi)

# Add noise to the globe
noise_level <- 0.5

globe$x <- globe$x + rnorm(nrow(globe), sd = noise_level)
globe$y <- globe$y + rnorm(nrow(globe), sd = noise_level)
globe$z <- globe$z + rnorm(nrow(globe), sd = noise_level)

# Convert the input data to matrix format
xmat <- matrix(globe$x, nrow = length(unique(globe$phi)), ncol = length(unique(globe$theta)), byrow = TRUE)
ymat <- matrix(globe$y, nrow = length(unique(globe$phi)), ncol = length(unique(globe$theta)), byrow = TRUE)
zmat <- matrix(globe$z, nrow = length(unique(globe$phi)), ncol = length(unique(globe$theta)), byrow = TRUE)

#Colours
cols <- terrain.colors(2000)

# Plot the globe using rgl
open3d()
## wgl 
##   3
bg3d(color = "black")
surface3d(x = xmat, y = ymat, z = zmat, color = cols)
rglwidget()