Created
October 2, 2025 21:09
-
-
Save ashap5/d2455f96ec1a34cd4bc9512f9077e1f0 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 math | |
| import random | |
| import tkinter as tk | |
| # Configuration | |
| WINDOW_WIDTH = 800 | |
| WINDOW_HEIGHT = 600 | |
| GRID_SIZE = 20 | |
| OFFSET = 200 # Offset from edges for circle positioning | |
| # Create window | |
| canvas = tk.Canvas(width=WINDOW_WIDTH, height=WINDOW_HEIGHT) | |
| canvas.pack() | |
| def calculate_intersection_points(x1, y1, r1, x2, y2, r2): | |
| """Calculate how many points two circles intersect at.""" | |
| # Distance between centers | |
| distance = math.sqrt((x1 - x2) ** 2 + (y1 - y2) ** 2) | |
| # Identical circles | |
| if x1 == x2 and y1 == y2 and r1 == r2: | |
| return "infinite" | |
| # Circles are too far apart | |
| if distance > r1 + r2: | |
| return 0 | |
| # One circle is inside the other without touching | |
| if distance < abs(r1 - r2): | |
| return 0 | |
| # Circles touch externally or internally (exactly one point) | |
| if distance == r1 + r2 or distance == abs(r1 - r2): | |
| return 1 | |
| # Circles intersect at two points | |
| return 2 | |
| def draw_circles(): | |
| """Generate and draw two random circles, then calculate intersections.""" | |
| canvas.delete("all") | |
| # Draw grid background | |
| for i in range(0, WINDOW_WIDTH, GRID_SIZE): | |
| for j in range(0, WINDOW_HEIGHT, GRID_SIZE): | |
| canvas.create_rectangle( | |
| i, j, i + GRID_SIZE, j + GRID_SIZE, | |
| fill="white", outline="lightgray" | |
| ) | |
| # Generate random circle parameters | |
| x1, y1, r1 = random.randint(1, 10), random.randint(1, 10), random.randint(1, 10) | |
| x2, y2, r2 = random.randint(1, 10), random.randint(1, 10), random.randint(1, 10) | |
| # Scale and position circles | |
| scale = GRID_SIZE | |
| # Draw first circle | |
| canvas.create_oval( | |
| OFFSET + x1 * scale - r1 * scale, | |
| OFFSET + y1 * scale - r1 * scale, | |
| OFFSET + x1 * scale + r1 * scale, | |
| OFFSET + y1 * scale + r1 * scale, | |
| outline="blue", width=2 | |
| ) | |
| # Draw second circle | |
| canvas.create_oval( | |
| OFFSET + x2 * scale - r2 * scale, | |
| OFFSET + y2 * scale - r2 * scale, | |
| OFFSET + x2 * scale + r2 * scale, | |
| OFFSET + y2 * scale + r2 * scale, | |
| outline="red", width=2 | |
| ) | |
| # Calculate results | |
| distance = math.sqrt((x1 - x2) ** 2 + (y1 - y2) ** 2) | |
| intersection_count = calculate_intersection_points(x1, y1, r1, x2, y2, r2) | |
| # Display information | |
| canvas.create_text( | |
| WINDOW_WIDTH / 2, 30, | |
| text=f"Distance between centers: {distance:.2f}\nRadii: {r1} and {r2}", | |
| font=("Arial", 12) | |
| ) | |
| canvas.create_text( | |
| WINDOW_WIDTH / 2, 80, | |
| text=f"Circles intersect at {intersection_count} point(s)", | |
| font=("Arial", 14, "bold") | |
| ) | |
| # Initial instruction text | |
| canvas.create_text( | |
| WINDOW_WIDTH / 2, WINDOW_HEIGHT / 2, | |
| text="Click to generate random circles", | |
| font=("Arial", 16) | |
| ) | |
| # Bind mouse click to generate circles | |
| canvas.bind("<Button-1>", lambda event: draw_circles()) | |
| canvas.mainloop() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment