FileDocCategorySizeDatePackage
Arc2D.javaAPI DocJava SE 5 API42434Fri Aug 26 14:56:52 BST 2005java.awt.geom

Arc2D

public abstract class Arc2D extends RectangularShape
Arc2D is the abstract superclass for all objects that store a 2D arc defined by a bounding rectangle, start angle, angular extent (length of the arc), and a closure type (OPEN, CHORD, or PIE).

The bounding rectangle defines the outer boundary of the full ellipse of which this arc is a partial section. The angles are specified relative to the non-square extents of the bounding rectangle such that 45 degrees always falls on the line from the center of the ellipse to the upper right corner of the bounding rectangle. As a result, if the bounding rectangle is noticeably longer along one axis than the other, the angles to the start and end of the arc segment will be skewed farther along the longer axis of the bounds.

The actual storage representation of the coordinates is left to the subclass.

version
10 Feb 1997
author
Jim Graham

Fields Summary
public static final int
OPEN
The closure type for an open arc with no path segments connecting the two ends of the arc segment.
public static final int
CHORD
The closure type for an arc closed by drawing a straight line segment from the start of the arc segment to the end of the arc segment.
public static final int
PIE
The closure type for an arc closed by drawing straight line segments from the start of the arc segment to the center of the full ellipse and from that point to the end of the arc segment.
private int
type
Constructors Summary
protected Arc2D(int type)
This is an abstract class that cannot be instantiated directly. Type-specific implementation subclasses are available for instantiation and provide a number of formats for storing the information necessary to satisfy the various accessor methods below.

param
type The closure type of this arc: {@link #OPEN OPEN}, {@link #CHORD CHORD}, or {@link #PIE PIE}.
see
java.awt.geom.Arc2D.Float
see
java.awt.geom.Arc2D.Double

	setArcType(type);
    
Methods Summary
public booleancontains(double x, double y)
Determines whether or not the specified point is inside the boundary of the arc.

param
x, y The coordinates of the point to test. (Specified in double precision.)
return
true if the point lies within the bound of the arc, false if the point lies outside of the arc's bounds.

	// Normalize the coordinates compared to the ellipse
	// having a center at 0,0 and a radius of 0.5.
	double ellw = getWidth();
	if (ellw <= 0.0) {
	    return false;
	}
	double normx = (x - getX()) / ellw - 0.5;
	double ellh = getHeight();
	if (ellh <= 0.0) {
	    return false;
	}
	double normy = (y - getY()) / ellh - 0.5;
	double distSq = (normx * normx + normy * normy);
	if (distSq >= 0.25) {
	    return false;
	}
	double angExt = Math.abs(getAngleExtent());
	if (angExt >= 360.0) {
	    return true;
	}
	boolean inarc = containsAngle(-Math.toDegrees(Math.atan2(normy,
								 normx)));
	if (type == PIE) {
	    return inarc;
	}
	// CHORD and OPEN behave the same way
	if (inarc) {
	    if (angExt >= 180.0) {
		return true;
	    }
	    // point must be outside the "pie triangle"
	} else {
	    if (angExt <= 180.0) {
		return false;
	    }
	    // point must be inside the "pie triangle"
	}
	// The point is inside the pie triangle iff it is on the same
	// side of the line connecting the ends of the arc as the center.
	double angle = Math.toRadians(-getAngleStart());
	double x1 = Math.cos(angle);
	double y1 = Math.sin(angle);
	angle += Math.toRadians(-getAngleExtent());
	double x2 = Math.cos(angle);
	double y2 = Math.sin(angle);
	boolean inside = (Line2D.relativeCCW(x1, y1, x2, y2, 2*normx, 2*normy) *
			  Line2D.relativeCCW(x1, y1, x2, y2, 0, 0) >= 0);
	return inarc ? !inside : inside;
    
public booleancontains(double x, double y, double w, double h)
Determine whether or not the interior of the arc entirely contains the specified rectangle.

param
x, y The coordinates of the rectangle's upper left corner. (Specified in double precision.)
param
w The width of the rectangle. (Specified in double precision.)
param
h The height of the rectangle. (Specified in double precision.)
return
true if the arc contains the rectangle, false if the arc doesn't contain the rectangle.

	return contains(x, y, w, h, null);
    
public booleancontains(java.awt.geom.Rectangle2D r)
Determine whether or not the interior of the arc entirely contains the specified rectangle.

param
r The Rectangle2D to test.
return
true if the arc contains the rectangle, false if the arc doesn't contain the rectangle.

	return contains(r.getX(), r.getY(), r.getWidth(), r.getHeight(), r);
    
private booleancontains(double x, double y, double w, double h, java.awt.geom.Rectangle2D origrect)

	if (!(contains(x, y) &&
	      contains(x + w, y) &&
	      contains(x, y + h) &&
	      contains(x + w, y + h))) {
	    return false;
	}
	// If the shape is convex then we have done all the testing
	// we need.  Only PIE arcs can be concave and then only if
	// the angular extents are greater than 180 degrees.
	if (type != PIE || Math.abs(getAngleExtent()) <= 180.0) {
	    return true;
	}
	// For a PIE shape we have an additional test for the case where
	// the angular extents are greater than 180 degrees and all four
	// rectangular corners are inside the shape but one of the
	// rectangle edges spans across the "missing wedge" of the arc.
	// We can test for this case by checking if the rectangle intersects
	// either of the pie angle segments.
	if (origrect == null) {
	    origrect = new Rectangle2D.Double(x, y, w, h);
	}
	double halfW = getWidth() / 2.0;
	double halfH = getHeight() / 2.0;
	double xc = getX() + halfW;
	double yc = getY() + halfH;
	double angle = Math.toRadians(-getAngleStart());
	double xe = xc + halfW * Math.cos(angle);
	double ye = yc + halfH * Math.sin(angle);
	if (origrect.intersectsLine(xc, yc, xe, ye)) {
	    return false;
	}
	angle += Math.toRadians(-getAngleExtent());
	xe = xc + halfW * Math.cos(angle);
	ye = yc + halfH * Math.sin(angle);
	return !origrect.intersectsLine(xc, yc, xe, ye);
    
public booleancontainsAngle(double angle)
Determines whether or not the specified angle is within the angular extents of the arc.

param
angle The angle to test. (Specified in double precision.)
return
true if the arc contains the angle, false if the arc doesn't contain the angle.

	double angExt = getAngleExtent();
	boolean backwards = (angExt < 0.0);
	if (backwards) {
	    angExt = -angExt;
	}
	if (angExt >= 360.0) {
	    return true;
	}
	angle = normalizeDegrees(angle) - normalizeDegrees(getAngleStart());
	if (backwards) {
	    angle = -angle;
	}
	if (angle < 0.0) {
	    angle += 360.0;
	}

      
	return (angle >= 0.0) && (angle < angExt);
    
public abstract doublegetAngleExtent()
Returns the angular extent of the arc.

return
A double value that represents the angular extent of the arc in degrees.
see
#setAngleExtent

public abstract doublegetAngleStart()
Returns the starting angle of the arc.

return
A double value that represents the starting angle of the arc in degrees.
see
#setAngleStart

public intgetArcType()
Returns the arc closure type of the arc: {@link #OPEN OPEN}, {@link #CHORD CHORD}, or {@link #PIE PIE}.

return
One of the integer constant closure types defined in this class.
see
#setArcType

	return type;
    
public java.awt.geom.Rectangle2DgetBounds2D()
Returns the high-precision bounding box of the arc. The bounding box contains only the part of this Arc2D that is in between the starting and ending angles and contains the pie wedge, if this Arc2D has a PIE closure type.

This method differs from the {@link RectangularShape#getBounds() getBounds} in that the getBounds method only returns the bounds of the enclosing ellipse of this Arc2D without considering the starting and ending angles of this Arc2D.

return
the Rectangle2D that represents the arc's bounding box.

	if (isEmpty()) {
	    return makeBounds(getX(), getY(), getWidth(), getHeight());
	}
	double x1, y1, x2, y2;
	if (getArcType() == PIE) {
	    x1 = y1 = x2 = y2 = 0.0;
	} else {
	    x1 = y1 = 1.0;
	    x2 = y2 = -1.0;
	}
	double angle = 0.0;
	for (int i = 0; i < 6; i++) {
	    if (i < 4) {
		// 0-3 are the four quadrants
		angle += 90.0;
		if (!containsAngle(angle)) {
		    continue;
		}
	    } else if (i == 4) {
		// 4 is start angle
		angle = getAngleStart();
	    } else {
		// 5 is end angle
		angle += getAngleExtent();
	    }
	    double rads = Math.toRadians(-angle);
	    double xe = Math.cos(rads);
	    double ye = Math.sin(rads);
	    x1 = Math.min(x1, xe);
	    y1 = Math.min(y1, ye);
	    x2 = Math.max(x2, xe);
	    y2 = Math.max(y2, ye);
	}
	double w = getWidth();
	double h = getHeight();
	x2 = (x2 - x1) * 0.5 * w;
	y2 = (y2 - y1) * 0.5 * h;
	x1 = getX() + (x1 * 0.5 + 0.5) * w;
	y1 = getY() + (y1 * 0.5 + 0.5) * h;
	return makeBounds(x1, y1, x2, y2);
    
public java.awt.geom.Point2DgetEndPoint()
Returns the ending point of the arc. This point is the intersection of the ray from the center defined by the starting angle plus the angular extent of the arc and the elliptical boundary of the arc.

return
A Point2D object representing the x,y coordinates of the ending point of the arc.

	double angle = Math.toRadians(-getAngleStart() - getAngleExtent());
	double x = getX() + (Math.cos(angle) * 0.5 + 0.5) * getWidth();
	double y = getY() + (Math.sin(angle) * 0.5 + 0.5) * getHeight();
	return new Point2D.Double(x, y);
    
public java.awt.geom.PathIteratorgetPathIterator(java.awt.geom.AffineTransform at)
Returns an iteration object that defines the boundary of the arc. This iterator is multithread safe. Arc2D guarantees that modifications to the geometry of the arc do not affect any iterations of that geometry that are already in process.

param
at an optional AffineTransform to be applied to the coordinates as they are returned in the iteration, or null if the untransformed coordinates are desired.
return
A PathIterator that defines the arc's boundary.

	return new ArcIterator(this, at);
    
public java.awt.geom.Point2DgetStartPoint()
Returns the starting point of the arc. This point is the intersection of the ray from the center defined by the starting angle and the elliptical boundary of the arc.

return
A Point2D object representing the x,y coordinates of the starting point of the arc.

	double angle = Math.toRadians(-getAngleStart());
	double x = getX() + (Math.cos(angle) * 0.5 + 0.5) * getWidth();
	double y = getY() + (Math.sin(angle) * 0.5 + 0.5) * getHeight();
	return new Point2D.Double(x, y);
    
public booleanintersects(double x, double y, double w, double h)
Determines whether or not the interior of the arc intersects the interior of the specified rectangle.

param
x, y The coordinates of the rectangle's upper left corner. (Specified in double precision.)
param
w The width of the rectangle. (Specified in double precision.)
param
h The height of the rectangle. (Specified in double precision.)
return
true if the arc intersects the rectangle, false if the arc doesn't intersect the rectangle.


	double aw = getWidth();
	double ah = getHeight();

	if ( w <= 0 || h <= 0 || aw <= 0 || ah <= 0 ) {
	    return false;
	}
	double ext = getAngleExtent();
	if (ext == 0) {
	    return false;
	}

	double ax  = getX();
	double ay  = getY();
	double axw = ax + aw;
	double ayh = ay + ah;
	double xw  = x + w;
	double yh  = y + h;

	// check bbox
	if (x >= axw || y >= ayh || xw <= ax || yh <= ay) {
	    return false;
	}

	// extract necessary data
	double axc = getCenterX();
	double ayc = getCenterY();
	Point2D sp = getStartPoint();
	Point2D ep = getEndPoint();
	double sx = sp.getX();
	double sy = sp.getY();
	double ex = ep.getX();
	double ey = ep.getY();

	/*
	 * Try to catch rectangles that intersect arc in areas
	 * outside of rectagle with left top corner coordinates
	 * (min(center x, start point x, end point x),
	 *  min(center y, start point y, end point y))
	 * and rigth bottom corner coordinates
	 * (max(center x, start point x, end point x),
	 *  max(center y, start point y, end point y)).
	 * So we'll check axis segments outside of rectangle above.
	 */
	if (ayc >= y && ayc <= yh) { // 0 and 180
	    if ((sx < xw && ex < xw && axc < xw &&
	         axw > x && containsAngle(0)) ||
	        (sx > x && ex > x && axc > x &&
	         ax < xw && containsAngle(180))) {
		return true;
	    }
	}
	if (axc >= x && axc <= xw) { // 90 and 270
	    if ((sy > y && ey > y && ayc > y &&
	         ay < yh && containsAngle(90)) ||
	        (sy < yh && ey < yh && ayc < yh &&
	         ayh > y && containsAngle(270))) {
		return true;
	    }
	}

	/*
	 * For PIE we should check intersection with pie slices;
	 * also we should do the same for arcs with extent is greater
	 * than 180, because we should cover case of rectangle, which
	 * situated between center of arc and chord, but does not
	 * intersect the chord.
	 */
	Rectangle2D rect = new Rectangle2D.Double(x, y, w, h);
	if (type == PIE || Math.abs(ext) > 180) {
	    // for PIE: try to find intersections with pie slices
	    if (rect.intersectsLine(axc, ayc, sx, sy) ||
		rect.intersectsLine(axc, ayc, ex, ey)) {
		return true;
	    }
	} else {
	    // for CHORD and OPEN: try to find intersections with chord
	    if (rect.intersectsLine(sx, sy, ex, ey)) {
		return true;
	    }
	}

	// finally check the rectangle corners inside the arc
	if (contains(x, y) || contains(x + w, y) ||
	    contains(x, y + h) || contains(x + w, y + h)) {
	    return true;
	}

	return false;
    
protected abstract java.awt.geom.Rectangle2DmakeBounds(double x, double y, double w, double h)
Constructs a Rectangle2D of the appropriate precision to hold the parameters calculated to be the bounding box of this arc.

param
x, y The coordinates of the upper left corner of the bounding box. (Specified in double precision.)
param
w The width of the bounding box. (Specified in double precision.)
param
h The height of the bounding box. (Specified in double precision.)
return
a Rectangle2D that is the bounding box of this arc.

static doublenormalizeDegrees(double angle)

	if (angle > 180.0) {
	    if (angle <= (180.0 + 360.0)) {
		angle = angle - 360.0;
	    } else {
		angle = Math.IEEEremainder(angle, 360.0);
		// IEEEremainder can return -180 here for some input values...
		if (angle == -180.0) {
		    angle = 180.0;
		}
	    }
	} else if (angle <= -180.0) {
	    if (angle > (-180.0 - 360.0)) {
		angle = angle + 360.0;
	    } else {
		angle = Math.IEEEremainder(angle, 360.0);
		// IEEEremainder can return -180 here for some input values...
		if (angle == -180.0) {
		    angle = 180.0;
		}
	    }
	}
	return angle;
    
public abstract voidsetAngleExtent(double angExt)
Sets the angular extent of this arc to the specified double value.

param
angExt The angular extent of the arc in degrees.
see
#getAngleExtent

public abstract voidsetAngleStart(double angSt)
Sets the starting angle of this arc to the specified double value.

param
angSt The starting angle of the arc in degrees.
see
#getAngleStart

public voidsetAngleStart(java.awt.geom.Point2D p)
Sets the starting angle of this arc to the angle that the specified point defines relative to the center of this arc. The angular extent of the arc will remain the same.

param
p The Point2D that defines the starting angle.
see
#getAngleStart

	// Bias the dx and dy by the height and width of the oval.
	double dx = getHeight() * (p.getX() - getCenterX());
	double dy = getWidth() * (p.getY() - getCenterY());
	setAngleStart(-Math.toDegrees(Math.atan2(dy, dx)));
    
public voidsetAngles(double x1, double y1, double x2, double y2)
Sets the starting angle and angular extent of this arc using two sets of coordinates. The first set of coordinates is used to determine the angle of the starting point relative to the arc's center. The second set of coordinates is used to determine the angle of the end point relative to the arc's center. The arc will always be non-empty and extend counterclockwise from the first point around to the second point.

param
x1, y1 The coordinates of the arc's starting point.
param
x2, y2 The coordinates of the arc's ending point.

	double x = getCenterX();
	double y = getCenterY();
	double w = getWidth();
	double h = getHeight();
	// Note: reversing the Y equations negates the angle to adjust
	// for the upside down coordinate system.
	// Also we should bias atans by the height and width of the oval.
	double ang1 = Math.atan2(w * (y - y1), h * (x1 - x));
	double ang2 = Math.atan2(w * (y - y2), h * (x2 - x));
	ang2 -= ang1;
	if (ang2 <= 0.0) {
	    ang2 += Math.PI * 2.0;
	}
	setAngleStart(Math.toDegrees(ang1));
	setAngleExtent(Math.toDegrees(ang2));
    
public voidsetAngles(java.awt.geom.Point2D p1, java.awt.geom.Point2D p2)
Sets the starting angle and angular extent of this arc using two points. The first point is used to determine the angle of the starting point relative to the arc's center. The second point is used to determine the angle of the end point relative to the arc's center. The arc will always be non-empty and extend counterclockwise from the first point around to the second point.

param
p1 The Point2D that defines the arc's starting point.
param
p2 The Point2D that defines the arc's ending point.

	setAngles(p1.getX(), p1.getY(), p2.getX(), p2.getY());
    
public voidsetArc(java.awt.geom.Arc2D a)
Sets this arc to be the same as the specified arc.

param
a The Arc2D to use to set the arc's values.

	setArc(a.getX(), a.getY(), a.getWidth(), a.getHeight(),
	       a.getAngleStart(), a.getAngleExtent(), a.type);
    
public abstract voidsetArc(double x, double y, double w, double h, double angSt, double angExt, int closure)
Sets the location, size, angular extents, and closure type of this arc to the specified double values.

param
x, y The coordinates of the upper left corner of the arc.
param
w The overall width of the full ellipse of which this arc is a partial section.
param
h The overall height of the full ellipse of which this arc is a partial section.
param
angSt The starting angle of the arc in degrees.
param
angExt The angular extent of the arc in degrees.
param
closure The closure type for the arc: {@link #OPEN OPEN}, {@link #CHORD CHORD}, or {@link #PIE PIE}.

public voidsetArc(java.awt.geom.Point2D loc, java.awt.geom.Dimension2D size, double angSt, double angExt, int closure)
Sets the location, size, angular extents, and closure type of this arc to the specified values.

param
loc The Point2D representing the coordinates of the upper left corner of the arc.
param
size The Dimension2D representing the width and height of the full ellipse of which this arc is a partial section.
param
angSt The starting angle of the arc in degrees. (Specified in double precision.)
param
angExt The angular extent of the arc in degrees. (Specified in double precision.)
param
closure The closure type for the arc: {@link #OPEN OPEN}, {@link #CHORD CHORD}, or {@link #PIE PIE}.

	setArc(loc.getX(), loc.getY(), size.getWidth(), size.getHeight(),
	       angSt, angExt, closure);
    
public voidsetArc(java.awt.geom.Rectangle2D rect, double angSt, double angExt, int closure)
Sets the location, size, angular extents, and closure type of this arc to the specified values.

param
rect The bounding rectangle that defines the outer boundary of the full ellipse of which this arc is a partial section.
param
angSt The starting angle of the arc in degrees. (Specified in double precision.)
param
angExt The angular extent of the arc in degrees. (Specified in double precision.)
param
closure The closure type for the arc: {@link #OPEN OPEN}, {@link #CHORD CHORD}, or {@link #PIE PIE}.

	setArc(rect.getX(), rect.getY(), rect.getWidth(), rect.getHeight(),
	       angSt, angExt, closure);
    
public voidsetArcByCenter(double x, double y, double radius, double angSt, double angExt, int closure)
Sets the position, bounds, angular extents, and closure type of this arc to the specified values. The arc is defined by a center point and a radius rather than a bounding box for the full ellipse.

param
x, y The coordinates of the center of the arc. (Specified in double precision.)
param
radius The radius of the arc. (Specified in double precision.)
param
angSt The starting angle of the arc in degrees. (Specified in double precision.)
param
angExt The angular extent of the arc in degrees. (Specified in double precision.)
param
closure The closure type for the arc: {@link #OPEN OPEN}, {@link #CHORD CHORD}, or {@link #PIE PIE}.

	setArc(x - radius, y - radius, radius * 2.0, radius * 2.0,
	       angSt, angExt, closure);
    
public voidsetArcByTangent(java.awt.geom.Point2D p1, java.awt.geom.Point2D p2, java.awt.geom.Point2D p3, double radius)
Sets the position, bounds, and angular extents of this arc to the specified value. The starting angle of the arc is tangent to the line specified by points (p1, p2), the ending angle is tangent to the line specified by points (p2, p3), and the arc has the specified radius.

param
p1 The first point that defines the arc. The starting angle of the arc is tangent to the line specified by points (p1, p2).
param
p2 The second point that defines the arc. The starting angle of the arc is tangent to the line specified by points (p1, p2). The ending angle of the arc is tangent to the line specified by points (p2, p3).
param
p3 The third point that defines the arc. The ending angle of the arc is tangent to the line specified by points (p2, p3).
param
radius The radius of the arc. (Specified in double precision.)

	double ang1 = Math.atan2(p1.getY() - p2.getY(),
				 p1.getX() - p2.getX());
	double ang2 = Math.atan2(p3.getY() - p2.getY(),
				 p3.getX() - p2.getX());
	double diff = ang2 - ang1;
	if (diff > Math.PI) {
	    ang2 -= Math.PI * 2.0;
	} else if (diff < -Math.PI) {
	    ang2 += Math.PI * 2.0;
	}
	double bisect = (ang1 + ang2) / 2.0;
	double theta = Math.abs(ang2 - bisect);
	double dist = radius / Math.sin(theta);
	double x = p2.getX() + dist * Math.cos(bisect);
	double y = p2.getY() + dist * Math.sin(bisect);
	// REMIND: This needs some work...
	if (ang1 < ang2) {
	    ang1 -= Math.PI / 2.0;
	    ang2 += Math.PI / 2.0;
	} else {
	    ang1 += Math.PI / 2.0;
	    ang2 -= Math.PI / 2.0;
	}
	ang1 = Math.toDegrees(-ang1);
	ang2 = Math.toDegrees(-ang2);
	diff = ang2 - ang1;
	if (diff < 0) {
	    diff += 360;
	} else {
	    diff -= 360;
	}
	setArcByCenter(x, y, radius, ang1, diff, type);
    
public voidsetArcType(int type)
Sets the closure type of this arc to the specified value: OPEN, CHORD, or PIE.

param
type The integer constant that represents the closure type of this arc: {@link #OPEN}, {@link #CHORD}, or {@link #PIE}.
throws
IllegalArgumentException if type is not 0, 1, or 2.+
see
#getArcType

	if (type < OPEN || type > PIE) {
	    throw new IllegalArgumentException("invalid type for Arc: "+type);
	}
	this.type = type;
    
public voidsetFrame(double x, double y, double w, double h)
Sets the location and size of the outer bounds of this arc to the specified values.

param
x, y The coordinates of the upper left corner of the arc's bounding box. (Specified in double precision.)
param
w The width of the arc's bounding box. (Specified in double precision.)
param
h The height of the arc's bounding box. (Specified in double precision.)

	setArc(x, y, w, h, getAngleStart(), getAngleExtent(), type);