-
Notifications
You must be signed in to change notification settings - Fork 0
/
darts.py
116 lines (88 loc) · 3.3 KB
/
darts.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
import pygame
import screens
import generator
"""
This is a 'Pi' Calculator Simulation
You will be asked to enter the number of darts to use to calculate pi by the
following algorithm:
• A SQUARE screen will appear which contains a CIRCLE
• the diameter of the CIRCLE is exactly equal to the side of the SQUARE
• the darts will be placed one-by-one (randomly) inside the SQUARE
• let the total number of darts be Nₜ
• let the number of darts landing in the CIRCLE be Nₒ
• ratio Nₒ / Nₜ ∝ Area(CIRCLE) / Area(SQUARE) ≡ π / 4
when the diameter of the CIRCLE is exactly equal to side of the SQUARE
• so, 4 * (Nₒ/Nₜ) is a close approximation of pi, which gets better and
better for higher number of darts (as Nₜ → ∞)
Controls:
Press Space-Bar to stop at any point of time
Press P to print the current calculated value of Pi and Percentage Error
Press A / M to print the average / mean value of Pi over the last 50 terms
and Percentage Error in it
"""
screens.main()
SIDE: int = 650 # side of main screen and the SQUARE
RADIUS: int = SIDE // 2 # radius of the circle
CENTER: tuple[int, int] = (RADIUS, RADIUS)
WINDOW = pygame.display.set_mode((SIDE, SIDE))
FPS: int = 60
DARTS: int = screens.darts
INSIDE: int = 0
# colors
Color = tuple[int, int, int]
WHITE: Color = (255, 255, 255)
BLACK: Color = (0, 0, 0)
RED: Color = (255, 0, 0)
BLUE: Color = (0, 0, 255)
pygame.display.set_caption("Pi Calculator")
def draw_dart() -> None:
"""draws the darts one-by-one on the screen"""
global INSIDE
pair = (x, y) = generator.pair(SIDE)
if generator.inside(pair, CENTER, RADIUS):
INSIDE += 1
pygame.draw.rect(WINDOW, RED, (x, y, 2, 2))
else:
pygame.draw.rect(WINDOW, BLUE, (x, y, 2, 2))
def main() -> None:
total = 0
clock = pygame.time.Clock()
cache = []
WINDOW.fill(WHITE)
pygame.draw.circle(WINDOW, BLACK, CENTER, RADIUS, width=3)
pygame.display.update()
RUN = True
while RUN:
clock.tick(FPS)
for event in pygame.event.get():
if event.type == pygame.QUIT:
RUN = False
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE:
RUN = False
elif event.key == pygame.K_p:
print(f"{total=},",
(pi := generator.calculate(INSIDE, total)),
generator.error(pi)
)
elif event.key in (pygame.K_a, pygame.K_m):
print("avg(π) =",
(pi := sum(cache)/len(cache)), generator.error(pi)
)
if RUN and total < DARTS:
draw_dart()
if len(cache) > 50:
cache = cache[1:]
if total >= 1:
cache.append(generator.calculate(INSIDE, total))
total += 1
else:
RUN = False
screens.ending(
total,
(pi := generator.calculate(INSIDE, total)), generator.error(pi)
)
pygame.display.update()
pygame.quit()
if __name__ == "__main__":
main()