jwo.landserf.structure
Class Segment

java.lang.Object
  extended by jwo.landserf.structure.Segment

public class Segment
extends Object

Stores a directional 2d straight line segment. Can be used for geometric queries such as line intersection.

Version:
2.3, 8th January, 2008.
Author:
Jo Wood

Field Summary
static int AHEAD
          Indicates location aligned but in front of segment.
static int BEHIND
          Indicates location aligned but behind segment.
static int INTERSECTS
          Indicates location intersecting with segment.
static int LEFT
          Indicates location to the left of segment.
static int RIGHT
          Indicates location to the right of segment.
static int SEPARATE
          Indicates two segments are unconnected.
static int UNDEFINED
          Indicates location is undefined with respect to segment.
 
Constructor Summary
Segment(float px1, float py1, float px2, float py2)
          Creates a line segment from the two given end points.
 
Method Summary
 float calcDistance(float px, float py)
          Reports the distance between the given point and this segment.
 int compare(float px, float py)
          Determines where the given point is in relation to the segment.
 int compare(Segment otherSegment)
          Determines if and where the given segment intersects with this one.
 double getA()
          Reports the 'a' coefficient of the Cartesian equation of the segment.
 double getB()
          Reports the 'b' coefficient of the Cartesian equation of the segment.
 double getC()
          Reports the 'c' coefficient of the Cartesian equation of the segment.
 float getIntersectionX()
          Reports the x coordinate of the intersection with last segment to be compared.
 float getIntersectionY()
          Reports the y coordinate of the intersection with last segment to be compared.
 float getLength()
          Reports the length of the segment.
 float getPx1()
          Reports the x coordinate of the start point of the segment.
 float getPx2()
          Reports the x coordinate of the end point of the segment.
 float getPy1()
          Reports the y coordinate of the start point of the segment.
 float getPy2()
          Reports the y coordinate of the end point of the segment.
 boolean intersectsEndpoint()
          Reports whether intersection is with one of the segment's endpoints.
 boolean isUndefined()
          Reports whether segment is undefined (endpoints identical).
 Point2D nearestPoint(float px, float py)
          Reports the nearest point on the segment to the given point.
 String toString()
          Reports the coordinates of this segment.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

RIGHT

public static final int RIGHT
Indicates location to the right of segment.

See Also:
Constant Field Values

INTERSECTS

public static final int INTERSECTS
Indicates location intersecting with segment.

See Also:
Constant Field Values

LEFT

public static final int LEFT
Indicates location to the left of segment.

See Also:
Constant Field Values

AHEAD

public static final int AHEAD
Indicates location aligned but in front of segment.

See Also:
Constant Field Values

BEHIND

public static final int BEHIND
Indicates location aligned but behind segment.

See Also:
Constant Field Values

SEPARATE

public static final int SEPARATE
Indicates two segments are unconnected.

See Also:
Constant Field Values

UNDEFINED

public static final int UNDEFINED
Indicates location is undefined with respect to segment.

See Also:
Constant Field Values
Constructor Detail

Segment

public Segment(float px1,
               float py1,
               float px2,
               float py2)
Creates a line segment from the two given end points.

Parameters:
px1 - x-coordinate of first point on segment.
py1 - y-coordinate of first point on segment.
px2 - x-coordinate of second point on segment.
py2 - y-coordinate of second point on segment.
Method Detail

compare

public int compare(Segment otherSegment)
Determines if and where the given segment intersects with this one.

Parameters:
otherSegment - Segment which which to compare.
Returns:
Either INTERSECTS if the two segments cross, SEPARATE if they do not or UNDEFINED if either segment is undefined.

compare

public int compare(float px,
                   float py)
Determines where the given point is in relation to the segment.

Parameters:
px - x-coordinate of point to compare.
py - y-coordinate of point to compare.
Returns:
Relative position of point (LEFT, RIGHT, AHEAD, BEHIND, INTERSECTS or UNDEFINED).

calcDistance

public float calcDistance(float px,
                          float py)
Reports the distance between the given point and this segment.

Parameters:
px - x coordinate of point to consider.
py - y coordinate of point to consider.
Returns:
distance between point and segment.

nearestPoint

public Point2D nearestPoint(float px,
                            float py)
Reports the nearest point on the segment to the given point. If shortest distance between point and line of infinite length through the segment is outside the segment's bounds, the nearest segment endpoint is returned.

Parameters:
px - x coordinate of point to consider.
py - y coordinate of point to consider.
Returns:
Location of nearest point on segment.

intersectsEndpoint

public boolean intersectsEndpoint()
Reports whether intersection is with one of the segment's endpoints.

Returns:
true if intersection is with one of the endpoints.

getPx1

public float getPx1()
Reports the x coordinate of the start point of the segment.

Returns:
x coordinate of the start point.

getPy1

public float getPy1()
Reports the y coordinate of the start point of the segment.

Returns:
y coordinate of the start point.

getPx2

public float getPx2()
Reports the x coordinate of the end point of the segment.

Returns:
x coordinate of the end point.

getPy2

public float getPy2()
Reports the y coordinate of the end point of the segment.

Returns:
y coordinate of the end point.

isUndefined

public boolean isUndefined()
Reports whether segment is undefined (endpoints identical).

Returns:
True if undefined line.

getA

public double getA()
Reports the 'a' coefficient of the Cartesian equation of the segment. Cartesian equation is in the form aX + bY + c = 0.

Returns:
a coefficient.

getB

public double getB()
Reports the 'b' coefficient of the Cartesian equation of the segment. Cartesian equation is in the form aX + bY + c = 0.

Returns:
b coefficient.

getC

public double getC()
Reports the 'c' coefficient of the Cartesian equation of the segment. Cartesian equation is in the form aX + bY + c = 0.

Returns:
c coefficient.

getIntersectionX

public float getIntersectionX()
Reports the x coordinate of the intersection with last segment to be compared.

Returns:
x coordinate of the intersection.

getIntersectionY

public float getIntersectionY()
Reports the y coordinate of the intersection with last segment to be compared.

Returns:
y coordinate of the intersection.

getLength

public float getLength()
Reports the length of the segment.

Returns:
Length of the segment.

toString

public String toString()
Reports the coordinates of this segment.

Overrides:
toString in class Object
Returns:
Textual representation of this segment.


Copyright Jo Wood, 1996-2009, last modified, 17th April, 2009