double[] x = {1, 3, 5, 7}; double[] y = {8, 6, 2, 2};Think of the x array as representing x coordinates, and the y array as representing y coordinates. Thus, x[i],y[i] are the coordinates of the i-th point among four points.
Write two methods, with the following signatures
static double distance (double x1, double y1, double x2, double y2)
{
// This method should return the straight-line
// distance between the two points (x1,y1) and (x2,y2).
}
static double minDistance (double[] x, double[] y)
{
// This method should return the distance
// between the two points that are closest.
}
Then, in
main, write code
to find the distance between the two closest points
among the four in the arrays above. The output should
be 2.0
in this case, since the two closest points are
(5,2) and (7,2).
Call your program
MinimumDistance.java.
Test your program with longer arrays (more points).
To do this, it will help to write a method that
will generate points randomly using
Math.random()
as shown in Module 12. Write a method called
makeRandomCoordinates
so that it can be used as follows:
int n = 10; // Desired number of points.
double[] x = makeRandomCoords (n);
double[] y = makeRandomCoords (n);
Report the average that you get both for n=10 points
and for n=20 points, based on 1000 repetitions.
To display points moving randomly,
use the animation feature in
DrawTool.java.
(NOTE: please download the latest version.) For example,
here is a simple animation that moves a point back
and forth between two locations 10 times:
DrawTool.display ();
double x=5, y=5;
int T = 10;
DrawTool.startAnimationMode ();
double sign = -1;
for (int t=1; t<=T; t++) {
x += sign * 2;
y += sign * 2;
DrawTool.drawPoint (x,y);
DrawTool.animationPause (500);
sign = -sign;
}
DrawTool.endAnimationMode ();
How do you move a point (think of a point as representing a particle) randomly? Pick a random angle θ in the range [0,2π], and a distance r. This defines a new location. Then, you can compute the coordinates of the new location using simple trigonometry:
In your program:
Here, the gap is 4 units wide. With a barrier, a particle can cross over from one side to another only by going through the gap.
Write a program called BarrierDiffusion.java in which you simulate diffusion as before but now do not allow particles to cross through the barrier wall. That is, the only way to the other side is through the gap.
Write a method called
isValidMove with
the following signature:
static boolean isValidMove (double x1, double y1, double x2, double y2)
{
// This method should return true if a particle
// at (x1,y1) is allowed to move to (x2,y2).
}
Then, use this to determine whether a move is valid when you
generate a random move. Of course, inside this method is where
you will check whether the move is "legal" (within the bounds
of the region and does not go through the barrier walls).
How does the barrier gap affect the time for diffusion? Try gaps of size 2, 3, 4, 5, and 6, and plot a graph of the time needed to achieve disparity less than 20%. Once again, for this question, you probably want to comment out the drawing to speed up the execution.
Note: you will probably find the barrier-checking a little challenging. Initially, try a simple approximation just to get your program working, then work on refining your program.
Recommended, but not required:
This week, we'll explore software errors.