# walk ellipse with scaling

Apr 5th, 2021 (edited)
576
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. import matplotlib.pyplot as plt
2.
3. def get_ellipse_thing(f0, f1, r):
4.     def ellipse_thing(point):
5.         x, y = point
6.         d0 = ((x - f0[0]) ** 2 + (y - f0[1]) ** 2) ** 0.5
7.         d1 = ((x - f1[0]) ** 2 + (y - f1[1]) ** 2) ** 0.5
8.         return abs(r - d0 - d1)
9.     return ellipse_thing
10.
11. def get_cassini_thing(f0, f1, r):
12.     def cassini_thing(point):
13.         x, y = point
14.         d0 = ((x - f0[0]) ** 2 + (y - f0[1]) ** 2)
15.         d1 = ((x - f1[0]) ** 2 + (y - f1[1]) ** 2)
16.         return abs(r * r - d0 * d1)
17.     return cassini_thing
18.
19. def scale_my_thing(thing, scale):
20.     def new_thing(point):
21.         x, y = point
22.         return thing((x / scale, y / scale))
23.     return new_thing
24.
25. def surrounding_points(point):
26.     x, y = point
27.     offsets = [(-1, -1), (-1, 0), (-1, 1), (0, -1),
28.                (0, 1), (1, -1), (1, 0), (1, 1)]
29.     return {(x + i, y + j) for i, j in offsets}
30.
31. def gen_points(thing, init, scale=1):
32.     thing = scale_my_thing(thing, scale)
33.     points = {} # kinda depending on dictionary maintaining order
34.
35.     next_point = tuple(int(c * scale) for c in init)
36.     last_point = next_point
37.
38.     i = 0 # start counter
39.     while next_point not in points:
40.
41.         i += 1
42.         if i > 1_000_000:
43.             ValueError('You iterated too much.')
44.
45.         points[next_point] = i
46.         maybe_points = surrounding_points(next_point) - {last_point}
47.         last_point, next_point = next_point, min(maybe_points, key=thing)
48.
49.     return [(x / scale, y / scale) for (x, y), i in points.items() if i >= points[next_point]]
50.
51. thing = get_ellipse_thing(f0=(0, 0), f1=(1, 1), r=2)
52. # init is the initial point.  give it something reasonable.
53. ellipse_points = gen_points(thing, init=(1, 0), scale=100)
54.
55. x, y = zip(*ellipse_points)
56.
57. plt.scatter(x, y, marker='s', s=1)
58. fig = plt.gcf()
59. fig.set_size_inches(10, 10)
RAW Paste Data