import math
class Ball: # class ball
x = 0#data members
y = 0#data members
pos = 0#data members
xvel = 0#data members
yvel = 0#data members
vel = 0#data members
d = 0#data members
def __init__(self, x, y, d):#constructor
self.x = x
self.y = y
self.pos = math.sqrt(self.x**2+self.y**2)#pythagoras
self.d = d
self.xvel = math.cos(self.d)
self.yvel = math.sin(self.d)
self.vel = math.sqrt(self.x**2+self.y**2) # pythagoras
def update_ball(ball, boxWidth, boxHeight):#update_ball
function
temp = ball
xvel = 1
yvel = temp.yvel/temp.xvel
while temp.x != boxWidth and temp.y != boxHeight:
temp.x += xvel
temp.y += yvel
temp.d -= math.pi
temp.d %= 2*math.pi
temp.xvel = math.cos(ball.d)
temp.yvel = math.sin(ball.d)
ball.pos = math.sqrt(ball.x**2+ball.y**2)
vel = math.sqrt(ball.x**2+ball.y**2)
return temp
def plot_bounces(init_ball, numbounces, boxWidth,
boxHeight):#plot_bounces function
temp = init_ball
for i in range(numbounces):
temp = update_ball(temp, boxWidth, boxHeight)
ball = Ball(1, 2, 0)
print(int(ball.x))
print(int(ball.y))
print(int(ball.xvel))
print(int(ball.yvel))
ball = update_ball(ball, 2, 4)
print(int(ball.x))
print(int(ball.y))
print(int(ball.xvel))
print(int(ball.yvel))
print(ball.d)



OUTPUT

COMMENT DOWN FOR ANY QUERY
PLEASE GIVE A THUMBS UP
: Ball init 1.py import math 1 2 3 4 class Ball1: # class ball X = 0#data members 5 y = 0#data members 6 pos 0#data members xvel 0#data members yvel 0# data members vel = 0 #data members d 0#data members 7 10 11 12 def_init (self, x, y, d):#constructor 13 self.x x |14 self.y y self.pos math.sqrt(self.x 2 self.y 2) pythagoras 15 16 self.d d 17 self.xvel math.cos(self.d) self.yvel math.sin(self.d) self.vel math.sqrt(self.x2+self.y*2) #pythagoras 18 19 20 21 22 defupdate_ball(ball, boxWidth, boxHeight):#update_ball function 23 24 temp = ball xvel 1 25 yvel temp.yvel temp.xvel 26
1.py Ball init 27 while temp.x != boxWidth and temp.y- boxHeight: 28 temp.x += xvel temp.y += yvel temp.d math.pi temp.d%- 2*math.pi 29 30 31 temp.xvel math.cos(ball.d) temp.yvel math.sin(ball.d) ball.pos math.sqrt(ball.x 2+bally* 2) 32 33 34 vel math.sqrt(ball.x 2+ball.y 2) return temp 35 36 37 38 def plot_bounces(init_ball, numbounces, boxWidth, boxHeight):#plot_bounces function 39 40 init ball temp for i in range(numbounces): 41 update_ball(temp, boxWidth, boxHeight) 42 temp 43 44 ball = Ball(1, 2, 0) 45 46 print(int(ball.x)) print(int(ball.y) print(int(ball.xvel)) print(int(ball.yvel) ball update_ball(ball, 2, 4) print(int(ball.x)) 47 48 49 50 51 print(int(ball.y)) 52
Ball(1, 2, 0) ball 45 46 print(int(ball.x)) print(int(ball.y)) 47 print(int(ball.xvel)) print(int(ball.yvel) |ball- update_ball(ball, 2, 4) print(int(ball.x)) 52 print(int(ball.y)) 53 print(int(ball.xvel)) 54 print(int(ball.yvel)) print(ball.d) 48 49 50 51 55 56
PS E:\fixer> python .\1.py 1 2 1 0 $2 -1 0 3.141592653589793 HNHONN I