piRSquared

PIL Ellipse Bare Bones

Apr 6th, 2021
336
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. from PIL import Image, ImageDraw
  2.  
  3. def get_ellipse_penalty(f0, f1, r):
  4.     def ellipse_penalty(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_penalty
  10.  
  11. def surrounding_points(point):
  12.     x, y = point
  13.     offsets = [(-1, -1), (-1, 0), (-1, 1), (0, -1),
  14.                (0, 1), (1, -1), (1, 0), (1, 1)]
  15.     return {(x + i, y + j) for i, j in offsets}
  16.  
  17. def gen_points(thing, init):
  18.     points = {} # kinda depending on dictionary maintaining order
  19.  
  20.     next_point = tuple(init)
  21.     last_point = next_point
  22.    
  23.     i = 0 # start counter
  24.     while next_point not in points:
  25.        
  26.         points[next_point] = i
  27.         maybe_points = surrounding_points(next_point) - {last_point}
  28.         last_point, next_point = next_point, min(maybe_points, key=thing)
  29.        
  30.         i += 1
  31.        
  32.     return [(x, y) for (x, y), i in points.items() if i >= points[next_point]]
  33.  
  34. im = Image.new("RGB", (500, 500), "black")
  35. draw = ImageDraw.Draw(im)
  36.  
  37. penalty = get_ellipse_penalty(f0=(450, 50), f1=(50, 450), r=600)
  38. points = gen_points(penalty, init=(1, 0))
  39.  
  40. draw.point(points, 'white')
  41. im.show()
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×