/**
 * An immutable line segment.
 *
 * @author Drue Coles
 */
public class LineSegment2 implements Comparable {

    // coordinates of endpoints
    private final double x1;
    private final double y1;
    private final double x2;
    private final double y2;

    /**
     * Creates a new line segment with endpoints (x1, y1) and (x2, y2).
     */
    public LineSegment2(double x1, double y1, double x2, double y2) {
        this.x1 = x1;
        this.y1 = y1;
        this.x2 = x2;
        this.y2 = y2;
    }

    /**
     * Returns the length of this line segment.
     */
    public double length() {
        double dx = x2 - x1;
        double dy = y2 - y1;
        return Math.sqrt(dx * dx + dy * dy);
    }

    /**
     * Returns the slope of this line segment.
     */
    public double slope() {
        return (y2 - y1) / (x2 - x1);
    }

    /**
     * Returns a negative integer, zero, or a positive integer depending on whether the
     * length of this line segment is less than, equal to, or greater than the length of
     * given line segment.
     */
    @Override
    public int compareTo(Object o) {
        LineSegment2 other = (LineSegment2) o;
        return Double.compare(length(), other.length());
    }

    /**
     * Returns a description of this line segment.
     */
    @Override
    public String toString() {
        String p1 = String.format("(%.2f, %.2f)", x1, y1); // formatted endpoint
        String p2 = String.format("(%.2f, %.2f)", x2, y2); // formatted endpoint
        String length = String.format("%.2f", length()); // formatted length
        String slope = String.format("%.2f", slope()); // formatted slope
        return String.format("[%s; %s; length = %s; slope = %s]", p1, p2, length, slope);
    }
}