com.jfimagine.jfgraph.geom
Class LineSeg

java.lang.Object
  |
  +--com.jfimagine.jfgraph.geom.LineSeg
All Implemented Interfaces:
java.lang.Cloneable
Direct Known Subclasses:
LabelLine

public class LineSeg
extends java.lang.Object
implements java.lang.Cloneable

LineSeg class. A class used to represent a line in the plane with double coordinates.

Attention: Here we used a clockwise quadrant system. And the first quadrant is under right bottom corner.

Version:
$Revision: 1.1 $
Author:
CookieMaker

Constructor Summary
LineSeg()
          Constructor for LineSeg.
LineSeg(double x1, double y1, double x2, double y2)
          Constructor for LineSeg.
LineSeg(JFPoint startPoint, JFPoint endPoint)
          Constructor for LineSeg.
LineSeg(LineSeg lineSeg)
          Constructor for LineSeg.
 
Method Summary
 java.lang.Object clone()
          Creates a new object of the same class and with the same contents as this object.
 JFPoint closeTo(double px, double py)
          Returns a closest point on this line segment to specified point.
 boolean contains(double x, double y)
          Test if a point(x, y coordinates for instead) is on this line.
 boolean contains(double x, double y, double pickOffset)
          Test if a point(x, y coordinates for instead) is on this line.
 boolean contains(JFPoint pnt)
          Test if a point is on this line.
 boolean contains(JFPoint pnt, double pickOffset)
          Test if a point is on this line.
 double distance(double px, double py)
          Returns the distance from a point to this line segment.
 double distance(JFPoint pnt)
          Returns the distance from a point to this line segment.
 boolean equals(java.lang.Object obj)
          Determines whether or not two objects are equal.
 void flipBy()
          Reverse this line by a central y coordinate of this line.
 void flipBy(double baseY)
          Reverse this line by a y coordinate.
 Rect getBounds()
          Get the bounds of this line segment.
 JFPoint getCenter()
          Get a center point on this line.
 double getLength()
          Get the length of this line.
static double getLength(double x1, double y1, double x2, double y2)
          Get the length between two endpoints of a line.
static double getLength(JFPoint pnt1, JFPoint pnt2)
          Get the length between two endpoints of a line.
 JFPoint getPoint1()
          Get point1, i.e. the point at x1,y1
 JFPoint getPoint2()
          Get point2, i.e. the point at x2,y2
 double getSlope()
          Get the slope of this line.
static double getSlope(double x1, double y1, double x2, double y2)
          Get the slope of a specified line.
static double getSlope(JFPoint startPoint, JFPoint endPoint)
          Get the slope of a specified line.
static double getSlope(LineSeg line)
          Get the slope of a specified line.
 double getX1()
          Get x coordiate of first endpoint.
 double getX2()
          Get x coordiate of second endpoint.
 double getY1()
          Get y coordiate of first endpoint.
 double getY2()
          Get y coordiate of second endpoint.
 int hashCode()
          Returns the hashcode for this Object.
 boolean intersects(double x1, double y1, double x2, double y2)
          Tests if the specified line segment intersects this LineSeg.
 boolean intersects(JFPoint startPoint, JFPoint endPoint)
          Tests if the specified line segment intersects this LineSeg.
 boolean intersects(LineSeg l)
          Tests if the specified line segment intersects the this LineSeg.
 boolean intersects(Rect rect)
          Tests if the specified rectangle intersects the interior of this LineSeg.
 JFPoint intersectsAt(double x1, double y1, double x2, double y2)
          Get an intersected point both on this line and a new line.
 JFPoint intersectsAt(JFPoint startPoint, JFPoint endPoint)
          Get an intersected point both on this line and a new line.
 void mirrorBy()
          Mirror this line by a central x coordinate of this line.
 void mirrorBy(double baseX)
          Mirror this line by a x coordinate.
 void moveBy(double x, double y)
          Move this line by specific x and y coordinates.
 void rotateBy(double theta)
          Rotate this line by an angle theta.
 void rotateBy(double baseX, double baseY, double theta)
          Rotate this line by a specified point and an angle theta.
 void rotateBy(JFPoint pnt, double theta)
          Rotate this line by a specified point and an angle theta.
static boolean sameLinePoints(double x1, double y1, double x2, double y2, double x3, double y3)
          Test if three points are on same line.
static boolean sameLinePoints(JFPoint point1, JFPoint point2, JFPoint point3)
          Test if three points are on same line.
 void scaleBy(double scale)
          Scale this line by its center point, and specified scale.
 void scaleBy(double baseX, double baseY, double scale)
          Scale this line by a point, and specified scale.
 void scaleBy(double baseX, double baseY, double xScale, double yScale)
          Scale this line by a point, and specified scale.
 void scaleBy(JFPoint basePoint, JFPoint refPoint1, JFPoint refPoint2, double scale)
          Scale current rectangle by specified points and scale percent.
 void setPoint1(double x, double y)
          Set point1, i.e. the point at x1,y1
 void setPoint1(JFPoint pnt)
          Set point1, i.e. the point at x1,y1
 void setPoint2(double x, double y)
          Set point2, i.e. the point at x2,y2
 void setPoint2(JFPoint pnt)
          Set point2, i.e. the point at x2,y2
 void setValue(double x1, double y1, double x2, double y2)
          Set a line's value by two points.
 void setValue(JFPoint point1, JFPoint point2)
          Set a line's value by two points.
 void setValue(LineSeg line)
          Set a line's value by a line segment.
 void setX1(double val)
          Set x coordiate of first endpoint.
 void setX2(double val)
          Set x coordiate of second endpoint.
 void setY1(double val)
          Set y coordiate of first endpoint.
 void setY2(double val)
          Set y coordiate of second endpoint.
 java.lang.String toString()
          Convert this object to String
 JFPoint uprightFoot(double px, double py)
          Returns an upright foot point on this line segment to specified point.
static JFPoint uprightFoot(double slope, double x1, double y1, double px, double py)
          Returns an upright foot point on a specified slope of a line accross a specified point.
 
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, wait
 

Constructor Detail

LineSeg

public LineSeg()
Constructor for LineSeg.


LineSeg

public LineSeg(double x1,
               double y1,
               double x2,
               double y2)
Constructor for LineSeg.


LineSeg

public LineSeg(JFPoint startPoint,
               JFPoint endPoint)
Constructor for LineSeg.

Parameters:
startPoint - The first endpoint.
endPoint - The second endpoint.

LineSeg

public LineSeg(LineSeg lineSeg)
Constructor for LineSeg.

Parameters:
lineSeg - A JFLine Segment.
Method Detail

getX1

public double getX1()
Get x coordiate of first endpoint.

Returns:
The x coordiate of first endpoint.

setX1

public void setX1(double val)
Set x coordiate of first endpoint.

Parameters:
val - A new x coordiate of first endpoint.

getY1

public double getY1()
Get y coordiate of first endpoint.

Returns:
The y coordiate of first endpoint.

setY1

public void setY1(double val)
Set y coordiate of first endpoint.

Parameters:
val - A new y coordiate of first endpoint.

getX2

public double getX2()
Get x coordiate of second endpoint.

Returns:
The x coordiate of second endpoint.

setX2

public void setX2(double val)
Set x coordiate of second endpoint.

Parameters:
val - A new x coordiate of second endpoint.

getY2

public double getY2()
Get y coordiate of second endpoint.

Returns:
The y coordiate of second endpoint.

setY2

public void setY2(double val)
Set y coordiate of second endpoint.

Parameters:
val - A new y coordiate of second endpoint.

getPoint1

public JFPoint getPoint1()
Get point1, i.e. the point at x1,y1

Returns:
The start point of this line.

setPoint1

public void setPoint1(JFPoint pnt)
Set point1, i.e. the point at x1,y1

Parameters:
pnt - A new point.

setPoint1

public void setPoint1(double x,
                      double y)
Set point1, i.e. the point at x1,y1


getPoint2

public JFPoint getPoint2()
Get point2, i.e. the point at x2,y2

Returns:
The end point of this line.

setPoint2

public void setPoint2(JFPoint pnt)
Set point2, i.e. the point at x2,y2

Parameters:
pnt - A new point.

setPoint2

public void setPoint2(double x,
                      double y)
Set point2, i.e. the point at x2,y2


setValue

public void setValue(LineSeg line)
Set a line's value by a line segment.


setValue

public void setValue(JFPoint point1,
                     JFPoint point2)
Set a line's value by two points.

Parameters:
point1 - The start point
point2 - The end point

setValue

public void setValue(double x1,
                     double y1,
                     double x2,
                     double y2)
Set a line's value by two points.


getBounds

public Rect getBounds()
Get the bounds of this line segment.

Returns:
The bounds rectangle.

getLength

public double getLength()
Get the length of this line.

Returns:
the length.

getLength

public static double getLength(JFPoint pnt1,
                               JFPoint pnt2)
Get the length between two endpoints of a line.

Parameters:
pnt1 - One endpoint of the line.
pnt2 - Other endpoint of the line.
Returns:
the length.

getLength

public static double getLength(double x1,
                               double y1,
                               double x2,
                               double y2)
Get the length between two endpoints of a line.

Returns:
the length.

moveBy

public void moveBy(double x,
                   double y)
Move this line by specific x and y coordinates.

Parameters:
x - X coordiate to moveby.
y - Y coordiate to moveby.

getCenter

public JFPoint getCenter()
Get a center point on this line.

Returns:
The center point.

rotateBy

public void rotateBy(double theta)
Rotate this line by an angle theta.

Parameters:
theta - A rotate angle.

rotateBy

public void rotateBy(JFPoint pnt,
                     double theta)
Rotate this line by a specified point and an angle theta.

Parameters:
pnt - A rotate center point.
theta - A rotate angle.

rotateBy

public void rotateBy(double baseX,
                     double baseY,
                     double theta)
Rotate this line by a specified point and an angle theta.

Parameters:
theta - A rotate angle.

mirrorBy

public void mirrorBy()
Mirror this line by a central x coordinate of this line. We make a up-down flip here.


mirrorBy

public void mirrorBy(double baseX)
Mirror this line by a x coordinate. We make a left-right mirror here.

Parameters:
baseX - A mirror base x coordinate.

flipBy

public void flipBy()
Reverse this line by a central y coordinate of this line. We make a up-down flip here.


flipBy

public void flipBy(double baseY)
Reverse this line by a y coordinate. We make a up-down flip here.

Parameters:
baseY - A flip base y coordinate.

scaleBy

public void scaleBy(JFPoint basePoint,
                    JFPoint refPoint1,
                    JFPoint refPoint2,
                    double scale)
Scale current rectangle by specified points and scale percent. We only support a concurrent width-height scale here, suppose width as the length from basePoint to refPoint1, height as the length from basePoint to refPoint2, and one scale percent acts on both width and height.

Parameters:
basePoint - A base point that is unmovable.
refPoint1 - A 'width' reference point.
refPoint2 - A 'height' reference point.
scale - A reference scale percent.

scaleBy

public void scaleBy(double scale)
Scale this line by its center point, and specified scale.

Parameters:
scale - A scale value.

scaleBy

public void scaleBy(double baseX,
                    double baseY,
                    double scale)
Scale this line by a point, and specified scale.

Parameters:
scale - A scale value.

scaleBy

public void scaleBy(double baseX,
                    double baseY,
                    double xScale,
                    double yScale)
Scale this line by a point, and specified scale.

Parameters:
xScale - A scale value in x coordinate.
yScale - A scale value in y coordinate.

contains

public boolean contains(JFPoint pnt)
Test if a point is on this line.

Parameters:
pnt - A point to be measured.
Returns:
True if the point is on this line, false otherwise.

contains

public boolean contains(JFPoint pnt,
                        double pickOffset)
Test if a point is on this line. Here we used an analog offset for 'pick' this line.

Parameters:
pnt - A point to be measured.
pickOffset - An analog offset for 'pick' this line.
Returns:
True if the point is on this line, false otherwise.

contains

public boolean contains(double x,
                        double y)
Test if a point(x, y coordinates for instead) is on this line.

Parameters:
x - X coordinate of this point.
y - Y coordinate of this point.
Returns:
True if the point is on this line, false otherwise.

contains

public boolean contains(double x,
                        double y,
                        double pickOffset)
Test if a point(x, y coordinates for instead) is on this line. Here we used an analog offset for 'pick' this line.

Parameters:
x - X coordinate of this point.
y - Y coordinate of this point.
Returns:
True if the point is on this line, false otherwise.

sameLinePoints

public static boolean sameLinePoints(JFPoint point1,
                                     JFPoint point2,
                                     JFPoint point3)
Test if three points are on same line.

Parameters:
point1 - first point one the line.
point2 - second point one the line
point3 - third point one the line
Returns:
True if the three points are on same line, False otherwise.

sameLinePoints

public static boolean sameLinePoints(double x1,
                                     double y1,
                                     double x2,
                                     double y2,
                                     double x3,
                                     double y3)
Test if three points are on same line.

Returns:
True if the three points are on same line, False otherwise.

distance

public double distance(JFPoint pnt)
Returns the distance from a point to this line segment. The distance measured is the distance between the specified point and the closest point between the specified endpoints. If the specified point is on the line segment, this method returns 0.0.

Parameters:
pnt - the specified point being measured against the specified line segment
Returns:
a double value that is the distance from the specified point to this line segment.

distance

public double distance(double px,
                       double py)
Returns the distance from a point to this line segment. The distance measured is the distance between the specified point and the closest point between the specified endpoints. If the specified point is on the line segment, this method returns 0.0.

Returns:
a double value that is the distance from the specified point to this line segment.

closeTo

public JFPoint closeTo(double px,
                       double py)
Returns a closest point on this line segment to specified point.

Returns:
a point that is on this line segment and has a shortest distance to specified point.

uprightFoot

public JFPoint uprightFoot(double px,
                           double py)
Returns an upright foot point on this line segment to specified point.

Returns:
an upright foot point that is on this line segment or its extended line.

uprightFoot

public static JFPoint uprightFoot(double slope,
                                  double x1,
                                  double y1,
                                  double px,
                                  double py)
Returns an upright foot point on a specified slope of a line accross a specified point.

Parameters:
slope - A slope of a line.
Returns:
an upright foot point that is on this line segment or its extended line.

intersects

public boolean intersects(LineSeg l)
Tests if the specified line segment intersects the this LineSeg.

Parameters:
l - the specified LineSeg to test for intersection with this LineSeg
Returns:
true if the specified LineSeg intersects this LineSeg; false otherwise.
Since:
1.2

intersects

public boolean intersects(JFPoint startPoint,
                          JFPoint endPoint)
Tests if the specified line segment intersects this LineSeg.

Parameters:
startPoint - the first endpoint of the specified line segment
endPoint - the second endpoint of the specified line segment
Returns:
true if the specified line segment intersects the this LineSeg; false otherwise.

intersects

public boolean intersects(double x1,
                          double y1,
                          double x2,
                          double y2)
Tests if the specified line segment intersects this LineSeg.

Returns:
true if the specified line segment intersects the this LineSeg; false otherwise.

intersects

public boolean intersects(Rect rect)
Tests if the specified rectangle intersects the interior of this LineSeg.

Parameters:
rect - the specified Rect to test for intersection with the interior of this LineSeg
Returns:
true if the specified Rect intersects the interior of this LineSeg; false otherwise.

getSlope

public double getSlope()
Get the slope of this line. return 0 if horizontal line, return GeomConst.LARGE_VALUE if vertical line, return any double value if otherwise.

Returns:
the slope of this line.

getSlope

public static double getSlope(LineSeg line)
Get the slope of a specified line. return 0 if horizontal line, return GeomConst.LARGE_VALUE if vertical line, return any double value if otherwise.

Parameters:
line - A line for test slope.
Returns:
the slope of this line.

getSlope

public static double getSlope(JFPoint startPoint,
                              JFPoint endPoint)
Get the slope of a specified line. return 0 if horizontal line, return GeomConst.LARGE_VALUE if vertical line, return any double value if otherwise.

Parameters:
startPoint - one end point of the line.
endPoint - the other end point of the line.
Returns:
the slope of this line.

getSlope

public static double getSlope(double x1,
                              double y1,
                              double x2,
                              double y2)
Get the slope of a specified line. return 0 if horizontal line, return GeomConst.LARGE_VALUE if vertical line, return any double value if otherwise.

Returns:
the slope of this line.

intersectsAt

public JFPoint intersectsAt(JFPoint startPoint,
                            JFPoint endPoint)
Get an intersected point both on this line and a new line.

Parameters:
startPoint - the first endpoint of the specified line segment
endPoint - the second endpoint of the specified line segment
Returns:
a intersected point, null if no interception.

intersectsAt

public JFPoint intersectsAt(double x1,
                            double y1,
                            double x2,
                            double y2)
Get an intersected point both on this line and a new line. The general equation of a line is usually written as y=mx + b where m is the slope of the line and b is the intercept. Slope is how much the vertical (y) coordinate changes for each unit of change in the horizontal direction (x). Intercept is where the line crosses the y-axis, i.e. the value of y when x=0.

If we know two points on a line (x1,y1) and (x2,y2), we can determine m and b as follows. From its definition, m = change in y divided by change in x = (y2-y1)/(x2-x1). And since b=y-mx for any point on the line, we can compute b1=y1-mx1.

With two line segments, we can derive an equation for each, say y=m1x+b1 and y=m2x+b2. The x and y values at the intersection point must satisfy both, so y = m1x+b1=m2x+b2. If we take the last half of this and solve for x, we get m1x+b1=m2x+b2, or (m1-m2)x=b2-b1 or x=(b2-b1)/(m1-m2). Notice that when m1 equals m2, the lines are parallel and they will never intersect. The final test is to see if the intersection point is between the endpoints of each line segment. This just involves making sure that our x is greater than (or equal to) the smallest x endpoint value and smaller than (or equal to) the largest endpoint x value for each line.

Returns:
a intersected point, null if no interception.

toString

public java.lang.String toString()
Convert this object to String

Overrides:
toString in class java.lang.Object
Returns:
An string represents the content of the object

clone

public java.lang.Object clone()
                       throws java.lang.CloneNotSupportedException
Creates a new object of the same class and with the same contents as this object.

Overrides:
clone in class java.lang.Object
Returns:
A clone of this instance.
java.lang.CloneNotSupportedException

hashCode

public int hashCode()
Returns the hashcode for this Object.

Overrides:
hashCode in class java.lang.Object
Returns:
hash code for this Point2D.

equals

public boolean equals(java.lang.Object obj)
Determines whether or not two objects are equal.

Overrides:
equals in class java.lang.Object
Parameters:
obj - an object to be compared with this object
Returns:
true if the object to be compared is an instance of Port and has the same values; false otherwise.


Copyright 2004 ~ 2005 JingFei International Cooperation LTD. All Rights Reserved.