View Javadoc

1   /* File Shape2D.java 
2    *
3    * Project : Java Geometry Library
4    *
5    * ===========================================
6    * 
7    * This library is free software; you can redistribute it and/or modify it 
8    * under the terms of the GNU Lesser General Public License as published by
9    * the Free Software Foundation, either version 2.1 of the License, or (at
10   * your option) any later version.
11   *
12   * This library is distributed in the hope that it will be useful, but 
13   * WITHOUT ANY WARRANTY, without even the implied warranty of MERCHANTABILITY
14   * or FITNESS FOR A PARTICULAR PURPOSE.
15   *
16   * See the GNU Lesser General Public License for more details.
17   *
18   * You should have received a copy of the GNU Lesser General Public License
19   * along with this library. if not, write to :
20   * The Free Software Foundation, Inc., 59 Temple Place, Suite 330,
21   * Boston, MA 02111-1307, USA.
22   */
23  
24  // package
25  
26  package math.geom2d;
27  
28  import java.awt.Graphics2D;
29  import java.io.Serializable;
30  
31  // Imports
32  
33  /**
34   * <p> 
35   * Main interface for all geometric objects, including points, lines, curves,
36   * or planar regions... Instances of Shape2D can be either bounded
37   * (a point, a line segment, a square...) or unbounded (a parabola,
38   * a half-plane...).</p>
39   * <p>
40   * Shape2D implementations implements a more specialized interface depending 
41   * on the shape inner dimension:
42   * {@link math.geom2d.curve.Curve2D Curve2D}, 
43   * {@link math.geom2d.domain.Domain2D Domain2D} or
44   * {@link math.geom2d.point.PointShape2D PointShape2D}.</p>
45   * <p>
46   * Shape2D interface provide convenient method to check if the shape
47   * {@link #isEmpty() is empty}, to {@link #transform(AffineTransform2D) 
48   * transform} or to {@link #clip(Box2D) clip} the shape, get its
49   * {@link #getBoundingBox() bounding box}, or its
50   * {@link #getDistance(Point2D) distance} to a given point.</p>
51   */
52  public interface Shape2D extends Serializable {
53  
54      // ===================================================================
55      // constants
56  
57      /**
58       * The constant used for testing results.
59       */
60      public final static double  ACCURACY  = 1e-12;
61  
62      /**
63       * Checks if the shape contains the planar point defined by (x,y).
64       */
65      public abstract boolean contains(double x, double y);
66  
67      /**
68       * Checks if the shape contains the given point.
69       */
70      public abstract boolean contains(java.awt.geom.Point2D p);
71  
72      /**
73       * get the distance of the shape to the given point, or the distance of
74       * point to the frontier of the shape in the case of a plain shape.
75       */
76      public abstract double getDistance(java.awt.geom.Point2D p);
77  
78      /**
79       * get the distance of the shape to the given point, specified by x and y,
80       * or the distance of point to the frontier of the shape in the case of a
81       * plain (i.e. fillable) shape.
82       */
83      public abstract double getDistance(double x, double y);
84  
85      /**
86       * Returns true if the shape is bounded, that is if we can draw a finite
87       * rectangle enclosing the shape. For example, a straight line or a parabola
88       * are not bounded.
89       */
90      public abstract boolean isBounded();
91  
92      /**
93       * Returns true if the shape does not contain any point. This is the case
94       * for example for PointSet2D without any point.
95       * 
96       * @return true if the shape does not contain any point.
97       */
98      public abstract boolean isEmpty();
99  
100     /**
101      * Returns the bounding box of the shape.
102      * 
103      * @return the bounding box of the shape.
104      */
105     public abstract Box2D getBoundingBox();
106 
107     /**
108      * Clip the shape with the given box, and returns a new shape. The box must
109      * be bounded.
110      * 
111      * @param box the clipping box
112      * @return the clipped shape
113      */
114     public abstract Shape2D clip(Box2D box);
115 
116     /**
117      * Transforms the shape by an affine transform. Subclasses may override the
118      * type of returned shape.
119      * 
120      * @param trans an affine transform
121      * @return the transformed shape
122      */
123     public abstract Shape2D transform(AffineTransform2D trans);
124 
125     /**
126      * Draw the shape on the given graphics. 
127      * If the shape is empty, nothing is drawn.
128      * If the shape is unbounded, an exception is thrown.
129      */
130     public abstract void draw(Graphics2D g2);
131     
132 }