Package edu.princeton.cs.algs4
Class Particle
 Object

 edu.princeton.cs.algs4.Particle

public class Particle extends Object
TheParticle
class represents a particle moving in the unit box, with a given position, velocity, radius, and mass. Methods are provided for moving the particle and for predicting and resolvling elastic collisions with vertical walls, horizontal walls, and other particles. This data type is mutable because the position and velocity change.For additional documentation, see Section 6.1 of Algorithms, 4th Edition by Robert Sedgewick and Kevin Wayne.
 Author:
 Robert Sedgewick, Kevin Wayne


Constructor Summary
Constructors Constructor Description Particle()
Initializes a particle with a random position and velocity.Particle(double rx, double ry, double vx, double vy, double radius, double mass, Color color)
Initializes a particle with the specified position, velocity, radius, mass, and color.

Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
bounceOff(Particle that)
Updates the velocities of this particle and the specified particle according to the laws of elastic collision.void
bounceOffHorizontalWall()
Updates the velocity of this particle upon collision with a horizontal wall (by reflecting the velocity in the ydirection).void
bounceOffVerticalWall()
Updates the velocity of this particle upon collision with a vertical wall (by reflecting the velocity in the xdirection).int
count()
Returns the number of collisions involving this particle with vertical walls, horizontal walls, or other particles.void
draw()
Draws this particle to standard draw.double
kineticEnergy()
Returns the kinetic energy of this particle.void
move(double dt)
Moves this particle in a straight line (based on its velocity) for the specified amount of time.double
timeToHit(Particle that)
Returns the amount of time for this particle to collide with the specified particle, assuming no interening collisions.double
timeToHitHorizontalWall()
Returns the amount of time for this particle to collide with a horizontal wall, assuming no interening collisions.double
timeToHitVerticalWall()
Returns the amount of time for this particle to collide with a vertical wall, assuming no interening collisions.



Constructor Detail

Particle
public Particle(double rx, double ry, double vx, double vy, double radius, double mass, Color color)
Initializes a particle with the specified position, velocity, radius, mass, and color. Parameters:
rx
 xcoordinate of positionry
 ycoordinate of positionvx
 xcoordinate of velocityvy
 ycoordinate of velocityradius
 the radiusmass
 the masscolor
 the color

Particle
public Particle()
Initializes a particle with a random position and velocity. The position is uniform in the unit box; the velocity in either direciton is chosen uniformly at random.


Method Detail

move
public void move(double dt)
Moves this particle in a straight line (based on its velocity) for the specified amount of time. Parameters:
dt
 the amount of time

draw
public void draw()
Draws this particle to standard draw.

count
public int count()
Returns the number of collisions involving this particle with vertical walls, horizontal walls, or other particles. This is equal to the number of calls tobounceOff(edu.princeton.cs.algs4.Particle)
,bounceOffVerticalWall()
, andbounceOffHorizontalWall()
. Returns:
 the number of collisions involving this particle with vertical walls, horizontal walls, or other particles

timeToHit
public double timeToHit(Particle that)
Returns the amount of time for this particle to collide with the specified particle, assuming no interening collisions. Parameters:
that
 the other particle Returns:
 the amount of time for this particle to collide with the specified
particle, assuming no interening collisions;
Double.POSITIVE_INFINITY
if the particles will not collide

timeToHitVerticalWall
public double timeToHitVerticalWall()
Returns the amount of time for this particle to collide with a vertical wall, assuming no interening collisions. Returns:
 the amount of time for this particle to collide with a vertical wall,
assuming no interening collisions;
Double.POSITIVE_INFINITY
if the particle will not collide with a vertical wall

timeToHitHorizontalWall
public double timeToHitHorizontalWall()
Returns the amount of time for this particle to collide with a horizontal wall, assuming no interening collisions. Returns:
 the amount of time for this particle to collide with a horizontal wall,
assuming no interening collisions;
Double.POSITIVE_INFINITY
if the particle will not collide with a horizontal wall

bounceOff
public void bounceOff(Particle that)
Updates the velocities of this particle and the specified particle according to the laws of elastic collision. Assumes that the particles are colliding at this instant. Parameters:
that
 the other particle

bounceOffVerticalWall
public void bounceOffVerticalWall()
Updates the velocity of this particle upon collision with a vertical wall (by reflecting the velocity in the xdirection). Assumes that the particle is colliding with a vertical wall at this instant.

bounceOffHorizontalWall
public void bounceOffHorizontalWall()
Updates the velocity of this particle upon collision with a horizontal wall (by reflecting the velocity in the ydirection). Assumes that the particle is colliding with a horizontal wall at this instant.

kineticEnergy
public double kineticEnergy()
Returns the kinetic energy of this particle. The kinetic energy is given by the formula 1/2 m v^{2}, where m is the mass of this particle and v is its velocity. Returns:
 the kinetic energy of this particle

