这可能与您正在做的类似.我只是想看看是否
f(x,y)= x ^ 2 / r1 ^ 2 y ^ 2 / r2 ^ 2 = 1.
当f(x,y)大于1时,点x,y在椭圆之外.当它较小时,它就在椭圆内.当f(x,y)小于1时,我遍历每个椭圆以找到一个椭圆.
该代码也没有考虑以原点为中心的椭圆.包含此功能只是一个小小的改动.
import matplotlib.pyplot as plt
import matplotlib.patches as patches
import numpy as np
def inWhichEllipse(x,y,rads):
'''
With a list of (r1,r2) pairs, rads, return the index of the pair in which
the point x,y resides. Return None as the index if it is outside all
Ellipses.
'''
xx = x*x
yy = y*y
count = 0
ithEllipse =0
while True:
rx,ry = rads[count]
ellips = xx/(rx*rx)+yy/(ry*ry)
if ellips < 1:
ithEllipse = count
break
count+=1
if count >= len(rads):
ithEllipse = None
break