-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbounce.py
More file actions
79 lines (66 loc) · 1.95 KB
/
Copy pathbounce.py
File metadata and controls
79 lines (66 loc) · 1.95 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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# -*- coding: utf-8 -*-
"""
Created on Mon Apr 29 13:47:34 2019
@author: wahaj
5"""
import math
class Box:
def __init__(self, mass, width, velocity, x):
self.m = mass
self.w = width
self.v = velocity
self.x = x
def checkCollision (self, other):
if ((self.x + self.w) >= other.x):
return True
else:
return False
def checkWallCollision (self):
if (self.x <= 0):
return True
else:
return False
def updatePosition (self):
self.x += self.v
# print (self.x)
def updateVelocity (self, other):
newv1 = ((self.m - other.m)*self.v + (2*other.m)*other.v)/(self.m+other.m)
newv2 = ((2*self.m)*self.v + (other.m - self.m)*other.v)/(self.m+other.m)
self.v = newv1
other.v = newv2
# print("v", self.v)
# print("v2", other.v)
def reverseVelocity (self):
self.v *= -1
def checkIfFinalCollision (self, other):
# print ("v1 {} v2 {}".format(self.v, other.v))
if self.v >= 0:
if other.v > self.v:
return True
else:
return False
else:
return False
counter = 0
digits = int (input ("Enter Number of Digits: "))
timesteps = 100
a = Box(1, 20, 0, 100)
mb = 100 ** (digits-1)
b = Box((mb), 100, (-5/timesteps), 200)
while (True):
a.updatePosition()
b.updatePosition()
if (a.checkCollision(b)):
a.updateVelocity(b)
counter += 1
if (a.checkIfFinalCollision(b)):
break
if (a.checkWallCollision()):
a.reverseVelocity()
counter += 1
if (a.checkIfFinalCollision(b)):
break
# print (counter)
print ("collisions", counter)
piestimate = counter/(10**(digits-1))
print ("pi is approximately equal to {}".format(piestimate))