Java Reference
In-Depth Information
SOLUTION:
Program 11.5
The centroid of an
Atom
object
public static
Point3D middle ( Atom [ ]
t )
{
Point3D middle = t [ 0 ] . center ;
for
(
int
i=1; i
<
t. length ; ++i)
middle = Point3D . add (middle , t [ i ] . center ) ;
Point3D . scale (middle , 1.0/ t . length ) ;
return
middle ;
}
-
Write a static function
double
maxDistance(Point3D p, Atom a)
that
returns the maximal distance between point
p
and any point on the
sphere of atom
a
. This function shall be attached inside class
Atom
.
SOLUTION:
public static double
maxDistance ( Point3D p, Atom a)
{
return
Point3D . distance (p, a. center ) + a. radius
;
}
-
Describe now how to modify class
Molecule
for building its enclosing
sphere and using it for checking for collisions. Note that if we do not
intersect the enclosing ball of a molecule, it is not necessary to check
for collisions of its atoms.
Write a static function
boolean
bump(Atom a, Molecule b)
of class
Molecule
that allows us to check whether atom
a
is colliding with at
least one of the atoms of molecule
b
or not.
Write a static function
boolean
bump(Molecule a, Molecule b)
that
extends this test to two molecules.
SOLUTION:
Program 11.6
The
Molecule
class equipped with the
bump
predicate
public class
Molecule
{
Atom [ ] atoms ;
Atom sphere ;
public
Molecule ( Atom []
t)
{
this
. atoms = t ;
Point3D center = Atom . middle ( atoms ) ;
double
r=0;
for
(
int
i=0; i
<
atoms .
{
length
; ++i )
double
ri = Atom . maxDistance ( center
, atoms [ i ]) ;
if
(r
<
ri) r = ri ;
Search WWH ::
Custom Search