def test_vector_normalize(self): v1 = Vector(6, 8) normalize_v1 = v1.normalize() print(normalize_v1) # why get 0.600xxxx 0.800xxxx self.assertTrue(v1 == normalize_v1 * 10)
def test_mul_dot_vector(self): v1 = Vector(2, 2) v2 = Vector(1, 3) self.assertEqual(Vector(4, 4), v1 * 2) # really, OMG! swig rocks self.assertEqual(8, v1 * v2) self.assertEqual(8, v1.dot(v2))
def isBeClicked(self, x, y): l = (self.boundCircle.pos - Vector(x, y)).length() if l < self.boundCircle.radius: # be clicked, set v to zero self.velocity = Vector(0, 0) self.boundCircle.pos.point = (x, y) self.setUnMovable() return True return False
def test_isCollision(self): c1 = Circle(0, 0, 3) c2 = Circle(1, 1, 1) self.assertEqual(True, c1.isCollision(c2)) self.assertEqual(Vector(4, 4), c2.pos)
def update(self): dt = 1 if self.selectedObj: x, y = pygame.mouse.get_pos() self.selectedObj.velocity = Vector(x - self.x, y - self.y) self.x, self.y = x, y self.env.update(dt)
def forceInside(self, obj): if (obj.boundCircle.pos['x'] - obj.boundCircle.radius) < self._boundRegion.x: obj.boundCircle.pos[ 'x'] = self._boundRegion.x + obj.boundCircle.radius obj.velocity = obj.velocity.reflect(Vector(1, 0)) if (obj.boundCircle.pos['x'] + obj.boundCircle.radius) > self._boundRegion.width: obj.boundCircle.pos[ 'x'] = self._boundRegion.width - obj.boundCircle.radius obj.velocity = obj.velocity.reflect(Vector(1, 0)) if (obj.boundCircle.pos['y'] - obj.boundCircle.radius) < self._boundRegion.y: obj.boundCircle.pos[ 'y'] = self._boundRegion.y + obj.boundCircle.radius obj.velocity = obj.velocity.reflect(Vector(0, 1)) if (obj.boundCircle.pos['y'] + obj.boundCircle.radius) > self._boundRegion.height: obj.boundCircle.pos[ 'y'] = self._boundRegion.height - obj.boundCircle.radius obj.velocity = obj.velocity.reflect(Vector(0, 1))
def test_getitem_vector(self): v = Vector(2, 3) self.assertEqual(2, v['x'])
def test_declare_vector(self): v1 = Vector(2, 3, 4) self.assertEqual(v1.x, 2) self.assertEqual(v1.y, 3) self.assertEqual(v1.z, 4)
def test_vector_reflect(self): v1 = Vector(1, 1) n = Vector(0, 1) self.assertEqual(Vector(1, -1), v1.reflect(n))
def test_add_vector(self): v1 = Vector(2, 3) v2 = Vector(3, 4) v3 = Vector(5, 7) self.assertEqual(v1 + v2, v3)
def test_vector_length(self): v1 = Vector(3, 4) self.assertEqual(v1.length(), 5)
def test_sub_vector(self): v1 = Vector(2, 3) v2 = Vector(3, 4) v3 = Vector(5, 7) self.assertEqual(v1, v3 - v2)
def test_momentum(self): p = Particle(3, 4, 3, 4, 15) self.assertEqual(p.momentum, Vector(3, 4))
class Particle(object): def __init__(self, x=0, y=0, vx=0.0, vy=0.0, size=0, mass=1): self.velocity = Vector(vx, vy) self.mass = mass self.boundCircle = Circle(x, y, size) def setMovable(self): self.mass -= 1000000 def setUnMovable(self): self.mass += 1000000 @property def momentum(self): return self.mass * self.velocity def isBeClicked(self, x, y): l = (self.boundCircle.pos - Vector(x, y)).length() if l < self.boundCircle.radius: # be clicked, set v to zero self.velocity = Vector(0, 0) self.boundCircle.pos.point = (x, y) self.setUnMovable() return True return False def calKinetic(self): return 0.5 * self.mass * (self.velocity.length() ** 2) def collision(self, obj): ''' 2d collision -- split in two part first -- v parallel to the line connected the two circle center second -- v vertical to the line connected the two circle center inelastic case, both will has the same velocity.... v1' = (m1-m2)v1/(m1+m2) + 2 m2v2/(m1+m2) v2' = (2m1)v1/(m1+m2) + (m2-m1)v2/(m1+m2) ''' if self.boundCircle.isCollision(obj.boundCircle): # compare two circle p1 = obj.boundCircle.pos - self.boundCircle.pos v1, v2 = self.velocity, obj.velocity v1L, v2L = self.velocity.length(), obj.velocity.length() try: rad1 = math.acos(v1.dot(p1) / (p1.length() * v1L)) except: rad1 = 0 try: rad2 = math.acos(v2.dot(-p1) / (p1.length() * v2L)) except: rad2 = 0 v1 = p1.normalize() * v1L * math.cos(rad1) v2 = -p1.normalize() * v2L * math.cos(rad2) m1, m2 = self.mass, obj.mass v1f = (m1 - m2) * v1 / (m1 + m2) + 2 * m2 * v2 / (m1 + m2) v2f = 2 * m1 * v1 / (m1 + m2) + (m2 - m1) * v2 / (m1 + m2) self.velocity = v1f + (self.velocity - v1) obj.velocity = v2f + (obj.velocity - v2) def update(self, dt): # self.velocity*dt = Vector(dx,dy) self.boundCircle.pos += (self.velocity * dt)
def __init__(self, x=0, y=0, vx=0.0, vy=0.0, size=0, mass=1): self.velocity = Vector(vx, vy) self.mass = mass self.boundCircle = Circle(x, y, size)
def test_div_vector(self): v1 = Vector(5, 5) v2 = v1 / 3 self.assertEqual(v1 / 3, v2)
def test_vector_neg(self): v1 = Vector(1, 1) self.assertEqual(Vector(-1, -1), -v1)
class Particle(object): def __init__(self, x=0, y=0, vx=0.0, vy=0.0, size=0, mass=1): self.velocity = Vector(vx, vy) self.mass = mass self.boundCircle = Circle(x, y, size) def setMovable(self): self.mass -= 1000000 def setUnMovable(self): self.mass += 1000000 @property def momentum(self): return self.mass * self.velocity def isBeClicked(self, x, y): l = (self.boundCircle.pos - Vector(x, y)).length() if l < self.boundCircle.radius: # be clicked, set v to zero self.velocity = Vector(0, 0) self.boundCircle.pos.point = (x, y) self.setUnMovable() return True return False def calKinetic(self): return 0.5 * self.mass * (self.velocity.length()**2) def collision(self, obj): ''' 2d collision -- split in two part first -- v parallel to the line connected the two circle center second -- v vertical to the line connected the two circle center inelastic case, both will has the same velocity.... v1' = (m1-m2)v1/(m1+m2) + 2 m2v2/(m1+m2) v2' = (2m1)v1/(m1+m2) + (m2-m1)v2/(m1+m2) ''' if self.boundCircle.isCollision(obj.boundCircle): # compare two circle p1 = obj.boundCircle.pos - self.boundCircle.pos v1, v2 = self.velocity, obj.velocity v1L, v2L = self.velocity.length(), obj.velocity.length() try: rad1 = math.acos(v1.dot(p1) / (p1.length() * v1L)) except: rad1 = 0 try: rad2 = math.acos(v2.dot(-p1) / (p1.length() * v2L)) except: rad2 = 0 v1 = p1.normalize() * v1L * math.cos(rad1) v2 = -p1.normalize() * v2L * math.cos(rad2) m1, m2 = self.mass, obj.mass v1f = (m1 - m2) * v1 / (m1 + m2) + 2 * m2 * v2 / (m1 + m2) v2f = 2 * m1 * v1 / (m1 + m2) + (m2 - m1) * v2 / (m1 + m2) self.velocity = v1f + (self.velocity - v1) obj.velocity = v2f + (obj.velocity - v2) def update(self, dt): # self.velocity*dt = Vector(dx,dy) self.boundCircle.pos += (self.velocity * dt)
def test_setitem_vector(self): v = Vector(0, 3) v['x'] = 5 self.assertEqual(5, v['x'])
def test_call_vactor(self): v = Vector(5.3, 2.2) # call will return int value self.assertEqual(5, v()[0]) self.assertEqual(2, v()[1])
def test_create_Circle(self): c = Circle(2, 2, 3) self.assertEqual(Vector(2, 2), c.pos) self.assertEqual(3, c.radius)