Java Reference
In-Depth Information
start = new Point(xStart, yStart);
// Create the start point
end = new Point(xEnd, yEnd);
// Create the end point
}
// Calculate the length of a line
public double length() {
return start.distance(end);
// Use the method from the
Point class
}
// Return a point as the intersection of two lines -- called from a
Line object
public Point intersects(final Line line1) {
Point localPoint = new Point(0, 0);
double num = (end.getY() - start.getY()) *
(start.getX()-line1.start.getX())
- (end.getX() - start.getX()) * (start.getY() -
line1.start.getY());
double denom = (end.getY() - start.getY()) * (line1.end.getX() -
line1.start.getX())
- (end.getX() - start.getX()) * (line1.end.getY() -
line1.start.getY());
localPoint.setX(line1.start.getX() + (line1.end.getX() -
line1.start.getX())*num/
denom);
localPoint.setY(line1.start.getY() + (line1.end.getY() -
line1.start.getY())*num/
denom);
return localPoint;
}
// Convert a line to a string
public String toString() {
return "(" + start+ "):(" + end + ")"; // As "(start):(end)"
} // that is, "(x1,
y1):(x2, y2)"
// Data members
Point start; // Start point of line
Point end; // End point of line
}
Directory "Geometry"
Here you have left the data members of the class without an access attribute so they are accessible from
the Point class, but not from classes outside the Geometry package.
Search WWH ::




Custom Search