-
-
Notifications
You must be signed in to change notification settings - Fork 50.5k
Expand file tree
/
Copy patharc_length.py
More file actions
55 lines (45 loc) · 1.41 KB
/
arc_length.py
File metadata and controls
55 lines (45 loc) · 1.41 KB
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
from math import pi
def arc_length(angle: float, radius: float) -> float:
"""
Calculate the arc length of a circle.
The arc length is the distance along the curved line making up the arc.
Formula: arc_length = 2 * pi * radius * (angle / 360)
Wikipedia: https://en.wikipedia.org/wiki/Arc_length
Args:
angle: The angle in degrees
radius: The radius of the circle
Returns:
The arc length as a float
>>> arc_length(45, 5)
3.9269908169872414
>>> arc_length(120, 15)
31.415926535897928
>>> arc_length(90, 10)
15.707963267948966
>>> arc_length(0, 10)
0.0
>>> arc_length(360, 5)
31.41592653589793
>>> arc_length(180, 10)
31.41592653589793
>>> arc_length(45.5, 10.5)
8.329618601250994
>>> arc_length(-90, 10)
Traceback (most recent call last):
...
ValueError: arc_length() only accepts non-negative values
>>> arc_length(90, -10)
Traceback (most recent call last):
...
ValueError: arc_length() only accepts non-negative values
>>> arc_length(-45, -5)
Traceback (most recent call last):
...
ValueError: arc_length() only accepts non-negative values
"""
if angle < 0 or radius < 0:
raise ValueError("arc_length() only accepts non-negative values")
return 2 * pi * radius * (angle / 360)
if __name__ == "__main__":
import doctest
doctest.testmod()