Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from PIL import Image, ImageDraw
- def get_ellipse_penalty(f0, f1, r):
- def ellipse_penalty(point):
- x, y = point
- d0 = ((x - f0[0]) ** 2 + (y - f0[1]) ** 2) ** 0.5
- d1 = ((x - f1[0]) ** 2 + (y - f1[1]) ** 2) ** 0.5
- return abs(r - d0 - d1)
- return ellipse_penalty
- def get_cassini_penalty(f0, f1, r):
- def cassini_penalty(point):
- x, y = point
- d0 = (x - f0[0]) ** 2 + (y - f0[1]) ** 2
- d1 = (x - f1[0]) ** 2 + (y - f1[1]) ** 2
- return abs(r ** 4 - d0 * d1)
- return cassini_penalty
- def surrounding_points(point):
- x, y = point
- offsets = [(-1, -1), (-1, 0), (-1, 1), (0, -1),
- (0, 1), (1, -1), (1, 0), (1, 1)]
- return {(x + i, y + j) for i, j in offsets}
- def gen_points(penalty, init):
- # initialize first two points
- # I'll need two to track pathing in order to find where
- # path should be complete.
- next_point = tuple(init)
- maybe_points = surrounding_points(next_point)
- last_point, next_point = next_point, min(maybe_points, key=penalty)
- i = 0 # start counter
- points = {} # kinda depending on dictionary maintaining order
- while (next_point, last_point) not in points:
- points[(next_point, last_point)] = i
- maybe_points = surrounding_points(next_point) - {last_point} - surrounding_points(last_point)
- last_point, next_point = next_point, min(maybe_points, key=penalty)
- i += 1
- return [point for (point, _), i in points.items() if i >= points[(next_point, last_point)]]
- # im = Image.new("RGB", (500, 500), "black")
- # draw = ImageDraw.Draw(im)
- # penalty = get_ellipse_penalty(f0=(400, 150), f1=(100, 200), r=350)
- # points = gen_points(penalty, init=(1, 0))
- # draw.point(points, 'yellow')
- # im.show()
- im = Image.new("RGB", (500, 500), "black")
- draw = ImageDraw.Draw(im)
- penalty = get_cassini_penalty(f0=(125, 125), f1=(375, 375), r=250 / 2 ** .5)
- points = gen_points(penalty, init=(250, 306))
- draw.point(points, 'yellow')
- im.show()
RAW Paste Data