-
Notifications
You must be signed in to change notification settings - Fork 2
/
draw_a_circle.py
64 lines (52 loc) · 1.93 KB
/
draw_a_circle.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import pyautogui
import math
import time
import keyboard
START_TIMER = 3 # seconds before drawing starts
CIRCLE_RADIUS = 300 # size of the circle to draw
STEP_INCREMENT = 12 # degrees to increment for each step in the circle drawing
def wait_for_enter_key() -> None:
print("Move the mouse to the center of the circle and press Enter to start.")
while keyboard.read_key() != "enter":
time.sleep(0.1) # Prevent high CPU usage
def countdown(seconds: int) -> None:
for remaining in range(max(0, seconds), -1, -1):
print(f"Starting in {remaining}...")
time.sleep(1)
def get_circle_points(center: tuple[float, float], radius: float, steps: int = 360) -> list[tuple[float,float]]:
points = []
for i in range(0, steps + 1, STEP_INCREMENT):
angle = math.radians(i)
x = center[0] + radius * math.cos(angle)
y = center[1] + radius * math.sin(angle)
points.append((x, y))
return points
def draw_circle(center: tuple[float,float], radius: float) -> bool:
start_position = (center[0] + radius, center[1])
pyautogui.moveTo(*start_position)
pyautogui.mouseDown()
for point in get_circle_points(center, radius):
if keyboard.is_pressed('esc'):
pyautogui.mouseUp()
return False
pyautogui.moveTo(*point)
pyautogui.mouseUp()
return True
def main():
if 360 % STEP_INCREMENT != 0:
print("STEP_INCREMENT must be a divisor of 360 for a complete circle.")
return
if CIRCLE_RADIUS < 1:
print("CIRCLE_RADIUS must be greater than 0.")
return
wait_for_enter_key()
countdown(START_TIMER)
center = pyautogui.position()
print(f"Center defined as {center}.")
print("Drawing the circle. Press Esc to cancel.")
if draw_circle(center, CIRCLE_RADIUS):
print("Circle drawing complete.")
else:
print("Drawing cancelled.")
if __name__ == "__main__":
main()