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

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

java.lang.Object
  |
  +--edu.berkeley.guir.lib.awt.geom.Polygon2D
All Implemented Interfaces:
Cloneable, Shape
Direct Known Subclasses:
SatinConstants.UniquePolygon2D, TimedPolygon2D

public class Polygon2D
extends Object
implements Cloneable, Shape

Higher precision polygon. For Java2D purposes.

This software is distributed under the Berkeley Software License.

 Revisions:  - GUIRLib-v1.3-1.0.0, Feb 06 1999, JH
               Created class
             - GUIRLib-v1.4-1.0.0, Aug 31 2000, JH
               Touched for GUIRLib release
             - GUIRLib-v1.4-1.0.1, Nov 02 2000, JH
               Added clearPoints() to set number of points to 0.
 

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

Field Summary
 int npoints
           
 float[] xpoints
           
 float[] ypoints
           
 
Constructor Summary
Polygon2D()
           
Polygon2D(float[] xpoints, float[] ypoints, int npoints)
           
Polygon2D(int[] xpoints, int[] ypoints, int npoints)
           
Polygon2D(PathIterator it)
          Create a new Polygon2D approximating the specified PathIterator.
Polygon2D(Rectangle r)
           
Polygon2D(Shape s)
          Create a new Polygon2D approximating the specified Shape.
 
Method Summary
 void addPoint(double x, double y)
           
 void addPoint(Point2D pt)
           
 void clearPoints()
          Remove all points from this polygon.
 Object clone()
           
 Object clone(Polygon2D p)
          Make p the same as the current Polygon.
 boolean contains(double x, double y)
           
 boolean contains(double x, double y, double w, double h)
           
 boolean contains(Point2D pt)
           
 boolean contains(Rectangle2D r)
           
 void cut(Polygon2D p)
          Cut the specified Polygon2D out of the current Polygon2D.
 Rectangle getBounds()
          Deprecated. should use getBounds2D in SATIN and DENIM instead.
 Rectangle2D getBounds2D()
           
 PathIterator getPathIterator(AffineTransform tx)
           
 PathIterator getPathIterator(AffineTransform tx, double flatness)
          Does same as other getPathIterator(AffineTransform).
 boolean intersects(double x, double y, double w, double h)
           
 boolean intersects(Rectangle2D r)
           
 boolean isClosed()
          Check whether this polygon is a closed polygon or not.
 boolean isNormalized()
           
static void main(String[] argv)
           
 float minDistance(double x, double y)
          Calculate the minimum distance from all points in the Polygon to the specified point.
 float minDistance(Polygon p)
          Figure out the min distance between two polygons.
 Point2D normalize()
          Force the top-right bounds to be at (0, 0).
 void paste(Polygon2D p)
          Try to merge the current Polygon2D with the specified Polygon2D.
 double perimeter()
          Calculate the length of the lines in the path.
 void setClosed(boolean flag)
          Set whether this polygon is a closed polygon or not.
 void setPoly(Polygon poly)
          Set this polygon to be the same as the specified one.
 void setPoly(Polygon2D poly)
          Set this polygon to be the same as the specified one.
 void setToPathIterator(PathIterator it)
          Clear out the old polygon, set the polygon to be the specified PathIterator.
 void setToShape(Shape s)
           
 Polygon2D simplify()
          Get a simpler-to-render polygon that looks like this polygon.
 String toString()
           
 void transform(AffineTransform tx)
          Transform this Polygon2D in place.
 Polygon2D transformCopy(AffineTransform tx)
          Copy and then transform the new polygon.
 void translate(double dx, double dy)
          Translate by a certain amount.
 void trim()
          Trim the size of the array for space purposes.
 
Methods inherited from class java.lang.Object
equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

npoints

public int npoints

xpoints

public float[] xpoints

ypoints

public float[] ypoints
Constructor Detail

Polygon2D

public Polygon2D()

Polygon2D

public Polygon2D(Rectangle r)

Polygon2D

public Polygon2D(Shape s)
Create a new Polygon2D approximating the specified Shape. Assumes the paths go in straight lines, mostly because that's easier to program.

Polygon2D

public Polygon2D(PathIterator it)
Create a new Polygon2D approximating the specified PathIterator. Assumes the paths go in straight lines, mostly because that's easier to program.

Polygon2D

public Polygon2D(int[] xpoints,
                 int[] ypoints,
                 int npoints)

Polygon2D

public Polygon2D(float[] xpoints,
                 float[] ypoints,
                 int npoints)
Method Detail

contains

public boolean contains(double x,
                        double y)
Specified by:
contains in interface Shape

contains

public boolean contains(double x,
                        double y,
                        double w,
                        double h)
Specified by:
contains in interface Shape

contains

public boolean contains(Point2D pt)
Specified by:
contains in interface Shape

contains

public boolean contains(Rectangle2D r)
Specified by:
contains in interface Shape

getBounds

public Rectangle getBounds()
Deprecated. should use getBounds2D in SATIN and DENIM instead.

Specified by:
getBounds in interface Shape

getBounds2D

public Rectangle2D getBounds2D()
Specified by:
getBounds2D in interface Shape
Returns:
null if there are 0 points.

getPathIterator

public PathIterator getPathIterator(AffineTransform tx)
Specified by:
getPathIterator in interface Shape

getPathIterator

public PathIterator getPathIterator(AffineTransform tx,
                                    double flatness)
Does same as other getPathIterator(AffineTransform). Flatness is ignored.
Specified by:
getPathIterator in interface Shape

intersects

public boolean intersects(double x,
                          double y,
                          double w,
                          double h)
Specified by:
intersects in interface Shape

intersects

public boolean intersects(Rectangle2D r)
Specified by:
intersects in interface Shape

setToShape

public void setToShape(Shape s)

setToPathIterator

public void setToPathIterator(PathIterator it)
Clear out the old polygon, set the polygon to be the specified PathIterator.
 4 = PathIterator.SEG_CLOSE
 3 = PathIterator.SEG_CUBICTO
 2 = PathIterator.SEG_QUADTO
 1 = PathIterator.SEG_LINETO
 0 = PathIterator.SEG_MOVETO
 1 = PathIterator.WIND_NON_ZERO
 0 = PathIterator.WIND_EVEN_ODD
 

setPoly

public void setPoly(Polygon2D poly)
Set this polygon to be the same as the specified one.

setPoly

public void setPoly(Polygon poly)
Set this polygon to be the same as the specified one.

setClosed

public void setClosed(boolean flag)
Set whether this polygon is a closed polygon or not.

isClosed

public boolean isClosed()
Check whether this polygon is a closed polygon or not.

trim

public void trim()
Trim the size of the array for space purposes. Do this only if you know that the polygon won't be modified any time soon.

addPoint

public void addPoint(Point2D pt)

addPoint

public void addPoint(double x,
                     double y)

clearPoints

public void clearPoints()
Remove all points from this polygon.

translate

public void translate(double dx,
                      double dy)
Translate by a certain amount.

simplify

public Polygon2D simplify()
Get a simpler-to-render polygon that looks like this polygon. Eyeballing some samples, the simplified polygon contains 20-50% of the original number of points.

transform

public void transform(AffineTransform tx)
Transform this Polygon2D in place.
Returns:
a reference to the current Polygon.

transformCopy

public Polygon2D transformCopy(AffineTransform tx)
Copy and then transform the new polygon.
Returns:
a reference to a new and transformed Polygon.

isNormalized

public boolean isNormalized()

normalize

public Point2D normalize()
Force the top-right bounds to be at (0, 0).
Returns:
how much we translated.

paste

public void paste(Polygon2D p)
Try to merge the current Polygon2D with the specified Polygon2D. Does nothing if the two do not intersect, or if one of the two is not closed.

cut

public void cut(Polygon2D p)
Cut the specified Polygon2D out of the current Polygon2D. Does nothing if the two do not intersect, or if one of the two is not closed.

perimeter

public double perimeter()
Calculate the length of the lines in the path.

minDistance

public float minDistance(double x,
                         double 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.

If the polygon is closed, then the distance is 0 if the point is contained.

Parameters:
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 the Polygon.

minDistance

public float minDistance(Polygon p)
Figure out the min distance between two polygons.

toString

public String toString()
Overrides:
toString in class Object

clone

public Object clone()
Overrides:
clone in class Object

clone

public Object clone(Polygon2D p)
Make p the same as the current Polygon. This method is for clone-chaining.

main

public static void main(String[] argv)

Copyright Information