Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # tk_circle_by_area.py
- import tkinter as tk
- import math
- from PIL import Image, ImageDraw, ImageTk
- ww = hh = 200
- cc = hh // 2
- area = 500
- radius = pow(area / math.pi, 0.5)
- r = int(radius)
- root = tk.Tk()
- root.title("Circle By Area")
- canvas = tk.Canvas(root, width=ww, height=hh)
- canvas.pack()
- image = Image.new("RGB", (ww, hh), (255, 255, 255))
- draw = ImageDraw.Draw(image)
- draw.ellipse((cc - r, cc - r, cc + r, cc + r), fill=(255, 0, 0))
- t = area + 1
- radius += 1
- while 1:
- pixel_colors = {}
- for x in range(cc - r, cc + r + 1):
- for y in range(cc - r, cc + r + 1):
- if (x - cc) ** 2 + (y - cc) ** 2 <= radius ** 2:
- pixel_color = image.getpixel((x, y))
- pixel_colors[(x, y)] = pixel_color
- photo = ImageTk.PhotoImage(image)
- canvas.create_image(0, 0, image=photo, anchor=tk.NW)
- t = len(pixel_colors.values())
- print(t, radius)
- if t >= area:
- radius -= 0.01
- else:
- break
- root.update()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement