-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path1_point_class.py
120 lines (87 loc) · 3.19 KB
/
1_point_class.py
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
import math
class Point:
"""Create a coordinate point with values x and y on a 2D plane."""
def __init__(self, x, y):
"""Constructor of a point object with x, y coordinates.
Args:
x (float): value at horizontal axis
y (float): value at vertical axis
"""
self.x = x
self.y = y
def __str__(self):
return f"{self.x, self.y}"
def get_location(self):
return f"x: {self.x}\ny: {self.y}"
def move_x(self, a):
"""Move the point P in x coordinate.
Args:
a (float): distance to be moved in x direction
Returns:
location(x + a, y): The new location of the point
"""
print(f"Adding {a} to x: {self.x}")
return f"{self.x + a, self.y}"
def move_y(self, b):
"""Move the point P in y coordinate.
Args:
b (float): distance to be moved in y direction
Returns:
location(x, y + b): The new location of the point
"""
print(f"Adding {b} to y: {self.y}")
return f"{self.x, self.y + b}"
def distance_from_origin(self):
"""The distance of the point from the origin (0, 0)
Returns:
float: the distance from origin in 2D coordinate system.
"""
# Using dist() method of math module
# print(math.dist((self.x, self.y), (0, 0)))
# Or Using in-built pow() function
# print(pow((self.x ** 2) + (self.y ** 2), .5))s
return ((self.x ** 2) + (self.y ** 2)) ** .5
def distance_from_another_point(self, other):
"""The distance of the point p1 from another point p2.
Args:
other (point object): The second point object(x2, y2) in coordinate system
Returns:
float: The distance of the point p1(x1, y1) from point p2(x2, y2) in 2D coordinate system.
"""
return math.dist((self.x, self.y), (other.x, other.y))
def __add__(self, other):
"""Add the point p1(x1, y1) with point p2(x2, y2)
Args:
other (point object): The point object with coordinates(x2, y2)
Returns:
tuple: The new point after addition.
"""
return (self.x + other.x, self.y + other.y)
def cartesian_to_polar(self):
"""Convert cartesian coordinate(x, y) system to polar(radius, theta) system
Returns:
tuple(float, float): the radius and theta of the point from origin.
"""
radius = math.sqrt(self.x**2 + self.y**2)
theta = math.degrees(math.atan(self.y/self.x))
return radius, theta
# Instantiating the point class with (x=2, y=3)
p1 = Point(2, 2)
# Instantiating the point class with (x=5, y=6)
p2 = Point(5, 6)
# The str representation of the points (__str__ method)
print(p1)
print(p2)
# Calling 'get_location' method
print(p1.get_location())
print(p2.get_location())
# Move point p1, 5 points in x-direction
print(p1.move_x(5))
# Move point p2, 4 points in y-direction
print(p2.move_y(5))
# Calling 'distance_from_origin method
print(p1.distance_from_origin())
print(p1.distance_from_another_point(p2))
print(p1 + p2)
print(p1.cartesian_to_polar())
print(p2.cartesian_to_polar())