/** * A line in 2-dimensional space. A line is uniquely determined * by 2 points in space. * * Illustrates: * - A class whose instance variables themselves have class type * - Multipe constructors * */ public class Line{ private Point p1; private Point p2; private double slope; /** * Represents the line that passes through the specified points */ public Line(Point point1, Point point2){ p1 = point1; p2 = point2; slope = (p1.getY() - p2.getY()) / (p1.getX() - p2.getX()); } /** * Represents the line that passes through the specified points */ public Line(double x1, double y1, double x2, double y2){ p1 = new Point(x1, y1); p2 = new Point(x2, y2); slope = (p1.getY() - p2.getY()) / (p1.getX() - p2.getX()); } /** * Returns back the slope of the line */ public double getSlope(){ return slope; } /** * Uses the following formula to compute the y-coordinate for a given x-coordinate: * * y - y1 = m * (x - x1) * * or, equivalently, * * y = m * (x - x1) + y1 * * where * * x is the x-coordinate passed in * and * (x1,y1) is one of the two points on the line * */ public double computeY(double x){ double y = slope*(x - p1.getX()) + p1.getY(); return y; } }