FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
programmingWithPythonCode/classes/rectangle.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
/
rectangle.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
60 lines (45 loc) · 1.77 KB
Breadcrumbs
programmingWithPythonCode
/
classes
/
rectangle.py
Copy path
File metadata and controls
60 lines (45 loc) · 1.77 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
49
50
51
52
53
54
55
56
57
58
59
60
"""
A class for rectangles.
>>> Rectangle(Point(22, 1), Point(4, 12)).print()
(4, 1), (4, 12), (22, 12), (22, 1)
"""
from
typing
import
Final
from
point
import
Point
from
polygon
import
Polygon
class
Rectangle
(
Polygon
):
"""A rectangle defined by its bottom-left and top-right corners."""
def
__init__
(
self
,
p1
:
Point
,
p2
:
Point
)
->
None
:
"""
Create a rectangle.
:param p1: the first point spanning the rectangle
:param p2: the second point spanning the rectangle
"""
if
(
p1
.
x
==
p2
.
x
)
or
(
p1
.
y
==
p2
.
y
):
# check for non-emptiness
raise
ValueError
(
f"
{
p1
.
x
}
,
{
p1
.
y
}
,
{
p2
.
x
}
,
{
p2
.
y
}
is empty."
)
#: the bottom-left point spanning the rectangle
self
.
p1
:
Final
[
Point
]
=
Point
(
min
(
p1
.
x
,
p2
.
x
),
min
(
p1
.
y
,
p2
.
y
))
#: the top-right point spanning the rectangle
self
.
p2
:
Final
[
Point
]
=
Point
(
max
(
p1
.
x
,
p2
.
x
),
max
(
p1
.
y
,
p2
.
y
))
def
area
(
self
)
->
int
|
float
:
"""
Get the area of this rectangle.
:return: the area of this rectangle
>>> Rectangle(Point(7, 3), Point(12, 6)).area()
15
"""
return
(
self
.
p2
.
x
-
self
.
p1
.
x
)
*
(
self
.
p2
.
y
-
self
.
p1
.
y
)
def
perimeter
(
self
)
->
int
|
float
:
"""
Get the perimeter of this rectangle.
:return: the perimeter of this rectangle
>>> Rectangle(Point(10, 5), Point(4, 9)).perimeter()
20
"""
return
2
*
((
self
.
p2
.
x
-
self
.
p1
.
x
)
+
(
self
.
p2
.
y
-
self
.
p1
.
y
))
def
points
(
self
)
->
tuple
[
Point
,
Point
,
Point
,
Point
]:
"""
Get the four corner points of this rectangle.
:return: a tuple with the four corners of this rectangle
"""
return
(
self
.
p1
,
Point
(
self
.
p1
.
x
,
self
.
p2
.
y
),
self
.
p2
,
Point
(
self
.
p2
.
x
,
self
.
p1
.
y
))
Back
|
FazBrowse Home
|
New Git URL