-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathScript57_Project_05_Point_Class.py
More file actions
90 lines (70 loc) · 2.1 KB
/
Script57_Project_05_Point_Class.py
File metadata and controls
90 lines (70 loc) · 2.1 KB
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
82
83
84
85
86
87
88
89
90
# In the name of God
# Mohammad Hossein Zehtab
# python-evens-17
# Project 05: Point Class
class Point:
'''
Point class represents and manipulates x & y Coordinates.
'''
def __init__(self, x=0, y=0):
'''
Creates a new point at x & y
'''
self.x = x
self.y = y
def euclidean_distance_from_origin(self):
'''
Euclidean Distance between myself and the origin.
'''
addsq = self.x ** 2 + self.y ** 2
distfromorigin = addsq ** 0.5
return distfromorigin
def euclidean_distance(self, target):
'''
Euclidean Distance between myself and a target.
'''
addsq = (target.x - self.x) ** 2 + (target.y - self.y) ** 2
dist = addsq ** 0.5
return dist
def manhattan_distance(self, target):
'''
Manhattan Distance between myself and a target.
'''
sub1 = target.x - self.x
sub2 = target.y - self.y
dist = abs(sub1) + abs(sub2)
return dist
def halfway(self, target):
'''
Mid point between myself and a target.
'''
pointx = (target.x + self.x) / 2
pointy = (target.y + self.y) / 2
point = (pointx, pointy)
return point
def __str__(self):
'''
Shows the value of a point object.
'''
return f'({self.x}, {self.y})'
### Instantiations ###
p = Point()
p.x = 6
p.y = 8
print('p =', p)
q = Point()
q.x = 3
q.y = 7
print('q =', q)
# Euclidean distance from origin
edfo = p.euclidean_distance_from_origin()
print(f'\nEuclidean Distance between {p} and the origin is {edfo:.2f}')
# Euclidean distance between two points
ed = p.euclidean_distance(q)
print(f'\nEuclidean Distance between {p} and {q} is {ed:.2f}')
# Manhattan distance between two points
md = p.manhattan_distance(q)
print(f'\nManhattan Distance between {p} and {q} is {md:.2f}')
# Halfway between two points
hp = p.halfway(q)
print(f'\nHalfway Point between {p} and {q} is {hp}')