-
Notifications
You must be signed in to change notification settings - Fork 0
/
ellipse.py
59 lines (47 loc) · 1.58 KB
/
ellipse.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import sys
import math
DIVISION = 1000.0
CYCLE = 10
def angles(du, a, b):
"""Yeild angles of points on a ellipse.
@param du - distance of two points which is next to each other
@param a, b - length of two semi-axes
"""
phi = 0
while phi <= 2 * math.pi:
yield phi
phi += du / math.sqrt((a * math.sin(phi))**2 + (b * math.cos(phi))**2)
def coordinate(du, a, b):
"""Yeild coordinates of points on a ellipse.
@param du - distance of two points which is next to each other
@param a, b - length of two semi-axes
"""
for angle in angles(du, a, b):
yield (a * math.cos(angle), b * math.sin(angle))
def circumference(a, b):
"""Return a length of a circumference of an ellipse.
@param a, b - length of two semi-axes
reference: http://en.wikipedia.org/wiki/Ellipse#Circumference
"""
expr = ((a - b)/(a + b))**2
return math.pi * (a + b) * (1 + (3 * expr)/(10 + math.sqrt(4 - 3 * expr)))
def draw(argv=None):
if not argv:
argv = sys.argv
filename = argv[1]
a = float(argv[2])
b = float(argv[3])
du = circumference(a, b) / DIVISION
with open(filename, 'w') as out_file:
print >> out_file, 'plot "-" w l'
print >> out_file, '#x\ty'
for i, coord in enumerate(coordinate(du, a, b)):
i %= CYCLE * 2
if i < CYCLE:
print >> out_file, '{0}\t{1}'.format(*coord)
else:
print >> out_file, ''
print >> out_file, 'end'
print >> out_file, 'pause -1'
if __name__ == '__main__':
draw(sys.argv)