Advertisement
here2share

# tk_circle_by_area.py

Jun 26th, 2024 (edited)
485
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.93 KB | None | 0 0
  1. # tk_circle_by_area.py
  2.  
  3. import tkinter as tk
  4. import math
  5. from PIL import Image, ImageDraw, ImageTk
  6.  
  7. ww = hh = 200
  8. cc = hh // 2
  9.  
  10. area = 500
  11. radius = pow(area / math.pi, 0.5)
  12. r = int(radius)
  13.  
  14. root = tk.Tk()
  15. root.title("Circle By Area")
  16.  
  17. canvas = tk.Canvas(root, width=ww, height=hh)
  18. canvas.pack()
  19.  
  20. image = Image.new("RGB", (ww, hh), (255, 255, 255))
  21. draw = ImageDraw.Draw(image)
  22. draw.ellipse((cc - r, cc - r, cc + r, cc + r), fill=(255, 0, 0))
  23.  
  24. t = area + 1
  25. radius += 1
  26. while 1:
  27.     pixel_colors = {}
  28.     for x in range(cc - r, cc + r + 1):
  29.         for y in range(cc - r, cc + r + 1):
  30.             if (x - cc) ** 2 + (y - cc) ** 2 <= radius ** 2:
  31.                 pixel_color = image.getpixel((x, y))
  32.                 pixel_colors[(x, y)] = pixel_color
  33.                
  34.     photo = ImageTk.PhotoImage(image)
  35.     canvas.create_image(0, 0, image=photo, anchor=tk.NW)
  36.  
  37.     t = len(pixel_colors.values())
  38.     print(t, radius)
  39.     if t >= area:
  40.         radius -= 0.01
  41.     else:
  42.         break
  43.  
  44.     root.update()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement