我必须检查一个点是否在半轴a和b的椭圆内。我生成了一个元组列表(点),然后生成了另一个元组列表(点),其中我只保留椭圆内的那些点
但是,生成时,有些点会从椭圆中掉出来。这个误差是通过计算累积的吗?如果是的话,我该如何改进它,使点不会从曲线中掉出来
请注意,我对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()
你的条件
(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
相关问题 更多 >
编程相关推荐