如何判断一个点是否在椭圆内 [英] How to determine whether a points lies in an ellipse
本文介绍了如何判断一个点是否在椭圆内的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我之前发布过类似的问题.我试图确定一个点是否位于椭圆内.基本上我会生成一些二元法线数据并创建一个椭圆.这是我使用的代码
I had posted a similar question earlier. I was trying to determine whether a point lies within an ellipse. Basically I generate some bivariate normal data and create an ellipse. Heres the code I use
library(MASS)
set.seed(1234)
x1<-NULL
x2<-NULL
k<-1
Sigma2 <- matrix(c(.72,.57,.57,.46),2,2)
Sigma2
rho <- Sigma2[1,2]/sqrt(Sigma2[1,1]*Sigma2[2,2])
eta<-replicate(300,mvrnorm(k, mu=c(-2.503,-1.632), Sigma2))
p1<-exp(eta)/(1+exp(eta))
n<-60
x1<-replicate(300,rbinom(k,n,p1[,1]))
x2<-replicate(300,rbinom(k,n,p1[,2]))
rate1<-x1/60
rate2<-x2/60
library(car)
dataEllipse(rate1,rate2,levels=c(0.05, 0.95))
我需要找出这对 (p1[,1],p1[,2]) 是否在上面椭圆的区域内.
I need to find out whether the pair (p1[,1],p1[,2]) lies within the area of the ellipse above.
推荐答案
dataEllipse
将椭圆作为多边形返回,因此您可以使用 point.in.polygon
函数来自sp
库检查点是否在椭圆内:
dataEllipse
returns the ellipses as polygons, so you could use the point.in.polygon
function from the sp
library to check whether the points are inside the ellipse:
ell = dataEllipse(rate1, rate2, levels=c(0.05, 0.95))
point.in.polygon(rate1, rate2, ell$`0.95`[,1], ell$`0.95`[,2])
<小时>
当我运行以下代码时...
When I run the following code...
library(MASS)
set.seed(1234)
x1<-NULL
x2<-NULL
k<-1
Sigma2 <- matrix(c(.72,.57,.57,.46),2,2)
Sigma2
rho <- Sigma2[1,2]/sqrt(Sigma2[1,1]*Sigma2[2,2])
eta<-replicate(300,mvrnorm(k, mu=c(-2.503,-1.632), Sigma2))
p1<-exp(eta)/(1+exp(eta))
n<-60
x1<-replicate(300,rbinom(k,n,p1[,1]))
x2<-replicate(300,rbinom(k,n,p1[,2]))
rate1<-x1/60
rate2<-x2/60
library(car)
ell = dataEllipse(rate1, rate2, levels=c(0.05, 0.95))
library(sp)
point.in.polygon(rate1, rate2, ell$`0.95`[,1], ell$`0.95`[,2])
...我得到以下输出
[1] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1
[56] 1 1 1 1 1 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
[111] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
[166] 1 1 1 1 1 1 1 0 1 1 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1 1 1
[221] 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1 1 1 1 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
[276] 1 1 1 1 1 1 1 1 1 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
这篇关于如何判断一个点是否在椭圆内的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文