edu.umd.cs.piccolox.util
Class LineShape

java.lang.Object
  extended by edu.umd.cs.piccolox.util.LineShape
All Implemented Interfaces:
MutablePoints, Points, Shape

public class LineShape
extends Object
implements Shape, MutablePoints

A shape that can be used to represent hand drawn lines.


Constructor Summary
LineShape(MutablePoints points)
          Constructs a LineShape from a list of mutable points.
 
Method Summary
 void addPoint(int pointIndex, double x, double y)
          Adds a point with the given coordinates at the desired index.
 boolean contains(double x, double y)
          Returns true if point is within 2 pixels of any line segment of this LineShape.
 boolean contains(double x, double y, double d)
          Returns true if the given coordinates are within d units from any segment of the LineShape.
 boolean contains(double x, double y, double width, double height)
          Whether the LineShape contains the rectangle defined.
static boolean contains(double x, double y, double x1, double y1, double x2, double y2, boolean min, boolean max, double distance)
          Returns whether the given coordinates are on the line defined by (x1,y1) and (x2,y2) within the given distance.
 boolean contains(Point2D p)
          Returns true if point is within 2 pixels of any line segment of this LineShape.
 boolean contains(Rectangle2D r)
          Whether the LineShape contains the rectangle.
 Rectangle getBounds()
          Returns the current points of this LineShape as a simple Rectangle.
 Rectangle2D getBounds(Rectangle2D dst)
          Computes the bounds of this LineShape and stores them in the provided rectangle.
 Rectangle2D getBounds2D()
          Returns the current bounds in Rectangle2D format.
 PathIterator getPathIterator(AffineTransform at)
          Returns an iterator that can be used to iterate of the segments of this LineShape.
 PathIterator getPathIterator(AffineTransform at, double flatness)
          Returns an iterator that can be used to iterate of the segments of this LineShape.
 Point2D getPoint(int pointIndex, Point2D destinationPoint)
          Copies the point at the given index into the destination point.
 int getPointCount()
          Returns the number points in this LineShape.
 double getX(int pointIndex)
          Returns the x component of the point at the given index.
 double getY(int pointIndex)
          Returns the y component of the point at the given index.
 boolean intersects(double x, double y, double w, double h)
          Returns true if any segment crosses an edge of the rectangle.
static boolean intersects(double x1, double y1, double x2, double y2, double x3, double y3, double x4, double y4, boolean min1, boolean max1, boolean min2, boolean max2)
          Returns true if the two segments defined by (x1,y1)->(x2,y2) and (x3,y3)->(x4,y4) intersect.
 boolean intersects(Rectangle2D r)
          Returns true if any segment crosses an edge of the rectangle.
 void removePoints(int pointIndex, int num)
          Removes n points from the LineShape starting at the provided index.
 void setPoint(int pointIndex, double x, double y)
          Sets the coordinate of the point at the given index.
 void setPoints(MutablePoints points)
          Changes the LineShape so that it's composed of the given points.
 void transformPoints(AffineTransform transform)
          Applies the given transform to all points in this LineShape.
 void updateBounds()
          Recalculates the bounds of this LineShape.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

LineShape

public LineShape(MutablePoints points)
Constructs a LineShape from a list of mutable points.

Parameters:
points - points to use when constructing LineShape
Method Detail

setPoints

public void setPoints(MutablePoints points)
Changes the LineShape so that it's composed of the given points.

Parameters:
points - new Points to use as this shape's path

getPointCount

public int getPointCount()
Returns the number points in this LineShape.

Specified by:
getPointCount in interface Points
Returns:
# of points in this line shape

getX

public double getX(int pointIndex)
Returns the x component of the point at the given index.

Specified by:
getX in interface Points
Parameters:
pointIndex - index of desired point
Returns:
x component of indexed point

getY

public double getY(int pointIndex)
Returns the y component of the point at the given index.

Specified by:
getY in interface Points
Parameters:
pointIndex - index of desired point
Returns:
y component of indexed point

getPoint

public Point2D getPoint(int pointIndex,
                        Point2D destinationPoint)
Copies the point at the given index into the destination point.

Specified by:
getPoint in interface Points
Parameters:
pointIndex - the index of the desired point
destinationPoint - the point into which to load the values, or null if a new point is desired
Returns:
destinationPoint or new one if null was provided

getBounds

public Rectangle2D getBounds(Rectangle2D dst)
Computes the bounds of this LineShape and stores them in the provided rectangle.

Specified by:
getBounds in interface Points
Parameters:
dst - rectangle to populate with this LineShape's bounds
Returns:
the bounds

updateBounds

public void updateBounds()
Recalculates the bounds of this LineShape.


setPoint

public void setPoint(int pointIndex,
                     double x,
                     double y)
Sets the coordinate of the point at the given index.

Specified by:
setPoint in interface MutablePoints
Parameters:
pointIndex - index of the point to change
x - x component to assign to the point
y - y component to assign to the point

addPoint

public void addPoint(int pointIndex,
                     double x,
                     double y)
Adds a point with the given coordinates at the desired index.

Specified by:
addPoint in interface MutablePoints
Parameters:
pointIndex - Index at which to add the point
x - x component of the new point
y - y component of the new point

removePoints

public void removePoints(int pointIndex,
                         int num)
Removes n points from the LineShape starting at the provided index.

Specified by:
removePoints in interface MutablePoints
Parameters:
pointIndex - Starting index from which points are being removed
num - The number of sequential points to remove

transformPoints

public void transformPoints(AffineTransform transform)
Applies the given transform to all points in this LineShape.

Specified by:
transformPoints in interface MutablePoints
Parameters:
transform - Transform to apply

getBounds

public Rectangle getBounds()
Returns the current points of this LineShape as a simple Rectangle.

Specified by:
getBounds in interface Shape
Returns:
bounds of this LineShape

getBounds2D

public Rectangle2D getBounds2D()
Returns the current bounds in Rectangle2D format.

Specified by:
getBounds2D in interface Shape
Returns:
bounds of LineShape as a Rectangle2D

contains

public static boolean contains(double x,
                               double y,
                               double x1,
                               double y1,
                               double x2,
                               double y2,
                               boolean min,
                               boolean max,
                               double distance)
Returns whether the given coordinates are on the line defined by (x1,y1) and (x2,y2) within the given distance.

Parameters:
x - x component of point being tested
y - y component of point being tested
x1 - x component of start point of line segment
y1 - y component of start point of line segment
x2 - x component of end point of line segment
y2 - y component of end point of line segment
min - whether the point should be constrained to "after" the start of the segment
max - whether the point should be constrained to "before" the end of the segment
distance - distance from line acceptable as "touching"
Returns:
whether the point (x,y) is near enough to the given line

contains

public boolean contains(double x,
                        double y,
                        double d)
Returns true if the given coordinates are within d units from any segment of the LineShape.

Parameters:
x - x component of point being tested
y - y component of point being tested
d - acceptable distance
Returns:
true if point is close enough to the LineShape

contains

public boolean contains(double x,
                        double y)
Returns true if point is within 2 pixels of any line segment of this LineShape.

Specified by:
contains in interface Shape
Parameters:
x - x component of point being tested
y - y component of point being tested
Returns:
true if point is within 2 pixels of any of this LineShape's segments

contains

public boolean contains(Point2D p)
Returns true if point is within 2 pixels of any line segment of this LineShape.

Specified by:
contains in interface Shape
Parameters:
p - point being tested
Returns:
true if point is within 2 pixels of any of this LineShape's segments

intersects

public static boolean intersects(double x1,
                                 double y1,
                                 double x2,
                                 double y2,
                                 double x3,
                                 double y3,
                                 double x4,
                                 double y4,
                                 boolean min1,
                                 boolean max1,
                                 boolean min2,
                                 boolean max2)
Returns true if the two segments defined by (x1,y1)->(x2,y2) and (x3,y3)->(x4,y4) intersect. Optional fields allow for consideration of extending the segments to infinity at either end.

Parameters:
x1 - segment 1's start x component
y1 - segment 1's start y component
x2 - segment 1's end x component
y2 - segment 1's end y component
x3 - segment 2's start x component
y3 - segment 2's start y component
x4 - segment 2's end x component
y4 - segment 2's end y component
min1 - whether the second segment is acceptable if it passes "before the start of the first segment"
max1 - whether the second segment is acceptable if it passes "after the end of the first segment"
min2 - whether the first segment is acceptable if it passes "before the start of the second segment"
max2 - whether the first segment is acceptable if it passes "after the start of the second segment"
Returns:
true if line segments intersect

intersects

public boolean intersects(double x,
                          double y,
                          double w,
                          double h)
Returns true if any segment crosses an edge of the rectangle.

Specified by:
intersects in interface Shape
Parameters:
x - left of rectangle to be tested
y - top of rectangle to be tested
w - width of rectangle to be tested
h - height of rectangle to be tested
Returns:
true if rectangle intersects

intersects

public boolean intersects(Rectangle2D r)
Returns true if any segment crosses an edge of the rectangle.

Specified by:
intersects in interface Shape
Parameters:
r - rectangle to be tested
Returns:
true if rectangle intersects

contains

public boolean contains(double x,
                        double y,
                        double width,
                        double height)
Whether the LineShape contains the rectangle defined.

Specified by:
contains in interface Shape
Parameters:
x - left of defined rectangle
y - top of defined rectangle
width - width of defined rectangle
height - height of defined rectangle
Returns:
true if rectangle is contained

contains

public boolean contains(Rectangle2D r)
Whether the LineShape contains the rectangle.

Specified by:
contains in interface Shape
Parameters:
r - rectangle being tested
Returns:
true if rectangle is contained

getPathIterator

public PathIterator getPathIterator(AffineTransform at)
Returns an iterator that can be used to iterate of the segments of this LineShape. Optionally applying the given transform before returning it.

Specified by:
getPathIterator in interface Shape
Parameters:
at - optional transform to apply to segment before returning it. May be null
Returns:
iterator for iterating segments of this LineShape

getPathIterator

public PathIterator getPathIterator(AffineTransform at,
                                    double flatness)
Returns an iterator that can be used to iterate of the segments of this LineShape. Optionally applying the given transform before returning it.

Specified by:
getPathIterator in interface Shape
Parameters:
at - optional transform to apply to segment before returning it. May be null
flatness - ignored completely
Returns:
iterator for iterating segments of this LineShape


Copyright © 1995-2010 Piccolo2D. All Rights Reserved.