Similar questions were asked and answered previously here but none simple enough for me to understand . The below code calculates the points of an ellipse at equal angle intervals and sums the distances between adjacent points to get an approximate circumference. It then divides circumference into 10 supposedly equal arcs and ouputs the angles made by the dividing points.
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
It gives the output:
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
But these angles do not divide the circumference equally (picture). What is wrong with my logic/code and how can I improve it?