4

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?

  • I would guess that it can be shown mathematically that an ellipse for which any two equal angles cut the same portion out of the circonference must be a circle. In that sense the only thing that is wrong here is that you expect to have equal portions of the circonference cut out by equal angles. Code-wise it seems fine. – ImportanceOfBeingErnest Jun 02 '17 at 17:51
  • 2
    A lot of the math at https://en.wikipedia.org/wiki/Ellipse goes over my head, but it looks like you're (in part) trying to calculate the coordinates of points on the ellipse using the formula listed under "parametric representation". Note that it says "The parameter t [...] is _not_ the angle [...] with the x-axis". So at best, your `angle` variable is misleadingly named; at worst, you're printing values that have nothing to do with the data you're actually looking for. – Kevin Jun 02 '17 at 17:56
  • 2
    What code did you use to generate that picture? Maybe the error is in that code and not the code you posted. – Kevin Jun 02 '17 at 18:13
  • Try print the distance calculation at each point. I suspect there are places where the values blow up. – stark Jun 02 '17 at 18:52
  • 2
    Your calculations are correct, but your picture is wrong: you're confusing the angle that the line makes with the x-axis with the angle that you're using to parametrize the points of the ellipse. They're not the same thing. For example, the first non-horizontal line should join the origin to the point (5 * cos(radians(43)), 3 * sin(radians(43))). That line does not have an angle of 43 degrees to the horizontal (the actual angle of the line to the horizontal is approx. 29.2 degrees). – Mark Dickinson Jun 02 '17 at 18:57
  • Hmm; I should have read the other comments first. What @Kevin said. – Mark Dickinson Jun 02 '17 at 19:03
  • Take a look at these wery similar QAs: [I need an equation for equal movement along an ellipse](https://stackoverflow.com/a/26779231/2521214) , [Algorithm for shape calculation (Ellipse)](https://stackoverflow.com/a/19560243/2521214) and [Find equidistant points on ellipse](https://stackoverflow.com/a/27887568/2521214) – Spektre Jun 03 '17 at 07:51
  • @Kevin like you said I wrongly assumed angle in the parametric equations to be the same as angle with X axis. When corrected the new diagram looks like [this](https://i.imgur.com/1Cd3Kef.png) which I think is correct. The pictures were made manually (not code) in Geogebra with the angles output from the program. – absoluteIdiot Jun 03 '17 at 17:15
  • @Mark like you said I wrongly assumed angle in the parametric equations to be the same as angle with X axis. When corrected the new diagram looks like [this](https://i.imgur.com/1Cd3Kef.png) which I think is correct. – absoluteIdiot Jun 03 '17 at 17:16

2 Answers2

1

Your program divides the ellipse into equal arc lengths, not equal arcs. On an ellipse, this isn't the same. In the code below, I added the distance of each segment to the output to verify this.

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}".format(d))
onetenth = d/10
angle = 0
x0 = a
y0 = 0
angle0 = 0
for i in range(10):
    dist = 0
    while(dist<onetenth):
        angle += 0.025
        x = a * cos(radians(angle))
        y = b * sin(radians(angle))
        dist += distance(x0,y0,x,y)
        x0 = x
        y0 = y

    print(
        "{} : angle = {:.2f}\tdifference = {:.2f}\tDistance {:.2f}"
        .format(i+1,angle, angle-angle0,dist))
    angle0 = angle

Sample output:

Circumference of ellipse = 25.526979
1 : angle = 42.99   difference = 42.99  Distance 2.55
2 : angle = 75.27   difference = 32.28  Distance 2.55
3 : angle = 104.73  difference = 29.46  Distance 2.55
4 : angle = 137.01  difference = 32.28  Distance 2.55
5 : angle = 180.01  difference = 42.99  Distance 2.55
6 : angle = 223.00  difference = 42.99  Distance 2.55
7 : angle = 255.28  difference = 32.28  Distance 2.55
8 : angle = 284.74  difference = 29.46  Distance 2.55
9 : angle = 317.02  difference = 32.28  Distance 2.55
10 : angle = 360.02 difference = 43.00  Distance 2.55

Note that if you change the ellipse to a circle (ie a = b = 5), the angles and distances become uniform:

Circumference of ellipse = 31.415902
1 : angle = 36.00   difference = 36.00  Distance 3.14
2 : angle = 72.00   difference = 36.00  Distance 3.14
3 : angle = 108.00  difference = 36.00  Distance 3.14
4 : angle = 144.00  difference = 36.00  Distance 3.14
5 : angle = 180.00  difference = 36.00  Distance 3.14
6 : angle = 216.00  difference = 36.00  Distance 3.14
7 : angle = 252.00  difference = 36.00  Distance 3.14
8 : angle = 288.00  difference = 36.00  Distance 3.14
9 : angle = 324.00  difference = 36.00  Distance 3.14
10 : angle = 360.00 difference = 36.00  Distance 3.14

I also made a couple of small adjustments to the code. First, I moved the increment of the angle variable in your loop to before your calculation. On the final pass, angle was getting an extra increment after all of the calculations had occurred. I also made the increment value smaller to reduce error in the final result.

user7823241
  • 230
  • 2
  • 10
1

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

Bö macht Blau
  • 12,820
  • 5
  • 40
  • 61