2016-11-24 107 views
0

enter image description here曲线上的点掉出曲线

我必须检查一个点是否在半椭圆a和b的椭圆内。我生成了一个元组列表(点),然后生成另一个元组列表(dotsin),我只保留那些在椭圆内的点。

但是,生成时,有些点会掉出椭圆。这个错误是否会累积谷值计算,如果是的话,我该如何改进,使点不会脱离曲线?

请注意,我在Python中有点生疏,有些事情对我来说并不明显。 在此先感谢!

dots=[(random.uniform(-a,a),random.uniform(-b,b)) for i in range(1000)]#;dots 
    dotsin=[(x,y) for x,y in dots if (x**2 + y**2)<((a*cos(atan(y/x)))**2 + (b*sin(atan(y/x)))**2)]#;dotsin 
    plt.scatter([x[0] for x in dotsin],[y[1] for y in dotsin]) 
    plt.grid() 
+0

的图像是“在这里输入的形象描述”下,如果你感到困惑,因为页面将不会允许我还张贴图片。 –

回答

0

您的状态(x**2 + y**2)<((a*cos(atan(y/x)))**2 + (b*sin(atan(y/x)))**2)相当于(x**2 + y**2)**2 < (a*x) ** 2 + (b*y) ** 2。这不是椭圆方程。椭圆公式为(x/a)**2 + (y/b)**2 < 1

from numpy import random 
import numpy as np 
from math import sin, cos, atan 
import matplotlib.pyplot as plt 
%matplotlib inline 
a = 3 
b = 2 
n = 1000 
dots=[(random.uniform(-a,a),random.uniform(-b,b)) for i in range(n)]#;dots 
dotsin=[(x,y) for x,y in dots if (x/a)**2 + (y/b)**2 < 1]#;dotsin 
plt.scatter([x[0] for x in dotsin],[y[1] for y in dotsin]) 
phi = np.linspace(0, 2*np.pi, 200) 
plt.plot(a*np.cos(phi), b*np.sin(phi)) 
plt.grid() 

ellips

+0

现在我觉得这该死的愚蠢,下次我应该仔细阅读我的代码。感谢您向我展示我的方式错误。 –

+0

如果答案回答了您的问题,您可以通过单击左侧的复选标记(也可能是buttom“up”)来接受(并且可能的话)。 –