Created
April 5, 2023 06:22
-
-
Save mcneds/eb0ff8b4f53597c51d1acd02065c37da to your computer and use it in GitHub Desktop.
inscribed circle tkinter
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import tkinter as tk | |
import math | |
# define constants | |
CANVAS_WIDTH = 1200 | |
CANVAS_HEIGHT = 720 | |
DEFAULT_SIDES = 4 | |
DEFAULT_LENGTH = 100 | |
# initialize Tkinter | |
root = tk.Tk() | |
# create canvas for drawing | |
canvas = tk.Canvas(root, width=CANVAS_WIDTH, height=CANVAS_HEIGHT) | |
canvas.pack() | |
# create variables for sliders | |
sides_var = tk.IntVar() | |
sides_var.set(DEFAULT_SIDES) | |
length_var = tk.DoubleVar() | |
length_var.set(DEFAULT_LENGTH) | |
def circle_from_points(points): | |
""" | |
Calculate the center and radius of a circle from a list of points. | |
""" | |
# calculate coordinates of centroid | |
x = y = 0 | |
for point in points: | |
x += point[0] | |
y += point[1] | |
x /= len(points) | |
y /= len(points) | |
# calculate distance from centroid to farthest point | |
r = 0 | |
for point in points: | |
d = math.sqrt((point[0] - x)**2 + (point[1] - y)**2) | |
r = max(r, d) | |
return x, y, r | |
# function to update polygon and circle | |
def update(value=1.0): | |
# get values from sliders | |
n = sides_var.get() | |
s = length_var.get() * float(value) * .02 #scaling factor | |
# calculate radius of inscribed circle | |
r = (s / 2) / math.tan(math.pi / n) | |
# calculate coordinates of polygon vertices | |
center_x = CANVAS_WIDTH / 2 | |
center_y = CANVAS_HEIGHT / 2 | |
polygon_points = [] | |
for i in range(n): | |
x = center_x + r * math.cos(2 * math.pi * i / n - math.pi / 2) | |
y = center_y + r * math.sin(2 * math.pi * i / n - math.pi / 2) | |
polygon_points.append((x, y)) | |
# calculate coordinates of inscribed circle | |
circle_points = [] | |
for i in range(n): | |
x1, y1 = polygon_points[i] | |
x2, y2 = polygon_points[(i + 1) % n] | |
mid_x = (x1 + x2) / 2 | |
mid_y = (y1 + y2) / 2 | |
circle_points.append((mid_x, mid_y)) | |
circle_x, circle_y, circle_r = circle_from_points(circle_points) | |
# clear canvas | |
canvas.delete(tk.ALL) | |
# draw polygon | |
canvas.create_polygon(polygon_points, outline="blue", width=2) | |
# draw inscribed circle | |
canvas.create_oval( | |
circle_x - circle_r, | |
circle_y - circle_r, | |
circle_x + circle_r, | |
circle_y + circle_r, | |
outline="red", | |
width=2 | |
) | |
# create labels for sliders | |
sides_label = tk.Label(root, text="Number of Sides:") | |
sides_label.pack() | |
# create sliders | |
sides_slider = tk.Scale(root, from_=3, to=20, orient=tk.HORIZONTAL, variable=sides_var, command=update) | |
sides_slider.pack() | |
# create labels for sliders | |
length_label = tk.Label(root, text="Length of Sides:") | |
length_label.pack() | |
# create sliders | |
length_slider = tk.Scale(root, from_=10, to=300, orient=tk.HORIZONTAL, variable=length_var, command=update) | |
length_slider.pack() | |
# call update function to draw initial polygon and circle | |
update() | |
# run Tkinter event loop | |
root.mainloop() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment