Java Reference
In-Depth Information
distance(double x1, double y1,
double x2, double y2)
This is a static version of the method that calculates
the distance between the points x1,y1 and x2,y2.
distance(double xNext,
double yNext)
Calculates the distance from the current point (the
object for which the method is called) and the point
xNext , yNext .
distance(Point2D nextPoint)
Calculates the distance from the current point to the
point, nextPoint . The argument can be any of the
subclass types, Point , Point2D.Float or
Point2D.Double .
Here's how you might calculate the distance between two points:
Point2D.Double p1 = new Point2D.Double(2.5, 3.5);
Point p2 = new Point(20, 30);
double lineLength = p1.distance(p2);
You could also have calculated this distance without creating the points by using the static
method:
double lineLength = Point2D.distance(2.5, 3.5, 20, 30);
Corresponding to each of the three distance() methods there is a convenience method,
distanceSq() , with the same parameter list that returns the square of the distance as type
double .
3.
Comparing points:
The equals() method compares the current point with the point object referenced by the
argument and returns true if they are equal and false otherwise.
4. Setting a new location for a point:
The inherited setLocation() method comes in two versions. One accepts an argument that
is a reference of type Point2D , and sets the coordinate values of the current point to those of
the point passed as an argument. The other accepts two arguments of type double that are the
x and y coordinates of the new location. The Point class also defines a version of
setLocation() that accepts two arguments of type int to define the new coordinates.
Search WWH ::




Custom Search