📄 arc2d.java
字号:
* * @param a The <CODE>Arc2D</CODE> to use to set the arc's values. */ 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 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}. */ 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 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.) */ 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); // 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); } /** * 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 abstract void setAngleStart(double angSt); /** * 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 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. * * @param p The <CODE>Point2D</CODE> that defines the starting angle. * @see #getAngleStart */ public void setAngleStart(Point2D p) { setAngleStart(-Math.toDegrees(Math.atan2(p.getY() - getCenterY(), p.getX() - getCenterX()))); } /** * 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. */ public void setAngles(double x1, double y1, double x2, double y2) { double x = getCenterX(); double y = getCenterY(); // Note: reversing the Y equations negates the angle to adjust // for the upside down coordinate system. double ang1 = Math.atan2(y - y1, x1 - x); double ang2 = Math.atan2(y - y2, x2 - x); ang2 -= ang1; if (ang2 <= 0.0) { ang2 += Math.PI * 2.0; } setAngleStart(Math.toDegrees(ang1)); setAngleExtent(Math.toDegrees(ang2)); } /** * 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 <CODE>Point2D</CODE> that defines the arc's * starting point. * @param p2 The <CODE>Point2D</CODE> that defines the arc's * ending 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: * <CODE>OPEN</CODE>, <CODE>CHORD</CODE>, or <CODE>PIE</CODE>. * * @param type The integer constant that represents the closure * type of this arc: {@link #OPEN}, {@link #CHORD}, or * {@link #PIE}. * * @throws IllegalArgumentException if <code>type</code> is not * 0, 1, or 2.+ * @see #getArcType */ 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 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.) */ public void setFrame(double x, double y, double w, double h) { setArc(x, y, w, h, getAngleStart(), getAngleExtent(), type); } /** * Returns the high-precision bounding box of the arc. The bounding * box contains only the part of this <code>Arc2D</code> that is * in between the starting and ending angles and contains the pie * wedge, if this <code>Arc2D</code> has a <code>PIE</code> closure type. * <p> * This method differs from the * {@link RectangularShape#getBounds() getBounds} in that the * <code>getBounds</code> method only returns the bounds of the * enclosing ellipse of this <code>Arc2D</code> without considering * the starting and ending angles of this <code>Arc2D</code>. * * @return the <CODE>Rectangle2D</CODE> that represents the arc's * bounding box. */ 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 = 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); } /** * Constructs a <code>Rectangle2D</code> 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 <code>Rectangle2D</code> that is the bounding box * of this arc. */ protected abstract Rectangle2D makeBounds(double x, double y, double w, double h); /* * Normalizes the specified angle into the range -180 to 180. */ static double normalizeDegrees(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; } /** * 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 <CODE>true</CODE> if the arc contains the angle, * <CODE>false</CODE> if the arc doesn't contain the angle. */ 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); } /** * 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 <CODE>true</CODE> if the point lies within the bound of * the arc, <CODE>false</CODE> if the point lies outside of the * arc's bounds. */ 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;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -