Declare in code a variable called "xposition" to store the x coordinate of the moving ball and a variable called "yposition" to store the y coordinate of the moving ball at an arbitrary point in time. Declare a variable called "xposition2" to store the x coordinate of the moving ball and a variable called "yposition2" to store the y coordinate of the moving ball at a later point in time.
Declare a variable called "deltaY" to store the change in y-coordinates of the moving ball, a variable called "deltaX" to store the change in x-coordinates of the moving ball, a variable called "slope" to store the slope of the line that the moving ball follows and a variable called "angle" to store the deflection angle of the moving ball after it hits the stationary object (the edge of the pool table).
Code the statements necessary to retrieve and store in the variables xposition, yposition, xposition2 and yposition2 at two different times.
Program the code to calculate deltaY using the equation:
deltaY = ypostiion2-yposition
Program the code to calculate deltaX using the equation:
deltaY = xpostiion2-xposition
Program the code to calculate the slope of the line of the moving ball using the equation:
Slope = deltaY/deltaX
Program the code to calculate the angle of the moving ball using the equation
Angle = arctangent(slope)
Program the code to calculate the angle of deflection using the equation.
Angle = 180 - Angle
The equation takes the value of the angle calculated with the arctangent statement and subtracts it from 180 degrees to obtain the angle of deflection (of the moving ball with reference to the edge of the pool table.).