UC Berkeley Group for User Interface Research
Updated November 17, 2000

edu.berkeley.guir.lib.awt.geom
Class GeomLib

java.lang.Object
  |
  +--edu.berkeley.guir.lib.awt.geom.GeomLib

public final class GeomLib
extends Object

Miscellaneous geometry utilities.

This software is distributed under the Berkeley Software License.

 Revisions:  - GUIRLib-v1.3-1.0.0, July 17 2000, JH
               Created class
             - GUIRLib-v1.4-1.0.0, Aug 31 2000, JH
               Touched for GUIRLib v1.4 release
 

Since:
JDK 1.2.2
Version:
GUIRLib-v1.4-1.0.0, Aug 31 2000
Author:
Jason Hong ( jasonh@cs.berkeley.edu )

Method Summary
static Point2D calcIntersectPoint(double xa1, double ya1, double xa2, double ya2, double xb1, double yb1, double xb2, double yb2)
          Figure out where these two lines intersect.
static Point2D calcIntersectPoint(Line2D lAA, Line2D lBB)
          Figure out where these two lines intersect.
static List calcIntersectPoints(Line2D l, Shape s)
          Get a list of all points where the line intersects with the Shape.
static AffineTransform calcPassthrough(AffineTransform txA, AffineTransform txB, AffineTransform txOut)
          Find txC, where [txA] * [txB] = [txB] * [txC].
static Polygon clonePolygon(Polygon p)
          Got sick of writing this piece of code over and over.
static Polygon cutPolygon(Polygon a, Polygon b)
          Cut polygon b out of the boundaries of polygon a.
static float distance(double x1, double y1, double x2, double y2)
          Calculate the Euclidean distance from point 1 to point 2.
static float distance(int x1, int y1, int x2, int y2)
          Calculate the Euclidean distance from point 1 to point 2.
static float distance(Point2D p1, Point2D p2)
           
static Polygon makePolygon(Rectangle rect)
          Make a Polygon out of a Rectangle.
static Polygon makePolygon(Rectangle2D rect)
          Make a Polygon out of a Rectangle.
static Polygon makePolygon(Shape s)
           
static Polygon mergePolygons(Polygon a, Polygon b)
          Merge two polygons together, if they intersect.
static float minDistance(Polygon poly, int x, int y)
          Calculate the minimum distance from all points in the Polygon to the specified point.
static float minDistance(Polygon polyA, Polygon polyB)
          Figure out the min distance between two polygons.
static double minDistance(Shape saa, Shape sbb)
           
static float minDistanceToCenter(Rectangle r, int x, int y)
          Calculate the distance from the (x,y) point to the center of the specified Rectangle.
static Polygon pathIteratorToPolygon(PathIterator it)
          Convert a path iterator into a polygon.
static Polygon simplify(int[] xpoints, int[] ypoints, int npoints)
           
static Polygon simplify(Polygon p)
          Given a polygon, return a "simpler" version of it.
static Polygon transformPolygon(AffineTransform tx, Polygon p)
          Run the transformation on a polygon, returning a new polygon with transformed coordinates.
static Polygon transformPolygonInPlace(AffineTransform tx, Polygon p)
          Run the transformation on a polygon, returning the same polygon with transformed coordinates.
static Rectangle2D transformRectangle(AffineTransform tx, Rectangle2D rSrc)
           
static Rectangle2D transformRectangle(AffineTransform tx, Rectangle2D rSrc, Rectangle2D rDst)
          Apply a transform to a Rectangle.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

clonePolygon

public static final Polygon clonePolygon(Polygon p)
Got sick of writing this piece of code over and over. Silly Polygon should be cloneable.
Returns:
a clone of the specified Polygon.

makePolygon

public static final Polygon makePolygon(Rectangle rect)
Make a Polygon out of a Rectangle.
Parameters:
rect - is a Rectangle to make into a Polygon.
Returns:
a Polygon the same as the Rectangle.

makePolygon

public static final Polygon makePolygon(Rectangle2D rect)
Make a Polygon out of a Rectangle.
Parameters:
rect - is a Rectangle to make into a Polygon.
Returns:
a Polygon the same as the Rectangle.

makePolygon

public static final Polygon makePolygon(Shape s)

simplify

public static final Polygon simplify(int[] xpoints,
                                     int[] ypoints,
                                     int npoints)

simplify

public static final Polygon simplify(Polygon p)
Given a polygon, return a "simpler" version of it.

calcPassthrough

public static AffineTransform calcPassthrough(AffineTransform txA,
                                              AffineTransform txB,
                                              AffineTransform txOut)
Find txC, where [txA] * [txB] = [txB] * [txC]. Used for calculating AffineTransforms and Sticky Graphical Objects.
Parameters:
txA - is as stated in the equation above.
txB - is as stated in the equation above.
txOut - is storage space, use null to create new storage.

transformPolygonInPlace

public static Polygon transformPolygonInPlace(AffineTransform tx,
                                              Polygon p)
Run the transformation on a polygon, returning the same polygon with transformed coordinates.
Parameters:
tx - is the AffineTransform to execute on the Polygon.
p - is Polygon to transform. p is modified.
Returns:
a new Polygon with transformed coordinates.

transformPolygon

public static Polygon transformPolygon(AffineTransform tx,
                                       Polygon p)
Run the transformation on a polygon, returning a new polygon with transformed coordinates.
Parameters:
tx - is the AffineTransform to execute on the Polygon.
p - is Polygon to transform. p is NOT modified in any way.
Returns:
a new Polygon with transformed coordinates. Please note that you may want to keep the original Polygon around, since we have to do some rounding, meaning that this new Polygon may not necessarily have the same shape as the original.

transformRectangle

public static Rectangle2D transformRectangle(AffineTransform tx,
                                             Rectangle2D rSrc)
See Also:
transformRectangle(AffineTransform, Rectangle2D, Rectangle2D)

transformRectangle

public static Rectangle2D transformRectangle(AffineTransform tx,
                                             Rectangle2D rSrc,
                                             Rectangle2D rDst)
Apply a transform to a Rectangle. Note that this doesn't quite make sense if there are shears or rotations. In these cases, we just take the bounding points.
Parameters:
tx - is the transform to apply.
rSrc - is the source rectangle to start with.
rDst - is the destination rectangle after transformation. Okay if same as rSrc.
Returns:
a reference to rDst.

pathIteratorToPolygon

public static final Polygon pathIteratorToPolygon(PathIterator it)
Convert a path iterator into a polygon.

mergePolygons

public static final Polygon mergePolygons(Polygon a,
                                          Polygon b)
Merge two polygons together, if they intersect. Otherwise, just return the first polygon.

This is useful for making Patch areas larger.


cutPolygon

public static final Polygon cutPolygon(Polygon a,
                                       Polygon b)
Cut polygon b out of the boundaries of polygon a.

minDistance

public static final double minDistance(Shape saa,
                                       Shape sbb)

minDistance

public static final float minDistance(Polygon poly,
                                      int x,
                                      int y)
Calculate the minimum distance from all points in the Polygon to the specified point. Make sure that the two are in the same coordinate space.

Technically, this implementation is not correct, since we should really be checking the minimum distance from the specified point to all of the line segments in the Polygon. This was just easier to write and seems good enough for most purposes.

Parameters:
p - is the Polygon to check.
x - is the x-coordinate to check from.
y - is the y-coordinate to check from.
Returns:
the distance from (x,y) to the closest point in Polygon p.

minDistance

public static final float minDistance(Polygon polyA,
                                      Polygon polyB)
Figure out the min distance between two polygons.

minDistanceToCenter

public static final float minDistanceToCenter(Rectangle r,
                                              int x,
                                              int y)
Calculate the distance from the (x,y) point to the center of the specified Rectangle. Make sure that the two are in the same coordinate space.
Parameters:
rect - is the Rectangle whose center point we will use.
x - is the x-coordinate of the other point to use.
y - is the y-coordinate of the other point to use.
Returns:
the distance from rect's center to (x,y).

distance

public static final float distance(int x1,
                                   int y1,
                                   int x2,
                                   int y2)
Calculate the Euclidean distance from point 1 to point 2.
Parameters:
x1 - is the x-coordinate of the first point.
y1 - is the y-coordinate of the first point.
x2 - is the x-coordinate of the second point.
y2 - is the y-coordinate of the second point.

distance

public static final float distance(double x1,
                                   double y1,
                                   double x2,
                                   double y2)
Calculate the Euclidean distance from point 1 to point 2.
Parameters:
x1 - is the x-coordinate of the first point.
y1 - is the y-coordinate of the first point.
x2 - is the x-coordinate of the second point.
y2 - is the y-coordinate of the second point.

distance

public static final float distance(Point2D p1,
                                   Point2D p2)

calcIntersectPoints

public static final List calcIntersectPoints(Line2D l,
                                             Shape s)
Get a list of all points where the line intersects with the Shape.

calcIntersectPoint

public static final Point2D calcIntersectPoint(Line2D lAA,
                                               Line2D lBB)
Figure out where these two lines intersect.
Returns:
null if no intersect.

calcIntersectPoint

public static final Point2D calcIntersectPoint(double xa1,
                                               double ya1,
                                               double xa2,
                                               double ya2,
                                               double xb1,
                                               double yb1,
                                               double xb2,
                                               double yb2)
Figure out where these two lines intersect.
Returns:
null if no intersect.

Copyright Information