-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMyVec2D.py
More file actions
60 lines (57 loc) · 1.79 KB
/
MyVec2D.py
File metadata and controls
60 lines (57 loc) · 1.79 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
class Vec2D:
def __init__(self,x,y):
self.x = x; self.y = y
def __add__(self,other):
return Vec2D(self.x+other.x,self.y+other.y)
def __sub__(self,other):
return Vec2D(self.x-other.x,self.y-other.y)
def __neg__(self):
return Vec2D(-self.x,-self.y)
def __mul__(self,other):
if type(other) in [int, float]:
return Vec2D(self.x*other,self.y*other)
elif type(other) is Vec2D:
return self.x*other.x + self.y*other.y
def __truediv__(self,other):
if type(other) in [int, float]:
return Vec2D(self.x/other,self.y/other)
def __rmul__(self,other):
return self.__mul__(other)
def __eq__(self,other):
return self.x==other.x and self.y==other.y
def __ne__(self,other):
return not(self==other)
def __round__(self,n=0):
return Vec2D(round(self.x, n), round(self.y, n))
def __str__(self):
return f'Vec2D({self.x}, {self.y})'
def __repr__(self):
return f'Vec2D({self.x}, {self.y})'
def tup(self):
return self.x,self.y
def mag(self):
return (self.x*self.x + self.y*self.y)**0.5
def mag2(self):
return self.x*self.x + self.y*self.y
def slope(self):
return self.y/self.x
def unit(self):
m = self.mag()
return Vec2D(self.x/m, self.y/m)
class Ball:
def __init__(self,posx,posy,velx,vely,radius=10,color=None,mass=1):
self.vel = Vec2D(velx,vely)
self.pos = Vec2D(posx,posy)
self.mass = mass
if not color: color = tuple(random.randrange(50,255) for x in range(3))
self.color = color
self.radius = radius
self.density = self.mass / (self.radius*self.radius)
x = self.pos_tup()
self.l20pos = [(int(x[0]),int(x[1]))]
def __str__(self):
return f'Ball(r={self.radius},m={self.mass})'
def __repr__(self):
return f'Ball(r={self.radius},m={self.mass})'
def pos_tup(self):
return (self.pos + V(640,376.5)).tup()