Arc2Dpublic 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). |
Fields Summary |
---|
public static final int | OPENThe arc type OPEN indicates that the shape includes only the curved arc
segment. | public static final int | CHORDThe 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 | PIEThe 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 | typeThe closure type of the arc. |
Constructors Summary |
---|
protected Arc2D(int type)Instantiates a new arc2D.
setArcType(type);
|
Methods Summary |
---|
public boolean | contains(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 boolean | contains(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 boolean | contains(java.awt.geom.Rectangle2D rect)
return contains(rect.getX(), rect.getY(), rect.getWidth(), rect.getHeight());
| public boolean | containsAngle(double angle)Determines whether the given angle is contained in the span 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 double | getAngleExtent()Gets the width angle.
| public abstract double | getAngleStart()Gets the start angle.
| public int | getArcType()Gets the arc type, either {@link Arc2D#OPEN}, {@link Arc2D#CHORD}, or
{@link Arc2D#PIE}.
return type;
| public java.awt.geom.Rectangle2D | getBounds2D()
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.Point2D | getEndPoint()Gets the end point of the arc as a Point2D.
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);
| double | getNormAngle(double angle)Normalizes the angle by removing extra winding (past 360 degrees) and
placing it in the positive degree range.
double n = Math.floor(angle / 360.0);
return angle - n * 360.0;
| public java.awt.geom.PathIterator | getPathIterator(java.awt.geom.AffineTransform at)
return new Iterator(this, at);
| public java.awt.geom.Point2D | getStartPoint()Gets the start point of the arc as a Point2D.
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 boolean | intersects(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.Rectangle2D | makeBounds(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.
| public abstract void | setAngleExtent(double extent)Sets the width angle.
| public void | setAngleStart(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.
double angle = Math.atan2(point.getY() - getCenterY(), point.getX() - getCenterX());
setAngleStart(getNormAngle(-Math.toDegrees(angle)));
| public abstract void | setAngleStart(double start)Sets the start angle.
| public void | setAngles(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).
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 void | setAngles(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.
setAngles(p1.getX(), p1.getY(), p2.getX(), p2.getY());
| public void | setArc(java.awt.geom.Point2D point, java.awt.geom.Dimension2D size, double start, double extent, int type)Sets the data that defines the arc.
setArc(point.getX(), point.getY(), size.getWidth(), size.getHeight(), start, extent, type);
| public void | setArc(java.awt.geom.Rectangle2D rect, double start, double extent, int type)Sets the data that defines the arc.
setArc(rect.getX(), rect.getY(), rect.getWidth(), rect.getHeight(), start, extent, type);
| public void | setArc(java.awt.geom.Arc2D arc)Sets the data that defines the arc by copying it from another Arc2D.
setArc(arc.getX(), arc.getY(), arc.getWidth(), arc.getHeight(), arc.getAngleStart(), arc
.getAngleExtent(), arc.getArcType());
| public abstract void | setArc(double x, double y, double width, double height, double start, double extent, int type)Sets the data values that define the arc.
| public void | setArcByCenter(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.
setArc(x - radius, y - radius, radius * 2.0, radius * 2.0, start, extent, type);
| public void | setArcByTangent(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).
// 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 void | setArcType(int type)Sets the arc type, either {@link Arc2D#OPEN}, {@link Arc2D#CHORD}, or
{@link Arc2D#PIE}.
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 void | setFrame(double x, double y, double width, double height)
setArc(x, y, width, height, getAngleStart(), getAngleExtent(), type);
|
|