pythonmathgeometryellipse

How to divide circumference of an ellipse equally?


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?


Solution

  • I am the OP of the question and I realized from comments that I made a wrong assumption. The code makes use of parametric equations of ellipse to calculate x and y coordinates. The angle in the parametric equations is not the angle made with the x axis. I assumed they are the same in the code. The corrected code is

    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
    

    It calculates the angle with x axis by taking arc tangent of a point's x and y coordinates. It gives the output:

    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
    

    These angles divide the circumference of ellipse almost equally.

    ellipse divided into sections