Geometría analítica para el plano

library(ggplot2)
library(plotly)
## 
## Adjuntando el paquete: 'plotly'
## The following object is masked from 'package:ggplot2':
## 
##     last_plot
## The following object is masked from 'package:stats':
## 
##     filter
## The following object is masked from 'package:graphics':
## 
##     layout
library(dplyr)
## 
## Adjuntando el paquete: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
x_vals=seq(-10,10,length.out = 100)
y_vals=seq(-10,10,length.out = 100)
plane=expand.grid(x=x_vals,y=y_vals)
plane$z=5-plane$x-2*plane$y

t_vals=seq(-10,10,length.out = 100)
linea=data.frame(x=1+2*t_vals,y=3-t_vals,z=2+t_vals)
linea
##                x           y           z
## 1   -19.00000000 13.00000000 -8.00000000
## 2   -18.59595960 12.79797980 -7.79797980
## 3   -18.19191919 12.59595960 -7.59595960
## 4   -17.78787879 12.39393939 -7.39393939
## 5   -17.38383838 12.19191919 -7.19191919
## 6   -16.97979798 11.98989899 -6.98989899
## 7   -16.57575758 11.78787879 -6.78787879
## 8   -16.17171717 11.58585859 -6.58585859
## 9   -15.76767677 11.38383838 -6.38383838
## 10  -15.36363636 11.18181818 -6.18181818
## 11  -14.95959596 10.97979798 -5.97979798
## 12  -14.55555556 10.77777778 -5.77777778
## 13  -14.15151515 10.57575758 -5.57575758
## 14  -13.74747475 10.37373737 -5.37373737
## 15  -13.34343434 10.17171717 -5.17171717
## 16  -12.93939394  9.96969697 -4.96969697
## 17  -12.53535354  9.76767677 -4.76767677
## 18  -12.13131313  9.56565657 -4.56565657
## 19  -11.72727273  9.36363636 -4.36363636
## 20  -11.32323232  9.16161616 -4.16161616
## 21  -10.91919192  8.95959596 -3.95959596
## 22  -10.51515152  8.75757576 -3.75757576
## 23  -10.11111111  8.55555556 -3.55555556
## 24   -9.70707071  8.35353535 -3.35353535
## 25   -9.30303030  8.15151515 -3.15151515
## 26   -8.89898990  7.94949495 -2.94949495
## 27   -8.49494949  7.74747475 -2.74747475
## 28   -8.09090909  7.54545455 -2.54545455
## 29   -7.68686869  7.34343434 -2.34343434
## 30   -7.28282828  7.14141414 -2.14141414
## 31   -6.87878788  6.93939394 -1.93939394
## 32   -6.47474747  6.73737374 -1.73737374
## 33   -6.07070707  6.53535354 -1.53535354
## 34   -5.66666667  6.33333333 -1.33333333
## 35   -5.26262626  6.13131313 -1.13131313
## 36   -4.85858586  5.92929293 -0.92929293
## 37   -4.45454545  5.72727273 -0.72727273
## 38   -4.05050505  5.52525253 -0.52525253
## 39   -3.64646465  5.32323232 -0.32323232
## 40   -3.24242424  5.12121212 -0.12121212
## 41   -2.83838384  4.91919192  0.08080808
## 42   -2.43434343  4.71717172  0.28282828
## 43   -2.03030303  4.51515152  0.48484848
## 44   -1.62626263  4.31313131  0.68686869
## 45   -1.22222222  4.11111111  0.88888889
## 46   -0.81818182  3.90909091  1.09090909
## 47   -0.41414141  3.70707071  1.29292929
## 48   -0.01010101  3.50505051  1.49494949
## 49    0.39393939  3.30303030  1.69696970
## 50    0.79797980  3.10101010  1.89898990
## 51    1.20202020  2.89898990  2.10101010
## 52    1.60606061  2.69696970  2.30303030
## 53    2.01010101  2.49494949  2.50505051
## 54    2.41414141  2.29292929  2.70707071
## 55    2.81818182  2.09090909  2.90909091
## 56    3.22222222  1.88888889  3.11111111
## 57    3.62626263  1.68686869  3.31313131
## 58    4.03030303  1.48484848  3.51515152
## 59    4.43434343  1.28282828  3.71717172
## 60    4.83838384  1.08080808  3.91919192
## 61    5.24242424  0.87878788  4.12121212
## 62    5.64646465  0.67676768  4.32323232
## 63    6.05050505  0.47474747  4.52525253
## 64    6.45454545  0.27272727  4.72727273
## 65    6.85858586  0.07070707  4.92929293
## 66    7.26262626 -0.13131313  5.13131313
## 67    7.66666667 -0.33333333  5.33333333
## 68    8.07070707 -0.53535354  5.53535354
## 69    8.47474747 -0.73737374  5.73737374
## 70    8.87878788 -0.93939394  5.93939394
## 71    9.28282828 -1.14141414  6.14141414
## 72    9.68686869 -1.34343434  6.34343434
## 73   10.09090909 -1.54545455  6.54545455
## 74   10.49494949 -1.74747475  6.74747475
## 75   10.89898990 -1.94949495  6.94949495
## 76   11.30303030 -2.15151515  7.15151515
## 77   11.70707071 -2.35353535  7.35353535
## 78   12.11111111 -2.55555556  7.55555556
## 79   12.51515152 -2.75757576  7.75757576
## 80   12.91919192 -2.95959596  7.95959596
## 81   13.32323232 -3.16161616  8.16161616
## 82   13.72727273 -3.36363636  8.36363636
## 83   14.13131313 -3.56565657  8.56565657
## 84   14.53535354 -3.76767677  8.76767677
## 85   14.93939394 -3.96969697  8.96969697
## 86   15.34343434 -4.17171717  9.17171717
## 87   15.74747475 -4.37373737  9.37373737
## 88   16.15151515 -4.57575758  9.57575758
## 89   16.55555556 -4.77777778  9.77777778
## 90   16.95959596 -4.97979798  9.97979798
## 91   17.36363636 -5.18181818 10.18181818
## 92   17.76767677 -5.38383838 10.38383838
## 93   18.17171717 -5.58585859 10.58585859
## 94   18.57575758 -5.78787879 10.78787879
## 95   18.97979798 -5.98989899 10.98989899
## 96   19.38383838 -6.19191919 11.19191919
## 97   19.78787879 -6.39393939 11.39393939
## 98   20.19191919 -6.59595960 11.59595960
## 99   20.59595960 -6.79797980 11.79797980
## 100  21.00000000 -7.00000000 12.00000000
p = ggplot() +
  geom_tile(data = plane, aes(x = x, y = y, fill = z), alpha = 0.3) +
  geom_line(data = linea, aes(x = x, y = y, color = z)) +
  labs(title = "Plano y Recta en el Espacio",
       x = "X",
       y = "Y",
       fill = "Z (Plano)",
       color = "Z (Recta)") +
  theme_minimal()
p

p1 = plot_ly() %>%
  add_surface(
    x = matrix(plane$x,nrow=100),
    y = matrix(plane$y,nrow=100),
    z = matrix(plane$z,nrow=100),
    colorscale="Viridis",
    showscale=FALSE,
    name="Plano",
    contours = list(
      x = list(show = TRUE, color = "black"),
      y = list(show = TRUE, color = "black"),
      z = list(show = TRUE, color = "black"))) %>%
  add_trace(
    x = linea$x, y = linea$y, z = linea$z,
    mode = 'lines', type = 'scatter3d', line = list(color = 'red', width = 4), name= "Linea recta"
  ) %>%  add_trace(
    x = c(-7), y = c(7), z = c(-2),
    mode = 'markers', type = 'scatter3d',
    marker = list(color = 'yellow', size = 5),
    name = "Intersección"
  ) %>%
  layout(
    title = "Plano y Recta en el Espacio",
    scene = list(
      xaxis = list(title = "X",range=c(-10,10),dtick=1),
      yaxis = list(title = "Y",range=c(-10,10),dtick=1),
      zaxis = list(title = "Z",range=c(-10,10),dtick=1)
    )
  )

p1