-
Notifications
You must be signed in to change notification settings - Fork 0
/
calc.py
36 lines (27 loc) · 951 Bytes
/
calc.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
"""Common pi calculation functions
This file is part of the DrawPi project, Copyright © 2023 JonLiuFYI
SPDX-License-Identifier: Apache-2.0
"""
from math import sqrt
from point import Point
def diameter(pts: list[Point]) -> float:
"""Find the "diameter" of the circle drawing (mean of width and height)"""
s = sorted(pts) # sorted by key x implicitly (thanks NamedTuple)
max_x = s[-1].x
min_x = s[0].x
width = max_x - min_x
s = sorted(pts, key=lambda p: p.y)
max_y = s[-1].y
min_y = s[0].y
height = max_y - min_y
return (width + height) / 2
def circumference(pts: list[Point]) -> float:
"""Find the "circumference" of the circle drawing (aka perimeter)"""
out = 0
for i, this_p in enumerate(pts):
prev_p = pts[i - 1]
out += sqrt(
(this_p.x - prev_p.x) * (this_p.x - prev_p.x)
+ (this_p.y - prev_p.y) * (this_p.y - prev_p.y)
)
return out