简体   繁体   English

如何在R中的3D图上添加椭圆平面

[英]How to add ellipse plane to 3D plot in R

R code about plot plane z = x+y: 关于绘图平面z的R代码= x + y:

library("rgl")
f <- function(x,y) 
x+y

x_grid <- seq(0,1, length=20)
y_grid <- x_grid 
z_grid <- outer(x_grid, y_grid, f)
surface3d(x_grid, y_grid, z_grid,front="line",back="line")
axes3d() 

R code about plot ellipse 5x^2-4xy+5y^2=1: 关于椭圆椭圆的R代码5x ^ 2-4xy + 5y ^ 2 = 1:

my.fun <- function(x,y){5*x^2 - 4*x*y + 5*y^2}
x<-seq(-sqrt(5/21), sqrt(5/21), length.out = 101)
y<-seq(-sqrt(5/21), sqrt(5/21), length.out = 101)
z<-outer(x,y,my.fun)
contour(x, y, z, levels = 1, drawlabels = 0, xlab = "x", ylab = "y")

How to add ellipse plane to 3D plot in R? 如何在R中的3D图上添加椭圆平面? Thanks! 谢谢!

Still not sure what you are trying to do, but this plots the intersection of your plane 仍然不确定您要做什么,但这会绘制飞机的交点

z = x + y z = x + y

and your parabolic ellipsoid 和你的抛物线椭球

z = 5x 2 -4xy + 5y 2 z = 5x 2 -4xy + 5y 2

and colors the resulting ellipse as in your linked image. 并像链接的图像一样为所得的椭圆着色。

library(rgl)
f      <- function(x,y) x+y
my.fun <- function(x,y) 5*x^2 - 4*x*y + 5*y^2

x_grid <- seq(0,1, length=20)
z_grid <- outer(x_grid, x_grid, f)

theta <- seq(0,2*pi,length=1000)
r     <- sqrt(5/21)
df    <- expand.grid(x=r*cos(theta),y=r*cos(theta))
df$z  <- with(df,f(x,y) - my.fun(x,y))
xyz   <- df[df$z>=0,]
surface3d(x_grid, x_grid, z_grid, front="line",back="line")
axes3d() 
zlim    <- range(-1000*f(xyz$x,xyz$y))
zlen    <- diff(zlim) + 1
palette <- heat.colors(zlen)                       
col     <- palette[-1000*f(xyz$x,xyz$y)-zlim[1]+1 ]
with(xyz,points3d(x,y,f(x,y),color=col))

This uses a quick and dirty approach to calculating the intersection of the plane and the ellipsoid: create a relatively fine grid of points in (x,y) and then simply identify all the points where f(x,y) - my.fun(x,y) < 0 . 这使用一种快速而肮脏的方法来计算平面和椭球的交点:在(x,y)中创建一个相对精细的点网格,然后简单地标识所有点,其中f(x,y) - my.fun(x,y) < 0 These points are inside the ellipse formed by the intersection. 这些点在相交形成的椭圆内。 Then just plot those points. 然后只需绘制这些点即可。

The idiomatic way to do this, I think, is using ploygon3d(...) but when I tried that the function did not converge... 我认为,惯用的方法是使用ploygon3d(...)但是当我尝试该功能未收敛时...

EDIT Response to OP's comment. 编辑对OP评论的回复。

So this plots the intersection of your parabolic ellipsoid with the place z = 1 and colors the result based on increasing values of x (which looks like the image you provided). 因此,这将绘制抛物面椭球与z = 1的交点,并根据x的增加值对结果进行着色(看起来像您提供的图像)。 I have no idea what the point of this is... 我不知道这是什么意思...

library(rgl)
f      <- function(x,y) rep(1,length(x))
my.fun <- function(x,y) 5*x^2 - 4*x*y + 5*y^2

x_grid <- seq(0,1, length=20)
z_grid <- outer(x_grid, x_grid, f)

theta <- seq(0,2*pi,length=1000)
r     <- sqrt(5/21)
df    <- expand.grid(x=r*cos(theta),y=r*cos(theta))
df$z  <- with(df,f(x,y) - my.fun(x,y))
xyz   <- df[df$z>=0,]
surface3d(x_grid, x_grid, z_grid, front="line",back="line")
axes3d() 
zlim    <- range(-1000*xyz$x)
zlen    <- diff(zlim) + 1
palette <- heat.colors(zlen)               # height color lookup table
col     <- palette[-1000*xyz$x-zlim[1]+1 ] # assign colors to heights for each point
with(xyz,points3d(x,y,f(x,y),color=col))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM