library(plotly)
x1_val <- seq(1, 7, length.out = 20)
x2_val <- seq(-5, 1, length.out = 20)
g <- expand.grid(x1 = x1_val, x2 = x2_val)

z1 <- matrix(-g$x1 - g$x2, nrow = 20)
z3 <- matrix(2 - g$x1, nrow = 20)




plot_ly() %>%
  add_surface(x = ~x1_val, y = ~x2_val, z = ~z1, name = "Ebene (I)", opacity = 0.6, colorscale = "Blues", showscale=FALSE) %>%
  add_surface(x = ~x1_val, y = ~x2_val, z = ~z3, name = "Ebene (III)", opacity = 0.6, colorscale = "Greens", showscale=FALSE) %>%
  add_trace(x = 4, y = -2, z = -2, type = "scatter3d", mode = "markers",
            marker = list(size = 8, color = "red"), name = "Schnittpunkt (4, -2, -2)") %>%
  layout(scene = list(xaxis = list(title = 'x1'), yaxis = list(title = 'x2'), zaxis = list(title = 'x3')))
x1_b <- seq(-2, 4, length.out = 20)
x2_b <- seq(-2, 4, length.out = 20)
g_b <- expand.grid(x1 = x1_b, x2 = x2_b)

z1_b <- matrix((12 - 4*g_b$x1 - g_b$x2)/7, nrow = 20)
z2_b <- matrix(0.5 - 0.5*g_b$x1, nrow = 20)

plot_ly() %>%
  add_surface(x = ~x1_b, y = ~x2_b, z = ~z1_b, name = "Ebene (I)", opacity = 0.6, showscale=FALSE) %>%
  add_surface(x = ~x1_b, y = ~x2_b, z = ~z2_b, name = "Ebene (II)", opacity = 0.6, showscale=FALSE) %>%
  layout(scene = list(xaxis = list(title = 'x1'), yaxis = list(title = 'x2'), zaxis = list(title = 'x3')))
t_val <- seq(-3, 3, length.out = 30)
x1_c <- t_val
x2_c <- -t_val - 1
x3_c <- -3 - 2*t_val

plot_ly() %>%
  add_trace(x = ~x1_c, y = ~x2_c, z = ~x3_c, type = "scatter3d", mode = "lines",
            line = list(width = 6, color = "red"), name = "Schnittgerade") %>%
  layout(scene = list(xaxis = list(title = 'x1'), yaxis = list(title = 'x2'), zaxis = list(title = 'x3')))