FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
programmingWithPythonCode/classes/polygon.py at main · thomasWeise/programmingWithPythonCode · GitHub
thomasWeise
programmingWithPythonCode
Repository navigation
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
programmingWithPythonCode
/
classes
/
polygon.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
42 lines (33 loc) · 1.58 KB
Breadcrumbs
programmingWithPythonCode
/
classes
/
polygon.py
Copy path
File metadata and controls
42 lines (33 loc) · 1.58 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
"""A polygon is a figure described by its corner points."""
from
typing
import
Iterable
from
kahan_sum
import
KahanSum
from
point
import
Point
from
shape
import
Shape
class
Polygon
(
Shape
):
"""Polygons are shapes delimited by straight lines."""
def
points
(
self
)
->
Iterable
[
Point
]:
"""
Get a :class:`Iterable` over the points describing this polygon.
:return: the points describing the polygon
:raises NotImplementedError: Must be implemented by subclasses.
"""
raise
NotImplementedError
# must be implemented by subclasses
def
perimeter
(
self
)
->
int
|
float
:
"""
Get the perimeter of this polygon.
:return: the perimeter of this polygon
"""
previous
:
Point
|
None
=
None
# the previous point
first
:
Point
|
None
=
None
# the first point
total
:
KahanSum
=
KahanSum
()
# the total perimeter length sum
for
current
in
self
.
points
():
# Iterate over the points.
if
previous
is
None
:
# We got the first point.
previous
=
first
=
current
# Remember it for last step.
else
:
# We now have previous != None, so we can add length.
total
.
add
(
previous
.
distance
(
current
))
# Add length.
previous
=
current
# Current point becomes previous.
total
.
add
(
previous
.
distance
(
first
))
# distance back to start
return
total
.
result
()
# Return the perimeter.
def
print
(
self
)
->
None
:
"""Print the points of this polygon."""
print
(
", "
.
join
(
f"(
{
p
.
x
}
,
{
p
.
y
}
)"
for
p
in
self
.
points
()))
Back
|
FazBrowse Home
|
New Git URL