com.partnersoft.geometry.xy
Class XyLine

java.lang.Object
  extended by com.partnersoft.geometry.xy.XyLine
All Implemented Interfaces:
Immutable

public class XyLine
extends java.lang.Object
implements Immutable

A geometric, infinite line in (x, y) space.

This is a "real" line, not a segment. It is infinite, defined by a vector and y-intercept. Vectors are used instead of slopes since vertical lines have undefined slopes.

In order to help prevent weird errors in client code, IllegalStateException is thrown by getSlope(), getXIntercept(), and getYIntercept(). These values may be undefined if the line is horizontal or vertical, and your code should not call them unless you first test isHorizontal() and isVertical().

Copyright 2007 Partner Software, Inc.

Version:
$Id: XyLine.java 2474 2010-03-13 14:28:43Z paul $
Author:
Paul Reavis, Rich Stepanski, Richard Fay

Method Summary
static XyLine forPoints(XyPoint p1, XyPoint p2)
          Returns the line going through the given points.
static XyLine forSlopeIntercept(double slope, double intercept)
          Returns a line for the given slope and y-intercept.
static XyLine forVector(XyVector vector)
          Returns the line going through the origin with the given vector direction.
static XyLine forX(double x)
          Returns a vertical line for the given x value.
static XyLine forY(double y)
          Returns a horizontal line for the given y value.
 double getSlope()
          The slope (usually annotated "m").
 double getXIntercept()
          The x-intercept.
 double getYIntercept()
          The y-intercept (usually annotated "b").
 XyPoint intersectionWith(XyLine nother)
          Find the intersection of the two lines.
 boolean isHorizontal()
          Returns true if the line is horizontal (no rise, all run).
 boolean isVertical()
          Returns true if the line is vertical (all rise, no run).
 double solveForX(double y)
          Solve for x, given y.
 double solveForY(double x)
          Solve for y, given x.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

forX

public static XyLine forX(double x)
Returns a vertical line for the given x value.


forY

public static XyLine forY(double y)
Returns a horizontal line for the given y value.


forSlopeIntercept

public static XyLine forSlopeIntercept(double slope,
                                       double intercept)
Returns a line for the given slope and y-intercept.

Parameters:
slope -
intercept -
Returns:

forPoints

public static XyLine forPoints(XyPoint p1,
                               XyPoint p2)
Returns the line going through the given points.

Parameters:
p1 -
p2 -
Returns:

forVector

public static XyLine forVector(XyVector vector)
Returns the line going through the origin with the given vector direction.

Parameters:
vector -
Returns:

solveForX

public double solveForX(double y)
Solve for x, given y. Returns 0 if it's a horizontal line.


solveForY

public double solveForY(double x)
Solve for y, given x. Returns 0 if it's a vertical line.


intersectionWith

public XyPoint intersectionWith(XyLine nother)
Find the intersection of the two lines. Returns null if they are parallel.


isHorizontal

public boolean isHorizontal()
Returns true if the line is horizontal (no rise, all run). If this is true then the slope will be zero.


isVertical

public boolean isVertical()
Returns true if the line is vertical (all rise, no run). If this is true then the slope will be infinite.


getSlope

public double getSlope()
The slope (usually annotated "m"). If the line is horizontal, slope is zero. If it is vertical, then slope will be infinite.


getYIntercept

public double getYIntercept()
The y-intercept (usually annotated "b"). This defines the line value of y for horizontal lines. It is undefined for vertical lines.


getXIntercept

public double getXIntercept()
The x-intercept. This defines the line value of x for vertical lines.