-
Notifications
You must be signed in to change notification settings - Fork 20
/
geometry.py
81 lines (62 loc) · 1.68 KB
/
geometry.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
"""Some computational geometry.
Includes definitions for points and circles, and some operations
thereupon.
"""
from math import pi, sqrt
from typing import NamedTuple
def sqr(x: float) -> float:
"""Computes the square of the given number.
>>> sqr(0)
0
>>> sqr(5)
25
"""
return x * x
class Position(NamedTuple):
"""Represents a 2-D position."""
x: float
y: float
def distance(p: Position, q: Position) -> float:
"""Computes the Euclidean distance between two positions.
>>> a = Position(0, 0)
>>> b = Position(3, 4)
>>> distance(a, a)
0.0
>>> distance(a, b)
5.0
"""
dx = p.x - q.x
dy = p.y - q.y
return sqrt(sqr(dx) + sqr(dy))
def manhattan_distance(p: Position, q: Position) -> float:
"""Computes the Manhattan distance between two positions.
>>> a = Position(0, 0)
>>> b = Position(3, 4)
>>> manhattan_distance(a, a)
0
>>> manhattan_distance(a, b)
7
"""
dx = p.x - q.x
dy = p.y - q.y
return abs(dx) + abs(dy)
class Circle(NamedTuple):
"""Represents a circle positioned on a 2-D plane."""
center: Position
radius: float
def area(self) -> float:
"""Computes the area of this circle.
>>> Circle(Position(2, 7), 1).area() - pi
0.0
>>> Circle(Position(2, 7), 3).area() - 9 * pi
0.0
"""
return pi * sqr(self.radius)
def circumference(self) -> float:
"""Computes the circumference of this circle
>>> Circle(Position(2, 7), 1).circumference() - 2 * pi
0.0
>>> Circle(Position(2, 7), 3).circumference() - 6 * pi
0.0
"""
return 2 * pi * self.radius