类似的问题以前也有,但没有一个简单到足以让我理解下面的代码以相等的角度间隔计算椭圆的点,并求出相邻点之间的距离以得到近似圆周。然后,它将圆周划分为10个假定相等的弧,并将这些等分点形成的角度进行分组。
from math import sqrt,cos,sin,radians
def distance(x1,y1,x2,y2):
return sqrt((x2-x1)**2 + (y2-y1)**2)
a = 5
b = 3
x0 = a
y0 = 0
angle = 0
d = 0
while(angle<=360):
x = a * cos(radians(angle))
y = b * sin(radians(angle))
d += distance(x0,y0,x,y)
x0 = x
y0 = y
angle += 0.25
print "Circumference of ellipse = %f" %d
onetenth = d/10
angle = 0
x0 = a
y0 = 0
angle0 = 0.25
for i in range(10):
dist = 0
while(dist<onetenth):
x = a * cos(radians(angle))
y = b * sin(radians(angle))
dist += distance(x0,y0,x,y)
x0 = x
y0 = y
angle += 0.25
print "%d : angle = %.2f\tdifference = %.2f" %(i+1,angle-0.25, angle-angle0)
angle0 = angle
它给出输出:
Circumference of ellipse = 25.526979
1 : angle = 43.00 difference = 43.00
2 : angle = 75.50 difference = 32.50
3 : angle = 105.00 difference = 29.50
4 : angle = 137.50 difference = 32.50
5 : angle = 180.75 difference = 43.25
6 : angle = 223.75 difference = 43.00
7 : angle = 256.00 difference = 32.25
8 : angle = 285.50 difference = 29.50
9 : angle = 318.00 difference = 32.50
10 : angle = 361.50 difference = 43.50
但这些角度并不能平均地划分周长(asked and answered)我的逻辑/代码有什么问题,如何改进?
最佳答案
我是这个问题的负责人,我从评论中意识到我的假设是错误的。该程序利用椭圆的参数方程来计算x、y坐标。参数方程中的角度不是由X轴构成的角度。我以为密码里的是一样的正确的代码是
from math import sqrt,cos,sin,atan2,radians,degrees
def distance(x1,y1,x2,y2):
return sqrt((x2-x1)**2 + (y2-y1)**2)
a = 5
b = 3
x0 = a
y0 = 0
angle = 0
d = 0
while(angle<=360):
x = a * cos(radians(angle))
y = b * sin(radians(angle))
d += distance(x0,y0,x,y)
x0 = x
y0 = y
angle += 0.25
print "Circumference of ellipse = %f" %d
onetenth = d/10
angle = 0
x0 = a
y0 = 0
angle0 = 0
for i in range(10):
dist = 0
while(dist<onetenth):
x = a * cos(radians(angle))
y = b * sin(radians(angle))
dist += distance(x0,y0,x,y)
x0 = x
y0 = y
angle += 0.25
xangle = degrees(atan2(y,x))
print "%d : angle = %.2f\tdifference = %.2f" %(i+1, xangle, xangle-angle0)
angle0 = xangle
利用X点和Y坐标点的反正切求出X轴的角度。它给出输出:
Circumference of ellipse = 25.526979
1 : angle = 29.23 difference = 29.23
2 : angle = 66.68 difference = 37.46
3 : angle = 114.06 difference = 47.38
4 : angle = 151.20 difference = 37.13
5 : angle = -179.55 difference = -330.75
6 : angle = -150.13 difference = 29.42
7 : angle = -112.57 difference = 37.56
8 : angle = -65.19 difference = 47.37
9 : angle = -28.38 difference = 36.81
10 : angle = 0.90 difference = 29.28
这些角度除以椭圆的周长。
关于python - 如何平均划分椭圆的周长?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/44334655/