home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Java 1.2 How-To
/
JavaHowTo.iso
/
3rdParty
/
jbuilder
/
unsupported
/
JDK1.2beta3
/
SOURCE
/
SRC.ZIP
/
java
/
awt
/
geom
/
Arc2D.java
< prev
next >
Encoding:
Amiga
Atari
Commodore
DOS
FM Towns/JPY
Macintosh
Macintosh JP
NeXTSTEP
RISC OS/Acorn
UTF-8
Wrap
Java Source
|
1998-03-20
|
28.8 KB
|
1,038 lines
/*
* @(#)Arc2D.java 1.9 98/03/18
*
* Copyright 1997, 1998 by Sun Microsystems, Inc.,
* 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
* All rights reserved.
*
* This software is the confidential and proprietary information
* of Sun Microsystems, Inc. ("Confidential Information"). You
* shall not disclose such Confidential Information and shall use
* it only in accordance with the terms of the license agreement
* you entered into with Sun.
*/
package java.awt.geom;
/**
* An arc defined by a bounding rectangle, start angle and angular extent,
* and a closure type (one of 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 in 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.
* <p>
* This class is only the abstract superclass for all objects which
* store a 2D arc.
* The actual storage representation of the coordinates is left to
* the subclass.
*
* @version 10 Feb 1997
* @author Jim Graham
*/
public abstract class Arc2D extends RectangularShape {
/**
* The closure type for an open arc with no path segments
* connecting the two ends of the arc segment.
*/
public final static int OPEN = 0;
/**
* The closure type for an arc closed by drawing a straight
* line segment from the start to the end of the arc segment.
*/
public final static int CHORD = 1;
/**
* 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 back to the end of the arc segment.
*/
public final static int PIE = 2;
/**
* An arc specified in float precision,
*/
public static class Float extends Arc2D {
/**
* The x coordinate of the upper left corner of the arc.
*/
public float x;
/**
* The y coordinate of the upper left corner of the arc.
*/
public float y;
/**
* The overall width of the full ellipse (not considering the
* angular extents).
*/
public float width;
/**
* The overall height of the full ellipse (not considering the
* angular extents).
*/
public float height;
/**
* The starting angle of the arc in degrees.
*/
public float start;
/**
* The angular extent of the arc in degrees.
*/
public float extent;
/**
* Constructs a new OPEN arc, initialized to location (0, 0),
* size (0, 0), angular extents (start = 0, extent = 0).
*/
public Float() {
super(OPEN);
}
/**
* Constructs a new arc, initialized to location (0, 0),
* size (0, 0), angular extents (start = 0, extent = 0), and
* the specified closure type.
*/
public Float(int type) {
super(type);
}
/**
* Constructs a new arc, initialized to the specified location,
* size, angular extents, and closure type.
*/
public Float(float x, float y, float w, float h,
float start, float extent, int type) {
super(type);
this.x = x;
this.y = y;
this.width = w;
this.height = h;
this.start = start;
this.extent = extent;
}
/**
* Constructs a new arc, initialized to the specified location,
* size, angular extents, and closure type.
*/
public Float(Rectangle2D ellipseBounds,
float start, float extent, int type) {
super(type);
this.x = (float) ellipseBounds.getX();
this.y = (float) ellipseBounds.getY();
this.width = (float) ellipseBounds.getWidth();
this.height = (float) ellipseBounds.getHeight();
this.start = start;
this.extent = extent;
}
/**
* Returns the X coordinate of the upper left corner of the arc
* in double precision.
*/
public double getX() {
return (double) x;
}
/**
* Returns the Y coordinate of the upper left corner of the arc
* in double precision.
*/
public double getY() {
return (double) y;
}
/**
* Returns the overall width of the arc in double precision.
*/
public double getWidth() {
return (double) width;
}
/**
* Returns the overall height of the arc in double precision.
*/
public double getHeight() {
return (double) height;
}
/**
* Returns the starting angle of the arc (in degrees).
*/
public double getAngleStart() {
return (double) start;
}
/**
* Returns the arc length (angular extent) of the arc (in degrees).
*/
public double getAngleExtent() {
return (double) extent;
}
/**
* Determines whether the rectangular shape is empty.
*/
public boolean isEmpty() {
return (width <= 0.0 || height <= 0.0);
}
/**
* Sets the location, size, angular extents, and closure type of
* this arc to the specified double values.
*/
public void setArc(double x, double y, double w, double h,
double angSt, double angExt, int closure) {
this.setArcType(closure);
this.x = (float) x;
this.y = (float) y;
this.width = (float) w;
this.height = (float) h;
this.start = (float) angSt;
this.extent = (float) angExt;
}
/**
* Sets the starting angle of this arc to the specified double
* value (in degrees).
*/
public void setAngleStart(double angSt) {
this.start = (float) angSt;
}
/**
* Sets the angular extent of this arc to the specified double
* value (in degrees).
*/
public void setAngleExtent(double angExt) {
this.extent = (float) angExt;
}
/**
* Return the high precision bounding box of the shape.
*/
public Rectangle2D makeBounds(double x, double y, double w, double h) {
return new Rectangle2D.Float((float) x, (float) y,
(float) w, (float) h);
}
}
/**
* An arc specified in double precision,
*/
public static class Double extends Arc2D {
/**
* The x coordinate of the upper left corner of the arc.
*/
public double x;
/**
* The y coordinate of the upper left corner of the arc.
*/
public double y;
/**
* The overall width of the full ellipse (not considering the
* angular extents).
*/
public double width;
/**
* The overall height of the full ellipse (not considering the
* angular extents).
*/
public double height;
/**
* The starting angle of the arc in degrees.
*/
public double start;
/**
* The angular extent of the arc in degrees.
*/
public double extent;
/**
* Constructs a new OPEN arc, initialized to location (0, 0),
* size (0, 0), angular extents (start = 0, extent = 0).
*/
public Double() {
super(OPEN);
}
/**
* Constructs a new arc, initialized to location (0, 0),
* size (0, 0), angular extents (start = 0, extent = 0), and
* the specified closure type.
*/
public Double(int type) {
super(type);
}
/**
* Constructs a new arc, initialized to the specified location,
* size, angular extents, and closure type.
*/
public Double(double x, double y, double w, double h,
double start, double extent, int type) {
super(type);
this.x = x;
this.y = y;
this.width = w;
this.height = h;
this.start = start;
this.extent = extent;
}
/**
* Constructs a new arc, initialized to the specified location,
* size, angular extents, and closure type.
*/
public Double(Rectangle2D ellipseBounds,
double start, double extent, int type) {
super(type);
this.x = ellipseBounds.getX();
this.y = ellipseBounds.getY();
this.width = ellipseBounds.getWidth();
this.height = ellipseBounds.getHeight();
this.start = start;
this.extent = extent;
}
/**
* Returns the X coordinate of the upper left corner of the arc
* in double precision.
*/
public double getX() {
return x;
}
/**
* Returns the Y coordinate of the upper left corner of the arc
* in double precision.
*/
public double getY() {
return y;
}
/**
* Returns the overall width of the arc in double precision.
*/
public double getWidth() {
return width;
}
/**
* Returns the overall height of the arc in double precision.
*/
public double getHeight() {
return height;
}
/**
* Returns the starting angle of the arc (in degrees).
*/
public double getAngleStart() {
return start;
}
/**
* Returns the arc length (angular extent) of the arc (in degrees).
*/
public double getAngleExtent() {
return extent;
}
/**
* Determines whether the rectangular shape is empty.
*/
public boolean isEmpty() {
return (width <= 0.0 || height <= 0.0);
}
/**
* Sets the location, size, angular extents, and closure type of
* this arc to the specified double values.
*/
public void setArc(double x, double y, double w, double h,
double angSt, double angExt, int closure) {
this.setArcType(closure);
this.x = x;
this.y = y;
this.width = w;
this.height = h;
this.start = angSt;
this.extent = angExt;
}
/**
* Sets the starting angle of this arc to the specified double
* value (in degrees).
*/
public void setAngleStart(double angSt) {
this.start = angSt;
}
/**
* Sets the angular extent of this arc to the specified double
* value (in degrees).
*/
public void setAngleExtent(double angExt) {
this.extent = angExt;
}
/**
* Return the high precision bounding box of the shape.
*/
public Rectangle2D makeBounds(double x, double y, double w, double h) {
return new Rectangle2D.Double(x, y, w, h);
}
}
private int type;
/**
* Constructs a new arc of the specified closure type.
*/
protected Arc2D(int type) {
setArcType(type);
}
/**
* Returns the starting angle of the arc (in degrees).
*/
public abstract double getAngleStart();
/**
* Returns the arc length (angular extent) of the arc (in degrees).
*/
public abstract double getAngleExtent();
/**
* Returns the arc closure type of the arc.
* @see OPEN
* @see CHORD
* @see PIE
*/
public int getArcType() {
return type;
}
/**
* 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.
*/
public Point2D getStartPoint() {
double angle = 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);
}
/**
* 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.
*/
public Point2D getEndPoint() {
double angle = 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);
}
/**
* Sets the location, size, angular extents, and closure type of
* this arc to the specified double values.
*/
public abstract void setArc(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 Point and Dimension and double values.
*/
public void setArc(Point2D loc, Dimension2D size,
double angSt, double angExt, int closure) {
setArc(loc.getX(), loc.getY(), size.getWidth(), size.getHeight(),
angSt, angExt, closure);
}
/**
* Sets the location, size, angular extents, and closure type of
* this arc to the specified Rectangle and double values.
*/
public void setArc(Rectangle2D rect, double angSt, double angExt,
int closure) {
setArc(rect.getX(), rect.getY(), rect.getWidth(), rect.getHeight(),
angSt, angExt, closure);
}
/**
* Sets this arc to be the same as the specified Arc.
*/
public void setArc(Arc2D a) {
setArc(a.getX(), a.getY(), a.getWidth(), a.getHeight(),
a.getAngleStart(), a.getAngleExtent(), a.type);
}
/**
* Sets the position, bounds, angular extents, and closure type of
* this arc to the specified double values based on a center point
* and a radius rather than a bounding box for the full ellipse.
*/
public void setArcByCenter(double x, double y, double radius,
double angSt, double angExt, int closure) {
setArc(x - radius, y - radius, radius * 2.0, radius * 2.0,
angSt, angExt, closure);
}
/**
* Sets the position, bounds, and angular extents of this arc
* so that the starting angle is tangent to the line specified
* by points (p1, p2) and the ending angle is tangent to the
* line specified by points (p2, p3) with the given radius.
*/
public void setArcByTangent(Point2D p1, Point2D p2, Point2D p3,
double radius) {
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);
theta += Math.PI / 2.0;
if (diff < 0.0) {
theta = -theta;
}
ang1 = toDegrees(bisect + theta);
ang2 = toDegrees(bisect - theta);
setArcByCenter(x, y, radius, ang1, ang2 - ang1, type);
}
/**
* Sets the starting angle of this arc to the specified double
* value (in degrees).
*/
public abstract void setAngleStart(double angSt);
/**
* Sets the angular extent of this arc to the specified double
* value (in degrees).
*/
public abstract void setAngleExtent(double angExt);
/**
* 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.
*/
public void setAngleStart(Point2D p) {
setAngleStart(toDegrees(Math.atan2(p.getY() - getCenterY(),
p.getX() - getCenterX())));
}
/**
* Sets the angular extents of this arc to the angles that the
* specified point coordinates define relative to the center of
* this arc.
* The arc will always be non-empty and extend counterclockwise
* from the first point around to the second point.
*/
public void setAngles(double x1, double y1, double x2, double y2) {
double x = getCenterX();
double y = getCenterY();
double ang1 = Math.atan2(y1 - y, x1 - x);
double ang2 = Math.atan2(y2 - y, x2 - x);
ang2 -= ang1;
if (ang2 <= 0.0) {
ang2 += Math.PI * 2.0;
}
setAngleStart(toDegrees(ang1));
setAngleExtent(toDegrees(ang2));
}
/**
* Sets the angular extents of this arc to the angles that the
* specified Point objects define relative to the center of this
* arc.
* The arc will always be non-empty and extend counterclockwise
* from the first point around to the second point.
*/
public void setAngles(Point2D p1, Point2D p2) {
setAngles(p1.getX(), p1.getY(), p2.getX(), p2.getY());
}
/**
* Sets the closure type of this arc to the specified value.
* @see OPEN
* @see CHORD
* @see PIE
*/
public void setArcType(int type) {
if (type < OPEN || type > PIE) {
throw new IllegalArgumentException("invalid type for Arc: "+type);
}
this.type = type;
}
/**
* Sets the location and size of the outer bounds of this shape
* to the specified rectangular values.
*/
public void setBounds(double x, double y, double w, double h) {
setArc(x, y, w, h, getAngleStart(), getAngleExtent(), type);
}
/**
* Return the high precision bounding box of the shape.
*/
public Rectangle2D getBounds2D() {
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 = 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);
}
/**
* Construct a rectangle object of the appropriate precision
* to hold the parameters calculated to be the bounding box
* of this arc.
*/
protected abstract Rectangle2D makeBounds(double x, double y,
double w, double h);
/*
* Converts radians to degrees.
*/
protected static double toDegrees(double angrad) {
return angrad * 180.0 / Math.PI;
}
/*
* Converts degrees to radians.
*/
protected static double toRadians(double angdeg) {
return angdeg / 180.0 * Math.PI;
}
/*
* Normalizes the specified angle into the range -180 to 180.
*/
protected static double normalizeDegrees(double angle) {
if (angle > 180.0 || angle <= -180.0) {
angle = toRadians(angle);
double x = Math.cos(angle);
double y = Math.sin(angle);
angle = toDegrees(Math.atan2(y, x));
}
return angle;
}
/**
* Tests if a given angle is within the angular extents of the arc.
*/
public boolean containsAngle(double 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);
}
/**
* Test if a given Point is inside the boundary of the shape.
*/
public boolean contains(double x, double y) {
// 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(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 = toRadians(getAngleStart());
double x1 = Math.cos(angle);
double y1 = Math.sin(angle);
angle += toRadians(getAngleExtent());
double x2 = Math.cos(angle);
double y2 = Math.sin(angle);
boolean inside = (Line2D.relativeCCW(x1, y1, x2, y2, x, y) *
Line2D.relativeCCW(x1, y1, x2, y2, 0, 0) >= 0);
return inarc ? !inside : inside;
}
/**
* Test if the interior of the Shape intersects the interior of a given
* Rectangle.
*/
public boolean intersects(double x, double y, double w, double h) {
/*
Change around so that is really works this time
We check each of the four line segments for intersection with an
ellipse of the same radius as the imaginary arc.
We then compute the angle of the intersections and call included
angle to find out if the intersection is within the arc itself
- Aaron Muderick
*/
double intersect_angle;
double yint, xint;
//if there are any points of the rect in the arc then return true;
if (contains(x, y) || contains(x + w, y) ||
contains(x, y + h) || contains(x + w, y + h)) return true;
//we need to translate the arc and the rect so that we can do
//quadrant checking
x = x - (getX() + (getWidth()/2));
y = (y - (getY() + (getHeight()/2))) * (-1);
//find out the squash ratio
double squash = getWidth()/getHeight();
if (((x*x)/((getWidth()/2)*(getWidth()/2)) < 1)) {
if ((x == 0) && ((((getHeight()/2) >= (y-h)) && ((getHeight()/2) <= y))
|| ((((-1) * (getHeight()/2)) >= (y-h)) && (((-1)
* (getHeight()/2) <= y))))) {
if (containsAngle(Math.PI/2)) {
return true;
}
if (containsAngle(Math.PI*(3/2))) {
return true;
}
}
yint = Math.abs(Math.sqrt((1-((x*x)/((getWidth()/2)*(getWidth()/2))))
*((getHeight()/2)*(getHeight()/2))));
intersect_angle = Math.abs(Math.atan((yint*squash)/x));
if ((x > 0) && (((yint >= (y-h)) && (yint <= y))
|| ((((-1) * yint) >= (y-h))
&& (((-1) * yint) <= y)))) {
if (containsAngle(intersect_angle/Math.PI * 180)) {
return true;
}
if (containsAngle((((2*Math.PI) - intersect_angle)/Math.PI)
* 180)) {
return true;
}
}
if ((x < 0) && (((yint >= (y-h)) && (yint <= y))
|| ((((-1) * yint) >= (y-h))
&& (((-1) * yint) <= y)))){
if (containsAngle((((Math.PI) - intersect_angle)/Math.PI) * 180)) {
return true;
}
if (containsAngle(((Math.PI + intersect_angle)/Math.PI) * 180)) {
return true;
}
}
}
if ((((x+w)*(x+w))/((getWidth()/2)*(getWidth()/2)) < 1)) {
if (((x+w) == 0) && ((((getHeight()/2) >= (y-h))
&& ((getHeight()/2) <= y))
|| ((((-1) * (getHeight()/2)) >= (y-h))
&& (((-1) * (getHeight()/2) <= y))))) {
if (containsAngle(Math.PI/2)) {
return true;
}
if (containsAngle(Math.PI*(3/2))) {
return true;
}
}
yint = Math.abs(Math.sqrt((1-(((x+w)*(x+w))
/((getWidth()/2)*(getWidth()/2))))
*((getHeight()/2)*(getHeight()/2))));
intersect_angle = Math.abs(Math.atan((yint*squash)/(x+w)));
if (((x+w) > 0) && (((yint >= (y-h)) && (yint <= y))
|| ((((-1) * yint) >= (y-h))
&& (((-1) * yint) <= y)))) {
if (containsAngle((intersect_angle/Math.PI) * 180)) {
return true;
}
if (containsAngle((((2*Math.PI) - intersect_angle)/Math.PI) * 180)) {
return true;
}
}
if (((x+w) < 0) && (((yint >= (y-h)) && (yint <= y))
|| ((((-1) * yint) >= (y-h))
&& (((-1) * yint) <= y)))) {
if (containsAngle((((Math.PI) - intersect_angle)/Math.PI) * 180)) {
return true;
}
if (containsAngle(((Math.PI + intersect_angle)/Math.PI) * 180)) {
return true;
}
}
}
if (((y*y)/((getHeight()/2)*(getHeight()/2)) < 1)) {
if ((y == 0) && ((((getWidth()/2) >= x) && ((getWidth()/2) <= (x+w)))
|| ((((-1) * (getWidth()/2)) >= x)
&& (((-1) * (getWidth()/2)) <= (x+w))))) {
if (containsAngle(Math.PI)) {
return true;
}
if (containsAngle(Math.PI*2)) {
return true;
}
}
xint = Math.abs(Math.sqrt((1-((y*y)
/((getHeight()/2)
*(getHeight()/2))))
*((getWidth()/2)*(getWidth()/2))));
intersect_angle = Math.abs(Math.atan(y/(xint/squash)));
if ((y > 0) && (((xint >= x) && (xint <= (x+w)))
|| ((((-1) * xint) >= x)
&& (((-1) * xint) <= (x+w))))) {
if (containsAngle((intersect_angle)/Math.PI * 180)) {
return true;
}
if (containsAngle(((Math.PI) - intersect_angle)/Math.PI * 180)) {
return true;
}
}
if ((y < 0) && (((xint >= x) && (xint <= (x+w)))
|| ((((-1) * xint) >= x)
&& (((-1) * xint) <= (x+w))))) {
if (containsAngle(((Math.PI*2) - intersect_angle)/Math.PI * 180)) {
return true;
}
if (containsAngle((Math.PI + intersect_angle)/Math.PI * 180)) {
return true;
}
}
}
if ((((y-h)*(y-h))/((getHeight()/2)*(getHeight()/2)) < 1)) {
if (((y-h) == 0) && ((((getWidth()/2) >= x)
&& ((getWidth()/2) <= (x+w)))
|| ((((-1) * (getWidth()/2)) >= x)
&& (((-1) * (getWidth()/2)) <= (x+w))))) {
if (containsAngle(Math.PI)) {
return true;
}
if (containsAngle(Math.PI*2)) {
return true;
}
}
xint = Math.abs(Math.sqrt((1-(((y-h)*(y-h))
/((getHeight()/2)*(getHeight()/2))))
*((getWidth()/2)*(getWidth()/2))));
intersect_angle = Math.abs(Math.atan((y-h)/(xint/squash)));
if (((y-h) > 0) && (((xint >= x) && (xint <= (x+w)))
|| ((((-1) * xint) >= x)
&& (((-1) * xint) <= (x+w))))) {
if (containsAngle(intersect_angle/Math.PI * 180)) {
return true;
}
if (containsAngle(((Math.PI) - intersect_angle)/Math.PI * 180)) {
return true;
}
}
if (((y-h) < 0) && (((xint >= x) && (xint <= (x+w)))
|| ((((-1) * xint) >= x)
&& (((-1) * xint) <= (x+w))))) {
if (containsAngle(((Math.PI*2) - intersect_angle)/Math.PI * 180)) {
return true;
}
if (containsAngle((Math.PI + intersect_angle)/Math.PI * 180)) {
return true;
}
}
}
return false;
}
/**
* Test if the interior of the Shape entirely contains the given
* set of rectangular coordinates.
*/
public boolean contains(double x, double y, double w, double h) {
return contains(x, y, w, h, null);
}
/**
* Test if the interior of the Shape entirely contains the given
* Rectangle.
*/
public boolean contains(Rectangle2D r) {
return contains(r.getX(), r.getY(), r.getWidth(), r.getHeight(), r);
}
private boolean contains(double x, double y, double w, double h,
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 || 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
// rectangles 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 = 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 += toRadians(getAngleExtent());
xe = xc + halfW * Math.cos(angle);
ye = yc + halfH * Math.sin(angle);
return !origrect.intersectsLine(xc, yc, xe, ye);
}
/**
* Return an iteration object that defines the boundary of the
* shape.
*/
public PathIterator getPathIterator(AffineTransform at) {
return new ArcIterator(this, at);
}
}