Class BasicShape

  • All Implemented Interfaces:
    Shape


    public class BasicShape
    extends Object
    implements Shape
    A faster, open-source alternative to the Area class. It might even be finished done day, too.
    • Constructor Detail

      • BasicShape

        public BasicShape(Shape shape)
        Creates a BasicShape from a shape.

        If the shape is a simple shape in the java.awt.geom package, then you should use a constructor that accepts a Shape argument instead of a PathIterator argument.

        Parameters:
        shape - the shape data to copy.
      • BasicShape

        public BasicShape(Shape shape,
                          AffineTransform transform)
        Creates a BasicShape from a shape.

        If the shape is a simple shape in the java.awt.geom package, then you should use a constructor that accepts a Shape argument instead of a PathIterator argument.

        Parameters:
        shape - the shape data to copy.
        transform - an optional transform to apply to the incoming shape.
      • BasicShape

        public BasicShape(PathIterator i)
        Creates a BasicShape from a path.
        Parameters:
        i - path data.
      • BasicShape

        protected BasicShape(PathIterator i,
                             boolean ignoreWinding)
        Creates a BasicShape from a path.
        Parameters:
        i - path data.
        ignoreWinding - if a path is WIND_NON_ZERO, then it normally will be converted. However some shapes (rectangles, circles, etc) don't need to be converted, so if you pass true here then that conversion will be skipped. By default you should pass false if you don't know the nature of the PathIterator you're working with.
      • BasicShape

        public BasicShape(String s)
        Creates a BasicShape from a serialized string.
        Parameters:
        s - this string should be the result of a former call to:
        myBasicShape.toString()
      • BasicShape

        public BasicShape(BasicShape s)
        Clones a BasicShape.
    • Method Detail

      • near

        protected static boolean near(double v1,
                                      double v2,
                                      double tolerance)
        Return true if two values are near each other.
        Parameters:
        v1 - the first value
        v2 - the second value
        tolerance - the acceptable tolerance. Must be positive.
        Returns:
        true if abs(v1-v2)<=tolerance
      • toString

        protected static String toString(double[] array)
      • toString

        protected static String toString(double[] array,
                                         int offset,
                                         int len)
        This lists an array of double values as a String. Mostly used for debugging.
        Parameters:
        array -
        offset -
        len -
      • convertToWindEvenOdd

        protected void convertToWindEvenOdd()
      • updateBounds

        protected void updateBounds()
        This method should be called when this shape has undergone a transformation.
      • contains

        public boolean contains(Point2D p)
      • contains

        public boolean contains(double x,
                                double y)
      • contains

        protected static boolean contains(com.bric.geom.BasicShapeSegment path,
                                          double x,
                                          double y,
                                          int windingRule,
                                          boolean includeEdges)
      • contains

        protected boolean contains(double x,
                                   double y,
                                   int windingRule)
      • add

        public void add(BasicShape shape)
        Adds the argument shape to this shape.

        If shape B is added to shape A, then the resulting shape will contain points in either A or B.

        Parameters:
        shape - the shape to add.
      • subtract

        public void subtract(BasicShape shape)
        Subtracts the argument shape from this shape.

        If shape B is subtracted from shape A, then the resulting shape will contain only points in A that do not also lie in B.

        Parameters:
        shape - the shape to subtract. For best performance this should be a BasicShape.
      • intersect

        public void intersect(BasicShape shape)
        Intersects the argument shape with this shape. This is also referred to as "clipping".

        If shape A is intersected with shape B, then the resulting shape will contain only points in both A and B.

        Parameters:
        shape - the shape to intersect with. For best performance this should be a BasicShape.
      • countCrossings

        protected static int countCrossings(com.bric.geom.BasicShapeSegment t,
                                            double x,
                                            double y,
                                            boolean includeEdges)
        This counts the number of crossings from (-infinity,y) to (x,y).

        This total is used in combination with the winding rule to determine when a point is inside a shape.

        Parameters:
        includeEdges - whether a point exactly on the edge of the shape counts.
      • contains

        public boolean contains(double x,
                                double y,
                                double w,
                                double h)
      • identifyCrossings

        protected boolean identifyCrossings(double x,
                                            double y,
                                            double w,
                                            double h)
        This checks 2 things: 1. Does this shape have any segments that lie completely inside the bounds provided? 2. Does this shape have any segments that cross the bounds provided?
      • identifyIntersections

        protected com.bric.geom.Intersection identifyIntersections(BasicShape shape)
        This identifies all the intersections of this shape and the argument. This relies on an IntersectionIdentifier to find those intersections.

        This replaces any previously existing intersection information in the two BasicShapes, so you cannot call this method on different threads simultaneously for the same shapes.

        Parameters:
        shape - the other shape to identify intersections with.
        Returns:
        any arbitrary Intersection object, if one was found.
      • getPathIterator

        public PathIterator getPathIterator(AffineTransform at)
        Creates a PathIterator for the outline of this object.
        Specified by:
        getPathIterator in interface  Shape
        Parameters:
        at - an optional AffineTransform to apply to this shape data
      • getPathIterator

        public PathIterator getPathIterator(AffineTransform at,
                                            double flatness)
        Creates a FlatteningPathIterator for the outline of this object.
        Specified by:
        getPathIterator in interface  Shape
        Parameters:
        at - an optional AffineTransform to apply to this shape data
        flatness - the maximum amount that the control points for a given curve can vary from colinear before a subdivided curve is replaced by a straight line connecting the endpoints
      • intersects

        public boolean intersects(Rectangle2D r)
      • intersects

        public boolean intersects(double x,
                                  double y,
                                  double w,
                                  double h)
      • transform

        public void transform(AffineTransform transform)
        Transforms this shape through the AffineTransform provided.
        Parameters:
        transform - the transform to apply to this shape.
      • toString

        public String toString()
        A java.lang.String representation of this shape.

        Note that this String can later be passed as an argument to the constructor to re-create this shape, so this is a very efficient way to serialize this data.

      • releaseIterators

        protected void releaseIterators()
        When we are about to modify this object we need to synch on this object and call this method. This will make sure any iterators that still depend on this shape have a copy of their original data in tact, and they don't try to iterate over our modified data.