-
Notifications
You must be signed in to change notification settings - Fork 0
/
L006_Classes_Objects-points.py
57 lines (46 loc) · 1.19 KB
/
L006_Classes_Objects-points.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
# Classes & Objects
# Using Coordinates
class Point:
def __init__(self,a=0,b=0):
self.x=a
self.y=b
def translate(self,deltaX,deltaY):
self.x+=deltaX
self.y=deltaY
def Odistance(self):
import math
d=math.sqrt(self.x*self.x+self.y*self.y)
return d
def __str__(self):
return('('+str(self.x)+','+str(self.y)+')')
def __add__(self,p):
return(Point(self.x+p.x,self.y+p.y))
# a=Point(2,3)
# b=Point(5,6)
# c=a+b
# print(c)
# Polar Form
import math
class Point:
def __init__(self,a=0,b=0):
self.r=math.sqrt(a*a+b*b)
if a==0:
self.theta=math.pi/2
else:
self.theta=math.atan(b/a)
def Odistance(self):
return(self.r)
def translate(self,deltaX,deltaY):
x=self.r*math.cos(self.theta)
y=self.r*math.sin(self.theta)
x+=deltaX
y==deltaY
self.r=math.sqrt(x*x+y*y)
if x==0:
self.theta=math.pi/2
else:
self.theta=math.atan(y/x)
def __str__(self):
return('r: '+str(self.r)+'\n'+'theta: '+str(self.theta)+' radian')
# z=Point(1,2)
# print(z)