椭圆的Meshgrid /栅格/矩阵

2024-04-25 01:15:05 发布

您现在位置:Python中文网/ 问答频道 /正文

我可能用错了词,但我正在寻求帮助。在

我想为位于椭圆形状周长内的网格生成一个x,y值数组。在

这里有代码:http://people.sc.fsu.edu/~jburkardt/c_src/ellipse_grid/ellipse_grid.html用Python来完成这个任务。在

然而,就我的目的而言,椭圆已经旋转了一定程度。当前的公式没有考虑到这一点,需要一些帮助来解释这种转换,不确定如何更改代码来实现这一点?在

我一直在调查np.网格如果有更好的方法,请说。在

非常感谢。在


Tags: 代码srchttp网格html数组peoplegrid
2条回答

要在椭圆内生成点阵点,我们必须知道水平线和椭圆相交的位置。在

以θ角旋转的零中心椭圆方程:

 x = a * Cos(t) * Cos(theta) - b * Sin(t) * Sin(theta)   
 y = a * Cos(t) * Sin(theta) + b * Sin(t) * Cos(theta)

为了简化计算,我们可以引入伪角Fi和幅值M(给定椭圆的常数)

^{pr2}$

所以

 y = M * Sin(Fi) * Cos(t) + M * Cos(Fi) * Sin(t)
 y/M = Sin(Fi) * Cos(t) +  Cos(Fi) * Sin(t)
 y/M = Sin(Fi + t) 

在y位置给定水平线的解为

 Fi + t = ArcSin( y / M)
 Fi + t = Pi - ArcSin( y / M)
 t1 = ArcSin( y / M) - Fi        //note two values
 t2 = Pi - ArcSin( y / M) - Fi

将第一个方程中t的两个值代入,得到给定Y的X值,生成一个点阵点序列

要获得顶部和底部坐标,请区分y

y' = M * Cos(Fi + t) = 0
th = Pi/2 - Fi
tl = -Pi/2 - Fi

找到相应的y,并将它们用作直线的起始和结束y坐标。在

import math

def ellipselattice(cx, cy, a, b, theta):
    res = []
    at = a * math.sin(theta)
    bt = b * math.cos(theta)
    Fi = math.atan2(at, bt)
    M = math.hypot(at, bt)
    ta = math.pi/2 - Fi
    tb = -math.pi/2 - Fi
    y0 = at * math.cos(ta) + bt *math.sin(ta)
    y1 = at * math.cos(tb) + bt *math.sin(tb)
    y0, y1 = math.ceil(cy + min(y0, y1)), math.floor(cy + max(y0, y1))
    for y  in range(y0, y1+1):
        t1 = math.asin(y / M) - Fi
        t2 = math.pi - math.asin(y / M) - Fi
        x1 = a * math.cos(t1) * math.cos(theta) - b* math.sin(t1) * math.sin(theta)
        x2 = a * math.cos(t2) * math.cos(theta) - b* math.sin(t2) * math.sin(theta)
        x1, x2 = math.ceil(cx + min(x1, x2)), math.floor(cx + max(x1, x2))
        line = [(x, y) for x in range(x1, x2 + 1)]
        res.append(line)
    return res

print(ellipselattice(0, 0, 4, 3, math.pi / 4))

给出了欧几里德平面上椭圆的最一般形式为二次曲线

f(x,y) = a x^2 + 2b x y + c y^2 + 2d x + 2f y + g,

我们可以通过以下方法计算中心(x0,y0)

^{pr2}$

(见方程式19和20在Ellipse on MathWorld)。长轴a_m的长度可由同页上的等式21计算。在

现在只要找到圆心为(x0,y0)和半径为a_m的圆内的所有网格点(x,y)

sign(f(x,y)) = sign(f(x0,y0)).

相关问题 更多 >