Ricocheting Bullet, continued
/* == the x corresponding to y and th. */
static double x( double y, double th ) {
return (y / Math.tan( th )) ;
/* == the smallest even y > 0 for which the fractional part of the corresponding x is not larger than d.*/
static double min_y( double d, double th ) {
int y = 2;
while ( (x(y,th) - Math.floor(x(y,th))) > d )
y = y + 2;
return y;
static double sqr( double x ) { return x * x; }
/* == distance traveled by bullet. */
static double distance( double d, double th )
{
double y = min_y(d, th );
return Math.sqrt( sqr(x(y,th)) + sqr(y));