-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpoint.py
executable file
·61 lines (42 loc) · 1.28 KB
/
point.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
#!/usr/bin/env python
import os
import sys
class Point:
# initialization method
# it is called automatically when a class is called
# When defining a method, the first parameter refers to the instance being created. It is customary to name this parameter self.
def __init__(self, x=0, y=0):
self.x = x
self.y = y
def distance_from_origin(self):
dist = (self.x ** 2 + self.y ** 2) ** (0.5)
return dist
def __str__(self):
return 'Point: (' + str(self.x) + ', ' + str(self.y) + ')'
def __add__(self, other):
print "Adding ..."
return Point((self.x + other.x), (self.y + other.y))
def __sub__(self, other):
print "Subtracting ..."
return Point((self.x - other.x), (self.y - other.y))
def __mul__(self, other):
print "Point Multiplication ..."
return Point((self.x * other.x), (self.y * other.y))
def __rmul__(self, scale):
print "Scalar Multiplication ..."
return Point((self.x * scale), (self.y * scale))
if __name__ == '__main__':
p1 = Point(3, 4)
print p1
p2 = Point(5, 12)
print p2
p3 = p1 + p2
print p3
p4 = p3 - p1
print p4
p5 = p1 * p2
print p5
p6 = 2 * p5
print p6
p7 = p6 * 2
print p7