Class Geometry

java.lang.Object
  extended byGeometry

public final class Geometry
extends java.lang.Object

Collection of geometry utility methods. All methods are static.


Constructor Summary
Geometry()
           
 
Method Summary
static double computeAngle(double[] p0, double[] p1, double[] p2)
          Find the angle between twree points.
static double[] computeCrossProduct(double[] v0, double[] v1)
          Compute the cross product (a vector) of two vectors.
static double computeDotProduct(double[] v0, double[] v1)
          Compute the dot product (a scalar) between two vectors.
static double[] computePointOnLine(double[] p0, double[] p1, double fractionFromP0)
          Find the point on the line p0,p1 [x,y,z] a given fraction from p0.
static double[] computePointOnLine(double x0, double y0, double x1, double y1, double fractionFrom0)
          Find the point on the line defined by x0,y0,x1,y1 a given fraction from x0,y0.
static double computePolygonArea(double[] xy)
          Compute the area of the specfied polygon.
static double computePolygonArea(double[] x, double[] y)
          Compute the area of the specfied polygon.
static double[] computePolygonCentroid(double[] x, double[] y)
          Compute centorid (center of gravity) of specified polygon.
static int[] createArrow(int[] x, int[] y, double length, double angle, double inset)
          Create the geometry of an arrow.
static int[] createArrow(int x0, int y0, int x1, int y1, double length, double angle, double inset)
          Create geometry for an arrow along the specified line and with tip at x1,y1.
static double[] createCircle(double x0, double y0, double radius)
          Create geometry for a circle.
static int[] createCircle(int x0, int y0, int radius)
          Create geometry for a circle.
static double[] createEllipse(double x0, double y0, double dx, double dy)
          Create the geometry for an unrotated, unskewed ellipse.
static int[] createEllipse(int[] x, int[] y)
          Return the geometry of an ellipse based on its four top points.
static int[] createEllipse(int x0, int y0, int dx, int dy)
          Create the geometry for an unrotated, unskewed ellipse.
static double[] createRectangle(double x0, double y0, double width, double height)
          Create geometry for a rectangle.
static int[] createRectangle(int x0, int y0, int width, int height)
          Create geometry for a rectangle.
static int[] createSector(int x0, int y0, int radius, double angle0, double angle1)
          Create the geometry of a sector of a circle.
static int[] createSector(int x0, int y0, int dx, int dy, double angle0, double angle1)
          Create the geometry of a sector of an ellipse.
static double[] createStar(double x0, double y0, double innerRadius, double outerRadius, int nArms)
          Create geometry of a star.
static int[] createStar(int x0, int y0, int innerRadius, int outerRadius, int nArms)
          Create geometry of a star.
static double[] createVector(double[] p0, double[] p1)
          Construct the vector specified by two points.
static double distance(int x0, int y0, int x1, int y1, int x, int y)
          Return distance bwetween the line defined by (x0,y0) and (x1,y1) and the point (x,y).
static void extendLine(double[] p0, double[] p1, double toLength)
          Extend a given line segment to a given length and holding the first point of the line as fixed.
static void extendLine(double[] p0, double[] p1, double toLength, double anchor)
          Extend a given line segment to a specified length.
static double[] findLinePolygonIntersections(double[] x, double[] y, double x0, double y0, double x1, double y1)
          Find the intersections between a polygon and a straight line.
static int findLineSegmentIntersection(double x0, double y0, double x1, double y1, double x2, double y2, double x3, double y3, double[] intersection)
          Compute the intersection between two line segments, or two lines of infinite length.
static void findPolygonExtent(double[] x, double[] y, double[] z, double[] xExtent, double[] yExtent, double[] zExtent)
          Find the 3D extent of a polyline.
static void findPolygonExtent(int[] x, int[] y, int[] xExtent, int[] yExtent)
          Find the extent of a polygon.
static boolean findPolygonPosition(int[] x, int[] y, double length, int[] position)
          Return the x,y position at distance "length" into the given polyline.
static boolean isLineIntersectingLine(int x0, int y0, int x1, int y1, int x2, int y2, int x3, int y3)
          Check if two line segments intersects.
static boolean isLineIntersectingRectangle(int lx0, int ly0, int lx1, int ly1, int x0, int y0, int x1, int y1)
          Check if a specified line intersects a specified rectangle.
static boolean isPointInsidePolygon(double[] x, double[] y, double pointX, double pointY)
          Check if a given point is inside a given (complex) polygon.
static boolean isPointInsidePolygon(int[] x, int[] y, int pointX, int pointY)
          Check if a given point is inside a given polygon.
static boolean isPointInsideRectangle(int x0, int y0, int x1, int y1, int x, int y)
          Check if a specified point is inside a specified rectangle.
static boolean isPolygonIntersectingRectangle(int[] x, int[] y, int x0, int y0, int x1, int y1)
          Check if a specified polygon intersects a specified rectangle.
static boolean isPolylineIntersectingRectangle(int[] x, int[] y, int x0, int y0, int x1, int y1)
          Check if a specified polyline intersects a specified rectangle.
static double length(double[] v)
          Return the length of a vector.
static double length(double[] p0, double[] p1)
          Compute distance between two points.
static double length(double x0, double y0, double x1, double y1)
          Compute the length of the line from (x0,y0) to (x1,y1)
static double length(int[] x, int[] y, boolean isClosed)
          Compute the length of a polyline.
static double length(int x0, int y0, int x1, int y1)
          Compute the length of the line from (x0,y0) to (x1,y1)
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Geometry

public Geometry()
Method Detail

isPointInsideRectangle

public static boolean isPointInsideRectangle(int x0,
                                             int y0,
                                             int x1,
                                             int y1,
                                             int x,
                                             int y)
Check if a specified point is inside a specified rectangle.

Returns:
True if the point is inside the rectangle, false otherwise.

isPointInsidePolygon

public static boolean isPointInsidePolygon(double[] x,
                                           double[] y,
                                           double pointX,
                                           double pointY)
Check if a given point is inside a given (complex) polygon.

Returns:
True if the given point is inside the polygon, false otherwise.

isPointInsidePolygon

public static boolean isPointInsidePolygon(int[] x,
                                           int[] y,
                                           int pointX,
                                           int pointY)
Check if a given point is inside a given polygon. Integer domain.

Returns:
True if the given point is inside the polygon, false otherwise.

computePointOnLine

public static double[] computePointOnLine(double[] p0,
                                          double[] p1,
                                          double fractionFromP0)
Find the point on the line p0,p1 [x,y,z] a given fraction from p0. Fraction of 0.0 whould give back p0, 1.0 give back p1, 0.5 returns midpoint of line p0,p1 and so on. F raction can be >1 and it can be negative to return any point on the line specified by p0,p1.

Parameters:
p0 - First coordinale of line [x,y,z].
fractionFromP0 - Point we are looking for coordinates of

computePointOnLine

public static double[] computePointOnLine(double x0,
                                          double y0,
                                          double x1,
                                          double y1,
                                          double fractionFrom0)
Find the point on the line defined by x0,y0,x1,y1 a given fraction from x0,y0. 2D version of method above..

Parameters:
fractionFrom0 - Distance from (x0,y0)
Returns:
x, y Coordinate of point we are looking for

extendLine

public static void extendLine(double[] p0,
                              double[] p1,
                              double toLength,
                              double anchor)
Extend a given line segment to a specified length.

Parameters:
toLength - Length of new line segment.
anchor - Specifies the fixed point during extension. If anchor is 0.0, p0 is fixed and p1 is adjusted. If anchor is 1.0, p1 is fixed and p0 is adjusted. If anchor is 0.5, the line is adjusted equally in each direction and so on.

extendLine

public static void extendLine(double[] p0,
                              double[] p1,
                              double toLength)
Extend a given line segment to a given length and holding the first point of the line as fixed.


length

public static double length(double[] v)
Return the length of a vector.

Parameters:
v - Vector to compute length of [x,y,z].
Returns:
Length of vector.

length

public static double length(double[] p0,
                            double[] p1)
Compute distance between two points.

Returns:
Distance between points.

length

public static double length(int x0,
                            int y0,
                            int x1,
                            int y1)
Compute the length of the line from (x0,y0) to (x1,y1)

Returns:
Length of line from (x0,y0) to (x1,y1).

length

public static double length(double x0,
                            double y0,
                            double x1,
                            double y1)
Compute the length of the line from (x0,y0) to (x1,y1)

Returns:
Length of line from (x0,y0) to (x1,y1).

length

public static double length(int[] x,
                            int[] y,
                            boolean isClosed)
Compute the length of a polyline.

Parameters:
isClosed - True if this is a closed polygon, false otherwise
Returns:
Length of polyline defined by x, y and nPoints.

distance

public static double distance(int x0,
                              int y0,
                              int x1,
                              int y1,
                              int x,
                              int y)
Return distance bwetween the line defined by (x0,y0) and (x1,y1) and the point (x,y). Ref: http://astronomy.swin.edu.au/pbourke/geometry/pointline/ The 3D case should be similar.

Returns:
Distance from x,y down to the (extended) line defined by x0, y0, x1, y1.

computeAngle

public static double computeAngle(double[] p0,
                                  double[] p1,
                                  double[] p2)
Find the angle between twree points. P0 is center point

Returns:
Angle (in radians) between given points.

computeDotProduct

public static double computeDotProduct(double[] v0,
                                       double[] v1)
Compute the dot product (a scalar) between two vectors.

Returns:
Dot product of given vectors.

computeCrossProduct

public static double[] computeCrossProduct(double[] v0,
                                           double[] v1)
Compute the cross product (a vector) of two vectors.


createVector

public static double[] createVector(double[] p0,
                                    double[] p1)
Construct the vector specified by two points.

Returns:
v Vector from p0 to p1 [x,y,z].

isLineIntersectingLine

public static boolean isLineIntersectingLine(int x0,
                                             int y0,
                                             int x1,
                                             int y1,
                                             int x2,
                                             int y2,
                                             int x3,
                                             int y3)
Check if two line segments intersects. Integer domain.

Returns:
True if the two lines intersects.

isLineIntersectingRectangle

public static boolean isLineIntersectingRectangle(int lx0,
                                                  int ly0,
                                                  int lx1,
                                                  int ly1,
                                                  int x0,
                                                  int y0,
                                                  int x1,
                                                  int y1)
Check if a specified line intersects a specified rectangle. Integer domain.

Returns:
True if the line intersects the rectangle, false otherwise.

isPolylineIntersectingRectangle

public static boolean isPolylineIntersectingRectangle(int[] x,
                                                      int[] y,
                                                      int x0,
                                                      int y0,
                                                      int x1,
                                                      int y1)
Check if a specified polyline intersects a specified rectangle. Integer domain.

Returns:
True if the polyline intersects the rectangle, false otherwise.

isPolygonIntersectingRectangle

public static boolean isPolygonIntersectingRectangle(int[] x,
                                                     int[] y,
                                                     int x0,
                                                     int y0,
                                                     int x1,
                                                     int y1)
Check if a specified polygon intersects a specified rectangle. Integer domain.

Parameters:
x - X coordinates of polyline.
y - Y coordinates of polyline.
x0 - X of upper left corner of rectangle.
y0 - Y of upper left corner of rectangle.
x1 - X of lower right corner of rectangle.
y1 - Y of lower right corner of rectangle.
Returns:
True if the polyline intersects the rectangle, false otherwise.

computePolygonArea

public static double computePolygonArea(double[] x,
                                        double[] y)
Compute the area of the specfied polygon.

Parameters:
x - X coordinates of polygon.
y - Y coordinates of polygon.
Returns:
Area of specified polygon.

computePolygonArea

public static double computePolygonArea(double[] xy)
Compute the area of the specfied polygon.

Parameters:
xy - Geometry of polygon [x,y,...]
Returns:
Area of specified polygon.

computePolygonCentroid

public static double[] computePolygonCentroid(double[] x,
                                              double[] y)
Compute centorid (center of gravity) of specified polygon.

Parameters:
x - X coordinates of polygon.
y - Y coordinates of polygon.
Returns:
Centroid [x,y] of specified polygon.

findPolygonExtent

public static void findPolygonExtent(double[] x,
                                     double[] y,
                                     double[] z,
                                     double[] xExtent,
                                     double[] yExtent,
                                     double[] zExtent)
Find the 3D extent of a polyline.

Parameters:
x - X coordinates of polyline.
y - Y coordinates of polyline.
z - Z coordinates of polyline. May be null if this is a 2D case.
xExtent - Will upon return contain [xMin,xMax].
yExtent - Will upon return contain [xMin,xMax].
zExtent - Will upon return contain [xMin,xMax]. Unused (may be set to null) if z is null.

findPolygonExtent

public static void findPolygonExtent(int[] x,
                                     int[] y,
                                     int[] xExtent,
                                     int[] yExtent)
Find the extent of a polygon.

Parameters:
x - X coordinates of polygon.
y - Y coordinates of polygon.
xExtent - Will upon return contain [xMin, xMax]
yExtent - Will upon return contain [yMin, yMax]

findLineSegmentIntersection

public static int findLineSegmentIntersection(double x0,
                                              double y0,
                                              double x1,
                                              double y1,
                                              double x2,
                                              double y2,
                                              double x3,
                                              double y3,
                                              double[] intersection)
Compute the intersection between two line segments, or two lines of infinite length.

Parameters:
x0 - X coordinate first end point first line segment.
y0 - Y coordinate first end point first line segment.
x1 - X coordinate second end point first line segment.
y1 - Y coordinate second end point first line segment.
x2 - X coordinate first end point second line segment.
y2 - Y coordinate first end point second line segment.
x3 - X coordinate second end point second line segment.
y3 - Y coordinate second end point second line segment.
Returns:
-1 if lines are parallel (x,y unset), -2 if lines are parallel and overlapping (x, y center) 0 if intesrection outside segments (x,y set) +1 if segments intersect (x,y set)

findLinePolygonIntersections

public static double[] findLinePolygonIntersections(double[] x,
                                                    double[] y,
                                                    double x0,
                                                    double y0,
                                                    double x1,
                                                    double y1)
Find the intersections between a polygon and a straight line. NOTE: This method is only guaranteed to work if the polygon is first preprocessed so that "unneccesary" vertices are removed (i.e vertices on the straight line between its neighbours).

Parameters:
x - X coordinates of polygon.
y - Y coordinates of polygon.
x0 - X first end point of line.
Returns:
Intersections [x,y,x,y...].

createEllipse

public static int[] createEllipse(int[] x,
                                  int[] y)
Return the geometry of an ellipse based on its four top points. Integer domain. The method use the generic createEllipse() method for the main task, and then transforms this according to any rotation or skew defined by the given top points.

Parameters:
x - X array of four top points of ellipse.
y - Y array of four top points of ellipse.
Returns:
Geometry of ellipse [x,y,x,y...].

createEllipse

public static int[] createEllipse(int x0,
                                  int y0,
                                  int dx,
                                  int dy)
Create the geometry for an unrotated, unskewed ellipse. Integer domain.

Parameters:
x0 - X center of ellipse.
y0 - Y center of ellipse.
dx - X ellipse radius.
dy - Y ellipse radius.
Returns:
Ellipse geometry [x,y,x,y,...].

createEllipse

public static double[] createEllipse(double x0,
                                     double y0,
                                     double dx,
                                     double dy)
Create the geometry for an unrotated, unskewed ellipse. Floating point domain.

Parameters:
x0 - X center of ellipse.
y0 - Y center of ellipse.
dx - X ellipse radius.
dy - Y ellipse radius.
Returns:
Ellipse geometry [x,y,x,y,...].

createCircle

public static int[] createCircle(int x0,
                                 int y0,
                                 int radius)
Create geometry for a circle. Integer domain.

Parameters:
x0 - X center of circle.
y0 - Y center of circle.
radius - Radius of circle.
Returns:
Geometry of circle [x,y,...]

createCircle

public static double[] createCircle(double x0,
                                    double y0,
                                    double radius)
Create geometry for a circle. Floating point domain.

Parameters:
x0 - X center of circle.
y0 - Y center of circle.
radius - Radius of circle.
Returns:
Geometry of circle [x,y,...]

createSector

public static int[] createSector(int x0,
                                 int y0,
                                 int dx,
                                 int dy,
                                 double angle0,
                                 double angle1)
Create the geometry of a sector of an ellipse.

Parameters:
x0 - X coordinate of center of ellipse.
y0 - Y coordinate of center of ellipse.
dx - X radius of ellipse.
dy - Y radius of ellipse.
angle0 - First angle of sector (in radians).
angle1 - Second angle of sector (in radians).
Returns:
Geometry of secor [x,y,...]

createSector

public static int[] createSector(int x0,
                                 int y0,
                                 int radius,
                                 double angle0,
                                 double angle1)
Create the geometry of a sector of a circle.

Parameters:
x0 - X coordinate of center of ellipse.
y0 - Y coordinate of center of ellipse.
angle0 - First angle of sector (in radians).
angle1 - Second angle of sector (in radians).
Returns:
Geometry of secor [x,y,...]

createArrow

public static int[] createArrow(int[] x,
                                int[] y,
                                double length,
                                double angle,
                                double inset)
Create the geometry of an arrow. The arrow is positioned at the end (last point) of the specified polyline, as follows: 0,4--, \ --, \ --, \ --, \ --, -------------------------3-----------1 / --' / --' / --' / --' 2--'

Parameters:
x - X coordinates of polyline of where arrow is positioned in the end. Must contain at least two points.
y - Y coordinates of polyline of where arrow is positioned in the end.
length - Length along the main axis from point 1 to the projection of point 0.
angle - Angle between the main axis and the line 1,0 (and 1,2) in radians.
inset - Specification of point 3 [0.0-1.0], 1.0 will put point 3 at distance length from 1, 0.0 will put it at point 1.
Returns:
Array of the five coordinates [x,y,...].

createArrow

public static int[] createArrow(int x0,
                                int y0,
                                int x1,
                                int y1,
                                double length,
                                double angle,
                                double inset)
Create geometry for an arrow along the specified line and with tip at x1,y1. See general method above.

Parameters:
x0 - X first end point of line.
y0 - Y first end point of line.
x1 - X second end point of line.
y1 - Y second end point of line.
length - Length along the main axis from point 1 to the projection of point 0.
angle - Angle between the main axis and the line 1,0 (and 1.2)
inset - Specification of point 3 [0.0-1.0], 1.0 will put point 3 at distance length from 1, 0.0 will put it at point 1.
Returns:
Array of the four coordinates [x,y,...].

createRectangle

public static int[] createRectangle(int x0,
                                    int y0,
                                    int width,
                                    int height)
Create geometry for a rectangle. Returns a closed polygon; first and last points matches. Integer domain.

Parameters:
x0 - X corner of rectangle.
y0 - Y corner of rectangle.
width - Width (may be negative to indicate leftwards direction)
height - Height (may be negative to indicaten upwards direction)

createRectangle

public static double[] createRectangle(double x0,
                                       double y0,
                                       double width,
                                       double height)
Create geometry for a rectangle. Returns a closed polygon; first and last points matches. Floating point domain.

Parameters:
x0 - X corner of rectangle.
y0 - Y corner of rectangle.
width - Width (may be negative to indicate leftwards direction)
height - Height (may be negative to indicaten upwards direction)

createStar

public static int[] createStar(int x0,
                               int y0,
                               int innerRadius,
                               int outerRadius,
                               int nArms)
Create geometry of a star. Integer domain.

Parameters:
x0 - X center of star.
y0 - Y center of star.
innerRadius - Inner radis of arms.
outerRadius - Outer radius of arms.
nArms - Number of arms.
Returns:
Geometry of star [x,y,x,y,...].

createStar

public static double[] createStar(double x0,
                                  double y0,
                                  double innerRadius,
                                  double outerRadius,
                                  int nArms)
Create geometry of a star. Floating point domain.

Parameters:
x0 - X center of star.
y0 - Y center of star.
innerRadius - Inner radis of arms.
outerRadius - Outer radius of arms.
nArms - Number of arms.
Returns:
Geometry of star [x,y,x,y,...].

findPolygonPosition

public static boolean findPolygonPosition(int[] x,
                                          int[] y,
                                          double length,
                                          int[] position)
Return the x,y position at distance "length" into the given polyline.

Parameters:
x - X coordinates of polyline
y - Y coordinates of polyline
length - Requested position
position - Preallocated to int[2]
Returns:
True if point is within polyline, false otherwise