FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
programmingWithPythonCode/classes/point.py at main · mlecjm/programmingWithPythonCode · GitHub
mlecjm
programmingWithPythonCode
Repository navigation
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
programmingWithPythonCode
/
classes
/
point.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
48 lines (38 loc) · 1.21 KB
Breadcrumbs
programmingWithPythonCode
/
classes
/
point.py
Copy path
File metadata and controls
48 lines (38 loc) · 1.21 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
"""A simple class for points."""
from
math
import
isfinite
,
sqrt
from
typing
import
Final
class
Point
:
"""
A class for representing a point in the two-dimensional plane.
>>> p = Point(1, 2.5)
>>> p.x
1
>>> p.y
2.5
>>> try:
... Point(1, 1e308 * 1e308)
... except ValueError as ve:
... print(ve)
x=1 and y=inf must both be finite.
"""
def
__init__
(
self
,
x
:
int
|
float
,
y
:
int
|
float
)
->
None
:
"""
The constructor: Create a point and set its coordinates.
:param x: the x-coordinate of the point
:param y: the y-coordinate of the point
"""
if
not
(
isfinite
(
x
)
and
isfinite
(
y
)):
raise
ValueError
(
f"x=
{
x
}
and y=
{
y
}
must both be finite."
)
#: the x-coordinate of the point
self
.
x
:
Final
[
int
|
float
]
=
x
#: the y-coordinate of the point
self
.
y
:
Final
[
int
|
float
]
=
y
def
distance
(
self
,
p
:
"Point"
)
->
float
:
"""
Get the distance to another point.
:param p: the other point
:return: the distance
>>> Point(1, 1).distance(Point(4, 4))
4.242640687119285
"""
return
sqrt
((
self
.
x
-
p
.
x
)
**
2
+
(
self
.
y
-
p
.
y
)
**
2
)
Back
|
FazBrowse Home
|
New Git URL