FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
programmingWithPythonCode/classes/triangle.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
/
triangle.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
56 lines (44 loc) · 1.66 KB
Breadcrumbs
programmingWithPythonCode
/
classes
/
triangle.py
Copy path
File metadata and controls
56 lines (44 loc) · 1.66 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
"""
A class for triangles.
>>> Triangle(Point(22, 1), Point(4, 12), Point(6, 3)).print()
(22, 1), (4, 12), (6, 3)
>>> Triangle(Point(0, 0), Point(0, 3), Point(4, 0)).perimeter()
12.0
"""
from
typing
import
Final
from
point
import
Point
from
polygon
import
Polygon
class
Triangle
(
Polygon
):
"""The class for triangles."""
def
__init__
(
self
,
p1
:
Point
,
p2
:
Point
,
p3
:
Point
)
->
None
:
"""
Create a triangle.
:param p1: the first point spanning the triangle
:param p2: the second point spanning the triangle
:param p3: the third point spanning the triangle
"""
if
(
p1
.
distance
(
p2
)
<=
0
)
or
(
p2
.
distance
(
p3
)
<=
0
)
or
(
p3
.
distance
(
p1
)
<=
0
):
# check for non-emptiness
raise
ValueError
(
"empty triangle"
)
#: the first point spanning the triangle
self
.
p1
:
Final
[
Point
]
=
p1
#: the second point spanning the triangle
self
.
p2
:
Final
[
Point
]
=
p2
#: the third point spanning the triangle
self
.
p3
:
Final
[
Point
]
=
p3
def
area
(
self
)
->
int
|
float
:
"""
Get the area of this triangle.
:return: the area of this triangle
>>> Triangle(Point(-1, 2), Point(2, 3), Point(4, -3)).area()
10.0
"""
return
0.5
*
abs
(
self
.
p1
.
x
*
(
self
.
p2
.
y
-
self
.
p3
.
y
)
+
self
.
p2
.
x
*
(
self
.
p3
.
y
-
self
.
p1
.
y
)
+
self
.
p3
.
x
*
(
self
.
p1
.
y
-
self
.
p2
.
y
))
def
points
(
self
)
->
tuple
[
Point
,
Point
,
Point
]:
"""
Get the three points describing this triangle.
:return: a tuple with the three corners of this triangle
"""
return
self
.
p1
,
self
.
p2
,
self
.
p3
Back
|
FazBrowse Home
|
New Git URL