FileDocCategorySizeDatePackage
Arc2D.javaAPI DocAndroid 1.5 API37089Wed May 06 22:41:54 BST 2009java.awt.geom

Arc2D

public abstract class Arc2D extends RectangularShape
The Class Arc2D represents a segment of a curve inscribed in a rectangle. The curve is defined by a start angle and an extent angle (the end angle minus the start angle) as a pie wedge whose point is in the center of the rectangle. The Arc2D as a shape may be either OPEN (including nothing but the curved arc segment itself), CHORD (the curved arc segment closed by a connecting segment from the end to the beginning of the arc, or PIE (the segments from the end of the arc to the center of the rectangle and from the center of the rectangle back to the arc's start point are included).
since
Android 1.0

Fields Summary
public static final int
OPEN
The arc type OPEN indicates that the shape includes only the curved arc segment.
public static final int
CHORD
The arc type CHORD indicates that as a shape the connecting segment from the end point of the curved arc to the beginning point is included.
public static final int
PIE
The arc type PIE indicates that as a shape the two segments from the arc's endpoint to the center of the rectangle and from the center of the rectangle to the arc's endpoint are included.
private int
type
The closure type of the arc.
Constructors Summary
protected Arc2D(int type)
Instantiates a new arc2D.

param
type the closure type.

        setArcType(type);
    
Methods Summary
public booleancontains(double px, double py)

        // Normalize point
        double nx = (px - getX()) / getWidth() - 0.5;
        double ny = (py - getY()) / getHeight() - 0.5;

        if ((nx * nx + ny * ny) > 0.25) {
            return false;
        }

        double extent = getAngleExtent();
        double absExtent = Math.abs(extent);
        if (absExtent >= 360.0) {
            return true;
        }

        boolean containsAngle = containsAngle(Math.toDegrees(-Math.atan2(ny, nx)));
        if (type == PIE) {
            return containsAngle;
        }
        if (absExtent <= 180.0 && !containsAngle) {
            return false;
        }

        Line2D l = new Line2D.Double(getStartPoint(), getEndPoint());
        int ccw1 = l.relativeCCW(px, py);
        int ccw2 = l.relativeCCW(getCenterX(), getCenterY());
        return ccw1 == 0 || ccw2 == 0 || ((ccw1 + ccw2) == 0 ^ absExtent > 180.0);
    
public booleancontains(double rx, double ry, double rw, double rh)


        if (!(contains(rx, ry) && contains(rx + rw, ry) && contains(rx + rw, ry + rh) && contains(
                rx, ry + rh))) {
            return false;
        }

        double absExtent = Math.abs(getAngleExtent());
        if (type != PIE || absExtent <= 180.0 || absExtent >= 360.0) {
            return true;
        }

        Rectangle2D r = new Rectangle2D.Double(rx, ry, rw, rh);

        double cx = getCenterX();
        double cy = getCenterY();
        if (r.contains(cx, cy)) {
            return false;
        }

        Point2D p1 = getStartPoint();
        Point2D p2 = getEndPoint();

        return !r.intersectsLine(cx, cy, p1.getX(), p1.getY())
                && !r.intersectsLine(cx, cy, p2.getX(), p2.getY());
    
public booleancontains(java.awt.geom.Rectangle2D rect)

        return contains(rect.getX(), rect.getY(), rect.getWidth(), rect.getHeight());
    
public booleancontainsAngle(double angle)
Determines whether the given angle is contained in the span of the arc.

param
angle the angle to test in degrees.
return
true, if the given angle is between the start angle and the end angle of the arc.

        double extent = getAngleExtent();
        if (extent >= 360.0) {
            return true;
        }
        angle = getNormAngle(angle);
        double a1 = getNormAngle(getAngleStart());
        double a2 = a1 + extent;
        if (a2 > 360.0) {
            return angle >= a1 || angle <= a2 - 360.0;
        }
        if (a2 < 0.0) {
            return angle >= a2 + 360.0 || angle <= a1;
        }
        return extent > 0.0 ? a1 <= angle && angle <= a2 : a2 <= angle && angle <= a1;
    
public abstract doublegetAngleExtent()
Gets the width angle.

return
the width angle.

public abstract doublegetAngleStart()
Gets the start angle.

return
the start angle.

public intgetArcType()
Gets the arc type, either {@link Arc2D#OPEN}, {@link Arc2D#CHORD}, or {@link Arc2D#PIE}.

return
the arc type.

        return type;
    
public java.awt.geom.Rectangle2DgetBounds2D()

        if (isEmpty()) {
            return makeBounds(getX(), getY(), getWidth(), getHeight());
        }
        double rx1 = getX();
        double ry1 = getY();
        double rx2 = rx1 + getWidth();
        double ry2 = ry1 + getHeight();

        Point2D p1 = getStartPoint();
        Point2D p2 = getEndPoint();

        double bx1 = containsAngle(180.0) ? rx1 : Math.min(p1.getX(), p2.getX());
        double by1 = containsAngle(90.0) ? ry1 : Math.min(p1.getY(), p2.getY());
        double bx2 = containsAngle(0.0) ? rx2 : Math.max(p1.getX(), p2.getX());
        double by2 = containsAngle(270.0) ? ry2 : Math.max(p1.getY(), p2.getY());

        if (type == PIE) {
            double cx = getCenterX();
            double cy = getCenterY();
            bx1 = Math.min(bx1, cx);
            by1 = Math.min(by1, cy);
            bx2 = Math.max(bx2, cx);
            by2 = Math.max(by2, cy);
        }
        return makeBounds(bx1, by1, bx2 - bx1, by2 - by1);
    
public java.awt.geom.Point2DgetEndPoint()
Gets the end point of the arc as a Point2D.

return
the end point of the curved arc segment.

        double a = Math.toRadians(getAngleStart() + getAngleExtent());
        return new Point2D.Double(getX() + (1.0 + Math.cos(a)) * getWidth() / 2.0, getY()
                + (1.0 - Math.sin(a)) * getHeight() / 2.0);
    
doublegetNormAngle(double angle)
Normalizes the angle by removing extra winding (past 360 degrees) and placing it in the positive degree range.

param
angle the source angle in degrees.
return
an angle between 0 and 360 degrees which corresponds to the same direction vector as the source angle.

        double n = Math.floor(angle / 360.0);
        return angle - n * 360.0;
    
public java.awt.geom.PathIteratorgetPathIterator(java.awt.geom.AffineTransform at)

        return new Iterator(this, at);
    
public java.awt.geom.Point2DgetStartPoint()
Gets the start point of the arc as a Point2D.

return
the start point of the curved arc segment.

        double a = Math.toRadians(getAngleStart());
        return new Point2D.Double(getX() + (1.0 + Math.cos(a)) * getWidth() / 2.0, getY()
                + (1.0 - Math.sin(a)) * getHeight() / 2.0);
    
public booleanintersects(double rx, double ry, double rw, double rh)


        if (isEmpty() || rw <= 0.0 || rh <= 0.0) {
            return false;
        }

        // Check: Does arc contain rectangle's points
        if (contains(rx, ry) || contains(rx + rw, ry) || contains(rx, ry + rh)
                || contains(rx + rw, ry + rh)) {
            return true;
        }

        double cx = getCenterX();
        double cy = getCenterY();
        Point2D p1 = getStartPoint();
        Point2D p2 = getEndPoint();
        Rectangle2D r = new Rectangle2D.Double(rx, ry, rw, rh);

        // Check: Does rectangle contain arc's points
        if (r.contains(p1) || r.contains(p2) || (type == PIE && r.contains(cx, cy))) {
            return true;
        }

        if (type == PIE) {
            if (r.intersectsLine(p1.getX(), p1.getY(), cx, cy)
                    || r.intersectsLine(p2.getX(), p2.getY(), cx, cy)) {
                return true;
            }
        } else {
            if (r.intersectsLine(p1.getX(), p1.getY(), p2.getX(), p2.getY())) {
                return true;
            }
        }

        // Nearest rectangle point
        double nx = cx < rx ? rx : (cx > rx + rw ? rx + rw : cx);
        double ny = cy < ry ? ry : (cy > ry + rh ? ry + rh : cy);
        return contains(nx, ny);
    
protected abstract java.awt.geom.Rectangle2DmakeBounds(double x, double y, double width, double height)
Takes the double-valued data and creates the corresponding Rectangle2D object with values either of type float or of type double depending on whether this Arc2D instance is of type Float or Double.

param
x the x coordinate of the upper left corner of the bounding rectangle.
param
y the y coordinate of the upper left corner of the bounding rectangle.
param
width the width of the bounding rectangle.
param
height the height of the bounding rectangle.
return
the corresponding Rectangle2D object.

public abstract voidsetAngleExtent(double extent)
Sets the width angle.

param
extent the new width angle.

public voidsetAngleStart(java.awt.geom.Point2D point)
Sets a new start angle to be the angle given by the the vector from the current center point to the specified point.

param
point the point that determines the new start angle.

        double angle = Math.atan2(point.getY() - getCenterY(), point.getX() - getCenterX());
        setAngleStart(getNormAngle(-Math.toDegrees(angle)));
    
public abstract voidsetAngleStart(double start)
Sets the start angle.

param
start the new start angle.

public voidsetAngles(double x1, double y1, double x2, double y2)
Sets the angles in terms of vectors from the current arc center to the points (x1, y1) and (x2, y2). The start angle is given by the vector from the current center to the point (x1, y1) and the end angle is given by the vector from the center to the point (x2, y2).

param
x1 the x coordinate of the point whose vector from the center point determines the new start angle of the arc.
param
y1 the y coordinate of the point whose vector from the center point determines the new start angle of the arc.
param
x2 the x coordinate of the point whose vector from the center point determines the new end angle of the arc.
param
y2 the y coordinate of the point whose vector from the center point determines the new end angle of the arc.

        double cx = getCenterX();
        double cy = getCenterY();
        double a1 = getNormAngle(-Math.toDegrees(Math.atan2(y1 - cy, x1 - cx)));
        double a2 = getNormAngle(-Math.toDegrees(Math.atan2(y2 - cy, x2 - cx)));
        a2 -= a1;
        if (a2 <= 0.0) {
            a2 += 360.0;
        }
        setAngleStart(a1);
        setAngleExtent(a2);
    
public voidsetAngles(java.awt.geom.Point2D p1, java.awt.geom.Point2D p2)
Sets the angles in terms of vectors from the current arc center to the points p1 and p2. The start angle is given by the vector from the current center to the point p1 and the end angle is given by the vector from the center to the point p2.

param
p1 the point whose vector from the center point determines the new start angle of the arc.
param
p2 the point whose vector from the center point determines the new end angle of the arc.

        setAngles(p1.getX(), p1.getY(), p2.getX(), p2.getY());
    
public voidsetArc(java.awt.geom.Point2D point, java.awt.geom.Dimension2D size, double start, double extent, int type)
Sets the data that defines the arc.

param
point the upper left corner of the bounding rectangle.
param
size the size of the bounding rectangle.
param
start the start angle of the arc in degrees.
param
extent the angle width of the arc in degrees.
param
type the closure type, either {@link Arc2D#OPEN}, {@link Arc2D#CHORD}, or {@link Arc2D#PIE}.

        setArc(point.getX(), point.getY(), size.getWidth(), size.getHeight(), start, extent, type);
    
public voidsetArc(java.awt.geom.Rectangle2D rect, double start, double extent, int type)
Sets the data that defines the arc.

param
rect the arc's bounding rectangle.
param
start the start angle of the arc in degrees.
param
extent the angle width of the arc in degrees.
param
type the closure type, either {@link Arc2D#OPEN}, {@link Arc2D#CHORD}, or {@link Arc2D#PIE}.

        setArc(rect.getX(), rect.getY(), rect.getWidth(), rect.getHeight(), start, extent, type);
    
public voidsetArc(java.awt.geom.Arc2D arc)
Sets the data that defines the arc by copying it from another Arc2D.

param
arc the arc whose data is copied into this arc.

        setArc(arc.getX(), arc.getY(), arc.getWidth(), arc.getHeight(), arc.getAngleStart(), arc
                .getAngleExtent(), arc.getArcType());
    
public abstract voidsetArc(double x, double y, double width, double height, double start, double extent, int type)
Sets the data values that define the arc.

param
x the x coordinate of the upper left corner of the rectangle that contains the arc.
param
y the y coordinate of the upper left corner of the rectangle that contains the arc.
param
width the width of the rectangle that contains the arc.
param
height the height of the rectangle that contains the arc.
param
start the start angle of the arc in degrees.
param
extent the width angle of the arc in degrees.
param
type the type of the new Arc2D, either {@link Arc2D#OPEN}, {@link Arc2D#CHORD}, or {@link Arc2D#PIE}.

public voidsetArcByCenter(double x, double y, double radius, double start, double extent, int type)
Sets the data for a circular arc by giving its center and radius.

param
x the x coordinate of the center of the circle.
param
y the y coordinate of the center of the circle.
param
radius the radius of the circle.
param
start the start angle of the arc in degrees.
param
extent the angle width of the arc in degrees.
param
type the closure type, either {@link Arc2D#OPEN}, {@link Arc2D#CHORD}, or {@link Arc2D#PIE}.

        setArc(x - radius, y - radius, radius * 2.0, radius * 2.0, start, extent, type);
    
public voidsetArcByTangent(java.awt.geom.Point2D p1, java.awt.geom.Point2D p2, java.awt.geom.Point2D p3, double radius)
Sets the arc data for a circular arc based on two tangent lines and the radius. The two tangent lines are the lines from p1 to p2 and from p2 to p3, which determine a unique circle with the given radius. The start and end points of the arc are the points where the circle touches the two lines, and the arc itself is the shorter of the two circle segments determined by the two points (in other words, it is the piece of the circle that is closer to the lines' intersection point p2 and forms a concave shape with the segments from p1 to p2 and from p2 to p3).

param
p1 a point which determines one of the two tangent lines (with p2).
param
p2 the point of intersection of the two tangent lines.
param
p3 a point which determines one of the two tangent lines (with p2).
param
radius the radius of the circular arc.

        // Used simple geometric calculations of arc center, radius and angles
        // by tangents
        double a1 = -Math.atan2(p1.getY() - p2.getY(), p1.getX() - p2.getX());
        double a2 = -Math.atan2(p3.getY() - p2.getY(), p3.getX() - p2.getX());
        double am = (a1 + a2) / 2.0;
        double ah = a1 - am;
        double d = radius / Math.abs(Math.sin(ah));
        double x = p2.getX() + d * Math.cos(am);
        double y = p2.getY() - d * Math.sin(am);
        ah = ah >= 0.0 ? Math.PI * 1.5 - ah : Math.PI * 0.5 - ah;
        a1 = getNormAngle(Math.toDegrees(am - ah));
        a2 = getNormAngle(Math.toDegrees(am + ah));
        double delta = a2 - a1;
        if (delta <= 0.0) {
            delta += 360.0;
        }
        setArcByCenter(x, y, radius, a1, delta, type);
    
public voidsetArcType(int type)
Sets the arc type, either {@link Arc2D#OPEN}, {@link Arc2D#CHORD}, or {@link Arc2D#PIE}.

param
type the new arc type.

        if (type != OPEN && type != CHORD && type != PIE) {
            // awt.205=Invalid type of Arc: {0}
            throw new IllegalArgumentException(Messages.getString("awt.205", type)); //$NON-NLS-1$
        }
        this.type = type;
    
public voidsetFrame(double x, double y, double width, double height)

        setArc(x, y, width, height, getAngleStart(), getAngleExtent(), type);