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:
Java Source  |  1998-03-20  |  28.8 KB  |  1,038 lines

  1. /*
  2.  * @(#)Arc2D.java    1.9 98/03/18
  3.  *
  4.  * Copyright 1997, 1998 by Sun Microsystems, Inc.,
  5.  * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
  6.  * All rights reserved.
  7.  *
  8.  * This software is the confidential and proprietary information
  9.  * of Sun Microsystems, Inc. ("Confidential Information").  You
  10.  * shall not disclose such Confidential Information and shall use
  11.  * it only in accordance with the terms of the license agreement
  12.  * you entered into with Sun.
  13.  */
  14.  
  15. package java.awt.geom;
  16.  
  17. /**
  18.  * An arc defined by a bounding rectangle, start angle and angular extent,
  19.  * and a closure type (one of OPEN, CHORD, or PIE).
  20.  * The bounding rectangle defines the outer boundary of the full ellipse
  21.  * of which this arc is a partial section.
  22.  * The angles are specified relative to the non-square extents of the
  23.  * bounding rectangle such that 45 degrees always falls on the line from
  24.  * the center of the ellipse to the upper right corner of the bounding
  25.  * rectangle.
  26.  * As a result, if the bounding rectangle is noticeably longer in one
  27.  * axis than the other, the angles to the start and end of the arc segment
  28.  * will be skewed farther along the longer axis of the bounds.
  29.  * <p>
  30.  * This class is only the abstract superclass for all objects which
  31.  * store a 2D arc.
  32.  * The actual storage representation of the coordinates is left to
  33.  * the subclass.
  34.  *
  35.  * @version 10 Feb 1997
  36.  * @author    Jim Graham
  37.  */
  38. public abstract class Arc2D extends RectangularShape {
  39.     /**
  40.      * The closure type for an open arc with no path segments
  41.      * connecting the two ends of the arc segment.
  42.      */
  43.     public final static int OPEN = 0;
  44.  
  45.     /**
  46.      * The closure type for an arc closed by drawing a straight
  47.      * line segment from the start to the end of the arc segment.
  48.      */
  49.     public final static int CHORD = 1;
  50.  
  51.     /**
  52.      * The closure type for an arc closed by drawing straight line
  53.      * segments from the start of the arc segment to the center
  54.      * of the full ellipse and back to the end of the arc segment.
  55.      */
  56.     public final static int PIE = 2;
  57.  
  58.     /**
  59.      * An arc specified in float precision,
  60.      */
  61.     public static class Float extends Arc2D {
  62.     /**
  63.      * The x coordinate of the upper left corner of the arc.
  64.      */
  65.     public float x;
  66.  
  67.     /**
  68.      * The y coordinate of the upper left corner of the arc.
  69.      */
  70.     public float y;
  71.  
  72.     /**
  73.      * The overall width of the full ellipse (not considering the
  74.      * angular extents).
  75.      */
  76.     public float width;
  77.  
  78.     /**
  79.      * The overall height of the full ellipse (not considering the
  80.      * angular extents).
  81.      */
  82.     public float height;
  83.  
  84.     /**
  85.      * The starting angle of the arc in degrees.
  86.      */
  87.     public float start;
  88.  
  89.     /**
  90.      * The angular extent of the arc in degrees.
  91.      */
  92.     public float extent;
  93.  
  94.     /**
  95.      * Constructs a new OPEN arc, initialized to location (0, 0),
  96.      * size (0, 0), angular extents (start = 0, extent = 0).
  97.      */
  98.     public Float() {
  99.         super(OPEN);
  100.     }
  101.  
  102.     /**
  103.      * Constructs a new arc, initialized to location (0, 0),
  104.      * size (0, 0), angular extents (start = 0, extent = 0), and
  105.      * the specified closure type.
  106.      */
  107.     public Float(int type) {
  108.         super(type);
  109.     }
  110.  
  111.     /**
  112.      * Constructs a new arc, initialized to the specified location,
  113.      * size, angular extents, and closure type.
  114.      */
  115.     public Float(float x, float y, float w, float h,
  116.              float start, float extent, int type) {
  117.         super(type);
  118.         this.x = x;
  119.         this.y = y;
  120.         this.width = w;
  121.         this.height = h;
  122.         this.start = start;
  123.         this.extent = extent;
  124.     }
  125.  
  126.     /**
  127.      * Constructs a new arc, initialized to the specified location,
  128.      * size, angular extents, and closure type.
  129.      */
  130.     public Float(Rectangle2D ellipseBounds,
  131.              float start, float extent, int type) {
  132.         super(type);
  133.         this.x = (float) ellipseBounds.getX();
  134.         this.y = (float) ellipseBounds.getY();
  135.         this.width = (float) ellipseBounds.getWidth();
  136.         this.height = (float) ellipseBounds.getHeight();
  137.         this.start = start;
  138.         this.extent = extent;
  139.     }
  140.  
  141.     /**
  142.      * Returns the X coordinate of the upper left corner of the arc
  143.      * in double precision.
  144.      */
  145.     public double getX() {
  146.         return (double) x;
  147.     }
  148.  
  149.     /**
  150.      * Returns the Y coordinate of the upper left corner of the arc
  151.      * in double precision.
  152.      */
  153.     public double getY() {
  154.         return (double) y;
  155.     }
  156.  
  157.     /**
  158.      * Returns the overall width of the arc in double precision.
  159.      */
  160.     public double getWidth() {
  161.         return (double) width;
  162.     }
  163.  
  164.     /**
  165.      * Returns the overall height of the arc in double precision.
  166.      */
  167.     public double getHeight() {
  168.         return (double) height;
  169.     }
  170.  
  171.     /**
  172.      * Returns the starting angle of the arc (in degrees).
  173.      */
  174.     public double getAngleStart() {
  175.         return (double) start;
  176.     }
  177.  
  178.     /**
  179.      * Returns the arc length (angular extent) of the arc (in degrees).
  180.      */
  181.     public double getAngleExtent() {
  182.         return (double) extent;
  183.     }
  184.  
  185.     /**
  186.      * Determines whether the rectangular shape is empty.
  187.      */
  188.     public boolean isEmpty() {
  189.         return (width <= 0.0 || height <= 0.0);
  190.     }
  191.  
  192.     /**
  193.      * Sets the location, size, angular extents, and closure type of
  194.      * this arc to the specified double values.
  195.      */
  196.     public void setArc(double x, double y, double w, double h,
  197.                double angSt, double angExt, int closure) {
  198.         this.setArcType(closure);
  199.         this.x = (float) x;
  200.         this.y = (float) y;
  201.         this.width = (float) w;
  202.         this.height = (float) h;
  203.         this.start = (float) angSt;
  204.         this.extent = (float) angExt;
  205.     }
  206.  
  207.     /**
  208.      * Sets the starting angle of this arc to the specified double
  209.      * value (in degrees).
  210.      */
  211.     public void setAngleStart(double angSt) {
  212.         this.start = (float) angSt;
  213.     }
  214.  
  215.     /**
  216.      * Sets the angular extent of this arc to the specified double
  217.      * value (in degrees).
  218.      */
  219.     public void setAngleExtent(double angExt) {
  220.         this.extent = (float) angExt;
  221.     }
  222.  
  223.     /**
  224.      * Return the high precision bounding box of the shape.
  225.      */
  226.     public Rectangle2D makeBounds(double x, double y, double w, double h) {
  227.         return new Rectangle2D.Float((float) x, (float) y,
  228.                      (float) w, (float) h);
  229.     }
  230.     }
  231.  
  232.     /**
  233.      * An arc specified in double precision,
  234.      */
  235.     public static class Double extends Arc2D {
  236.     /**
  237.      * The x coordinate of the upper left corner of the arc.
  238.      */
  239.     public double x;
  240.  
  241.     /**
  242.      * The y coordinate of the upper left corner of the arc.
  243.      */
  244.     public double y;
  245.  
  246.     /**
  247.      * The overall width of the full ellipse (not considering the
  248.      * angular extents).
  249.      */
  250.     public double width;
  251.  
  252.     /**
  253.      * The overall height of the full ellipse (not considering the
  254.      * angular extents).
  255.      */
  256.     public double height;
  257.  
  258.     /**
  259.      * The starting angle of the arc in degrees.
  260.      */
  261.     public double start;
  262.  
  263.     /**
  264.      * The angular extent of the arc in degrees.
  265.      */
  266.     public double extent;
  267.  
  268.     /**
  269.      * Constructs a new OPEN arc, initialized to location (0, 0),
  270.      * size (0, 0), angular extents (start = 0, extent = 0).
  271.      */
  272.     public Double() {
  273.         super(OPEN);
  274.     }
  275.  
  276.     /**
  277.      * Constructs a new arc, initialized to location (0, 0),
  278.      * size (0, 0), angular extents (start = 0, extent = 0), and
  279.      * the specified closure type.
  280.      */
  281.     public Double(int type) {
  282.         super(type);
  283.     }
  284.  
  285.     /**
  286.      * Constructs a new arc, initialized to the specified location,
  287.      * size, angular extents, and closure type.
  288.      */
  289.     public Double(double x, double y, double w, double h,
  290.               double start, double extent, int type) {
  291.         super(type);
  292.         this.x = x;
  293.         this.y = y;
  294.         this.width = w;
  295.         this.height = h;
  296.         this.start = start;
  297.         this.extent = extent;
  298.     }
  299.  
  300.     /**
  301.      * Constructs a new arc, initialized to the specified location,
  302.      * size, angular extents, and closure type.
  303.      */
  304.     public Double(Rectangle2D ellipseBounds,
  305.               double start, double extent, int type) {
  306.         super(type);
  307.         this.x = ellipseBounds.getX();
  308.         this.y = ellipseBounds.getY();
  309.         this.width = ellipseBounds.getWidth();
  310.         this.height = ellipseBounds.getHeight();
  311.         this.start = start;
  312.         this.extent = extent;
  313.     }
  314.  
  315.     /**
  316.      * Returns the X coordinate of the upper left corner of the arc
  317.      * in double precision.
  318.      */
  319.     public double getX() {
  320.         return x;
  321.     }
  322.  
  323.     /**
  324.      * Returns the Y coordinate of the upper left corner of the arc
  325.      * in double precision.
  326.      */
  327.     public double getY() {
  328.         return y;
  329.     }
  330.  
  331.     /**
  332.      * Returns the overall width of the arc in double precision.
  333.      */
  334.     public double getWidth() {
  335.         return width;
  336.     }
  337.  
  338.     /**
  339.      * Returns the overall height of the arc in double precision.
  340.      */
  341.     public double getHeight() {
  342.         return height;
  343.     }
  344.  
  345.     /**
  346.      * Returns the starting angle of the arc (in degrees).
  347.      */
  348.     public double getAngleStart() {
  349.         return start;
  350.     }
  351.  
  352.     /**
  353.      * Returns the arc length (angular extent) of the arc (in degrees).
  354.      */
  355.     public double getAngleExtent() {
  356.         return extent;
  357.     }
  358.  
  359.     /**
  360.      * Determines whether the rectangular shape is empty.
  361.      */
  362.     public boolean isEmpty() {
  363.         return (width <= 0.0 || height <= 0.0);
  364.     }
  365.  
  366.     /**
  367.      * Sets the location, size, angular extents, and closure type of
  368.      * this arc to the specified double values.
  369.      */
  370.     public void setArc(double x, double y, double w, double h,
  371.                double angSt, double angExt, int closure) {
  372.         this.setArcType(closure);
  373.         this.x = x;
  374.         this.y = y;
  375.         this.width = w;
  376.         this.height = h;
  377.         this.start = angSt;
  378.         this.extent = angExt;
  379.     }
  380.  
  381.     /**
  382.      * Sets the starting angle of this arc to the specified double
  383.      * value (in degrees).
  384.      */
  385.     public void setAngleStart(double angSt) {
  386.         this.start = angSt;
  387.     }
  388.  
  389.     /**
  390.      * Sets the angular extent of this arc to the specified double
  391.      * value (in degrees).
  392.      */
  393.     public void setAngleExtent(double angExt) {
  394.         this.extent = angExt;
  395.     }
  396.  
  397.     /**
  398.      * Return the high precision bounding box of the shape.
  399.      */
  400.     public Rectangle2D makeBounds(double x, double y, double w, double h) {
  401.         return new Rectangle2D.Double(x, y, w, h);
  402.     }
  403.     }
  404.  
  405.     private int type;
  406.  
  407.     /**
  408.      * Constructs a new arc of the specified closure type.
  409.      */
  410.     protected Arc2D(int type) {
  411.     setArcType(type);
  412.     }
  413.  
  414.     /**
  415.      * Returns the starting angle of the arc (in degrees).
  416.      */
  417.     public abstract double getAngleStart();
  418.  
  419.     /**
  420.      * Returns the arc length (angular extent) of the arc (in degrees).
  421.      */
  422.     public abstract double getAngleExtent();
  423.  
  424.     /**
  425.      * Returns the arc closure type of the arc.
  426.      * @see OPEN
  427.      * @see CHORD
  428.      * @see PIE
  429.      */
  430.     public int getArcType() {
  431.     return type;
  432.     }
  433.  
  434.     /**
  435.      * Returns the starting point of the arc.  This point is the
  436.      * intersection of the ray from the center defined by the
  437.      * starting angle and the elliptical boundary of the arc.
  438.      */
  439.     public Point2D getStartPoint() {
  440.     double angle = toRadians(getAngleStart());
  441.     double x = getX() + (Math.cos(angle) * 0.5 + 0.5) * getWidth();
  442.     double y = getY() + (Math.sin(angle) * 0.5 + 0.5) * getHeight();
  443.     return new Point2D.Double(x, y);
  444.     }
  445.  
  446.     /**
  447.      * Returns the ending point of the arc.  This point is the
  448.      * intersection of the ray from the center defined by the
  449.      * starting angle plus the angular extent of the arc and the
  450.      * elliptical boundary of the arc.
  451.      */
  452.     public Point2D getEndPoint() {
  453.     double angle = toRadians(getAngleStart() + getAngleExtent());
  454.     double x = getX() + (Math.cos(angle) * 0.5 + 0.5) * getWidth();
  455.     double y = getY() + (Math.sin(angle) * 0.5 + 0.5) * getHeight();
  456.     return new Point2D.Double(x, y);
  457.     }
  458.  
  459.     /**
  460.      * Sets the location, size, angular extents, and closure type of
  461.      * this arc to the specified double values.
  462.      */
  463.     public abstract void setArc(double x, double y, double w, double h,
  464.                 double angSt, double angExt, int closure);
  465.  
  466.     /**
  467.      * Sets the location, size, angular extents, and closure type of
  468.      * this arc to the specified Point and Dimension and double values.
  469.      */
  470.     public void setArc(Point2D loc, Dimension2D size,
  471.                double angSt, double angExt, int closure) {
  472.     setArc(loc.getX(), loc.getY(), size.getWidth(), size.getHeight(),
  473.            angSt, angExt, closure);
  474.     }
  475.  
  476.     /**
  477.      * Sets the location, size, angular extents, and closure type of
  478.      * this arc to the specified Rectangle and double values.
  479.      */
  480.     public void setArc(Rectangle2D rect, double angSt, double angExt,
  481.                int closure) {
  482.     setArc(rect.getX(), rect.getY(), rect.getWidth(), rect.getHeight(),
  483.            angSt, angExt, closure);
  484.     }
  485.  
  486.     /**
  487.      * Sets this arc to be the same as the specified Arc.
  488.      */
  489.     public void setArc(Arc2D a) {
  490.     setArc(a.getX(), a.getY(), a.getWidth(), a.getHeight(),
  491.            a.getAngleStart(), a.getAngleExtent(), a.type);
  492.     }
  493.  
  494.     /**
  495.      * Sets the position, bounds, angular extents, and closure type of
  496.      * this arc to the specified double values based on a center point
  497.      * and a radius rather than a bounding box for the full ellipse.
  498.      */
  499.     public void setArcByCenter(double x, double y, double radius,
  500.                    double angSt, double angExt, int closure) {
  501.     setArc(x - radius, y - radius, radius * 2.0, radius * 2.0,
  502.            angSt, angExt, closure);
  503.     }
  504.  
  505.     /**
  506.      * Sets the position, bounds, and angular extents of this arc
  507.      * so that the starting angle is tangent to the line specified
  508.      * by points (p1, p2) and the ending angle is tangent to the
  509.      * line specified by points (p2, p3) with the given radius.
  510.      */
  511.     public void setArcByTangent(Point2D p1, Point2D p2, Point2D p3,
  512.                 double radius) {
  513.     double ang1 = Math.atan2(p1.getY() - p2.getY(),
  514.                  p1.getX() - p2.getX());
  515.     double ang2 = Math.atan2(p3.getY() - p2.getY(),
  516.                  p3.getX() - p2.getX());
  517.     double diff = ang2 - ang1;
  518.     if (diff > Math.PI) {
  519.         ang2 -= Math.PI * 2.0;
  520.     } else if (diff < -Math.PI) {
  521.         ang2 += Math.PI * 2.0;
  522.     }
  523.     double bisect = (ang1 + ang2) / 2.0;
  524.     double theta = Math.abs(ang2 - bisect);
  525.     double dist = radius / Math.sin(theta);
  526.     double x = p2.getX() + dist * Math.cos(bisect);
  527.     double y = p2.getY() + dist * Math.sin(bisect);
  528.     theta += Math.PI / 2.0;
  529.     if (diff < 0.0) {
  530.         theta = -theta;
  531.     }
  532.     ang1 = toDegrees(bisect + theta);
  533.     ang2 = toDegrees(bisect - theta);
  534.     setArcByCenter(x, y, radius, ang1, ang2 - ang1, type);
  535.     }
  536.  
  537.     /**
  538.      * Sets the starting angle of this arc to the specified double
  539.      * value (in degrees).
  540.      */
  541.     public abstract void setAngleStart(double angSt);
  542.  
  543.     /**
  544.      * Sets the angular extent of this arc to the specified double
  545.      * value (in degrees).
  546.      */
  547.     public abstract void setAngleExtent(double angExt);
  548.  
  549.     /**
  550.      * Sets the starting angle of this arc to the angle that the
  551.      * specified point defines relative to the center of this arc.
  552.      * The angular extent of the arc will remain the same.
  553.      */
  554.     public void setAngleStart(Point2D p) {
  555.     setAngleStart(toDegrees(Math.atan2(p.getY() - getCenterY(),
  556.                        p.getX() - getCenterX())));
  557.     }
  558.  
  559.     /**
  560.      * Sets the angular extents of this arc to the angles that the
  561.      * specified point coordinates define relative to the center of
  562.      * this arc.
  563.      * The arc will always be non-empty and extend counterclockwise
  564.      * from the first point around to the second point.
  565.      */
  566.     public void setAngles(double x1, double y1, double x2, double y2) {
  567.     double x = getCenterX();
  568.     double y = getCenterY();
  569.     double ang1 = Math.atan2(y1 - y, x1 - x);
  570.     double ang2 = Math.atan2(y2 - y, x2 - x);
  571.     ang2 -= ang1;
  572.     if (ang2 <= 0.0) {
  573.         ang2 += Math.PI * 2.0;
  574.     }
  575.     setAngleStart(toDegrees(ang1));
  576.     setAngleExtent(toDegrees(ang2));
  577.     }
  578.  
  579.     /**
  580.      * Sets the angular extents of this arc to the angles that the
  581.      * specified Point objects define relative to the center of this
  582.      * arc.
  583.      * The arc will always be non-empty and extend counterclockwise
  584.      * from the first point around to the second point.
  585.      */
  586.     public void setAngles(Point2D p1, Point2D p2) {
  587.     setAngles(p1.getX(), p1.getY(), p2.getX(), p2.getY());
  588.     }
  589.  
  590.     /**
  591.      * Sets the closure type of this arc to the specified value.
  592.      * @see OPEN
  593.      * @see CHORD
  594.      * @see PIE
  595.      */
  596.     public void setArcType(int type) {
  597.     if (type < OPEN || type > PIE) {
  598.         throw new IllegalArgumentException("invalid type for Arc: "+type);
  599.     }
  600.     this.type = type;
  601.     }
  602.  
  603.     /**
  604.      * Sets the location and size of the outer bounds of this shape
  605.      * to the specified rectangular values.
  606.      */
  607.     public void setBounds(double x, double y, double w, double h) {
  608.     setArc(x, y, w, h, getAngleStart(), getAngleExtent(), type);
  609.     }
  610.  
  611.     /**
  612.      * Return the high precision bounding box of the shape.
  613.      */
  614.     public Rectangle2D getBounds2D() {
  615.     if (isEmpty()) {
  616.         return makeBounds(getX(), getY(), getWidth(), getHeight());
  617.     }
  618.     double x1, y1, x2, y2;
  619.     if (getArcType() == PIE) {
  620.         x1 = y1 = x2 = y2 = 0.0;
  621.     } else {
  622.         x1 = y1 = 1.0;
  623.         x2 = y2 = -1.0;
  624.     }
  625.     double angle = 0.0;
  626.     for (int i = 0; i < 6; i++) {
  627.         if (i < 4) {
  628.         // 0-3 are the four quadrants
  629.         angle += 90.0;
  630.         if (!containsAngle(angle)) {
  631.             continue;
  632.         }
  633.         } else if (i == 4) {
  634.         // 4 is start angle
  635.         angle = getAngleStart();
  636.         } else {
  637.         // 5 is end angle
  638.         angle += getAngleExtent();
  639.         }
  640.         double rads = toRadians(angle);
  641.         double xe = Math.cos(rads);
  642.         double ye = Math.sin(rads);
  643.         x1 = Math.min(x1, xe);
  644.         y1 = Math.min(y1, ye);
  645.         x2 = Math.max(x2, xe);
  646.         y2 = Math.max(y2, ye);
  647.     }
  648.     double w = getWidth();
  649.     double h = getHeight();
  650.     x2 = (x2 - x1) * 0.5 * w;
  651.     y2 = (y2 - y1) * 0.5 * h;
  652.     x1 = getX() + (x1 * 0.5 + 0.5) * w;
  653.     y1 = getY() + (y1 * 0.5 + 0.5) * h;
  654.     return makeBounds(x1, y1, x2, y2);
  655.     }
  656.  
  657.     /**
  658.      * Construct a rectangle object of the appropriate precision
  659.      * to hold the parameters calculated to be the bounding box
  660.      * of this arc.
  661.      */
  662.     protected abstract Rectangle2D makeBounds(double x, double y,
  663.                           double w, double h);
  664.  
  665.     /*
  666.      * Converts radians to degrees.
  667.      */
  668.     protected static double toDegrees(double angrad) {
  669.     return angrad * 180.0 / Math.PI;
  670.     }
  671.  
  672.     /*
  673.      * Converts degrees to radians.
  674.      */
  675.     protected static double toRadians(double angdeg) {
  676.     return angdeg / 180.0 * Math.PI;
  677.     }
  678.  
  679.     /*
  680.      * Normalizes the specified angle into the range -180 to 180.
  681.      */
  682.     protected static double normalizeDegrees(double angle) {
  683.     if (angle > 180.0 || angle <= -180.0) {
  684.         angle = toRadians(angle);
  685.         double x = Math.cos(angle);
  686.         double y = Math.sin(angle);
  687.         angle = toDegrees(Math.atan2(y, x));
  688.     }
  689.     return angle;
  690.     }
  691.  
  692.     /**
  693.      * Tests if a given angle is within the angular extents of the arc.
  694.      */
  695.     public boolean containsAngle(double angle) {
  696.     double angExt = getAngleExtent();
  697.     boolean backwards = (angExt < 0.0);
  698.     if (backwards) {
  699.         angExt = -angExt;
  700.     }
  701.     if (angExt >= 360.0) {
  702.         return true;
  703.     }
  704.     angle = normalizeDegrees(angle) - normalizeDegrees(getAngleStart());
  705.     if (backwards) {
  706.         angle = -angle;
  707.     }
  708.     if (angle < 0.0) {
  709.         angle += 360.0;
  710.     }
  711.  
  712.       
  713.     return (angle >= 0.0) && (angle < angExt);
  714.     }
  715.  
  716.     /**
  717.      * Test if a given Point is inside the boundary of the shape.
  718.      */
  719.     public boolean contains(double x, double y) {
  720.     // Normalize the coordinates compared to the ellipse
  721.     // having a center at 0,0 and a radius of 0.5.
  722.     double ellw = getWidth();
  723.     if (ellw <= 0.0) {
  724.         return false;
  725.     }
  726.     double normx = (x - getX()) / ellw - 0.5;
  727.     double ellh = getHeight();
  728.     if (ellh <= 0.0) {
  729.         return false;
  730.     }
  731.     double normy = (y - getY()) / ellh - 0.5;
  732.     double distSq = (normx * normx + normy * normy);
  733.     if (distSq >= 0.25) {
  734.         return false;
  735.     }
  736.     double angExt = Math.abs(getAngleExtent());
  737.     if (angExt >= 360.0) {
  738.         return true;
  739.     }
  740.     boolean inarc = containsAngle(toDegrees(Math.atan2(normy, normx)));
  741.     if (type == PIE) {
  742.         return inarc;
  743.     }
  744.     // CHORD and OPEN behave the same way
  745.     if (inarc) {
  746.         if (angExt >= 180.0) {
  747.         return true;
  748.         }
  749.         // point must be outside the "pie triangle"
  750.     } else {
  751.         if (angExt <= 180.0) {
  752.         return false;
  753.         }
  754.         // point must be inside the "pie triangle"
  755.     }
  756.     // The point is inside the pie triangle iff it is on the same
  757.     // side of the line connecting the ends of the arc as the center.
  758.     double angle = toRadians(getAngleStart());
  759.     double x1 = Math.cos(angle);
  760.     double y1 = Math.sin(angle);
  761.     angle += toRadians(getAngleExtent());
  762.     double x2 = Math.cos(angle);
  763.     double y2 = Math.sin(angle);
  764.     boolean inside = (Line2D.relativeCCW(x1, y1, x2, y2, x, y) *
  765.               Line2D.relativeCCW(x1, y1, x2, y2, 0, 0) >= 0);
  766.     return inarc ? !inside : inside;
  767.     }
  768.  
  769.     /**
  770.      * Test if the interior of the Shape intersects the interior of a given
  771.      * Rectangle.
  772.      */
  773.  
  774.   public boolean intersects(double x, double y, double w, double h) {
  775.  
  776.       /*
  777.         Change around so that is really works this time
  778.         We check each of the four line segments for intersection with an
  779.         ellipse of the same radius as the imaginary arc.
  780.         We then compute the angle of the intersections and call included
  781.         angle to find out if the intersection is within the arc itself
  782.         - Aaron Muderick
  783.         */
  784.  
  785.       double intersect_angle;
  786.       double yint, xint; 
  787.  
  788.       //if there are any points of the rect in the arc then return true;
  789.       if (contains(x, y) || contains(x + w, y) ||
  790.             contains(x, y + h) || contains(x + w, y + h)) return true;
  791.       
  792.       //we need to translate the arc and the rect so that we can do
  793.       //quadrant checking
  794.       x = x - (getX() + (getWidth()/2));
  795.       y = (y - (getY() + (getHeight()/2))) * (-1);
  796.  
  797.       //find out the squash ratio
  798.       double squash = getWidth()/getHeight();
  799.  
  800.       if (((x*x)/((getWidth()/2)*(getWidth()/2)) < 1)) {
  801.     if ((x == 0) && ((((getHeight()/2) >= (y-h)) && ((getHeight()/2) <= y))
  802.                          || ((((-1) * (getHeight()/2)) >= (y-h)) && (((-1) 
  803.                                     * (getHeight()/2) <= y))))) {
  804.       if (containsAngle(Math.PI/2)) {
  805.               return true;
  806.           }
  807.       if (containsAngle(Math.PI*(3/2))) {
  808.               return true;
  809.           }
  810.     }
  811.     
  812.     yint = Math.abs(Math.sqrt((1-((x*x)/((getWidth()/2)*(getWidth()/2))))
  813.                                   *((getHeight()/2)*(getHeight()/2))));
  814.     intersect_angle = Math.abs(Math.atan((yint*squash)/x)); 
  815.  
  816.     if ((x > 0) && (((yint >= (y-h)) && (yint <= y)) 
  817.                         || ((((-1) * yint) >= (y-h)) 
  818.                             && (((-1) * yint) <= y)))) {      
  819.       if (containsAngle(intersect_angle/Math.PI * 180)) {
  820.               return true;
  821.           }
  822.       if (containsAngle((((2*Math.PI) - intersect_angle)/Math.PI) 
  823.                             * 180)) {
  824.               return true;
  825.           }
  826.     }
  827.  
  828.     if ((x < 0) && (((yint >= (y-h)) && (yint <= y)) 
  829.                         || ((((-1) * yint) >= (y-h)) 
  830.                             && (((-1) * yint) <= y)))){
  831.       
  832.       if (containsAngle((((Math.PI) - intersect_angle)/Math.PI) * 180)) {
  833.               return true;
  834.           }
  835.       if (containsAngle(((Math.PI + intersect_angle)/Math.PI) * 180)) {
  836.               return true;
  837.           }
  838.     }
  839.   }
  840.  
  841.       if ((((x+w)*(x+w))/((getWidth()/2)*(getWidth()/2)) < 1)) {
  842.     if (((x+w) == 0) && ((((getHeight()/2) >= (y-h)) 
  843.                               && ((getHeight()/2) <= y)) 
  844.                              || ((((-1) * (getHeight()/2)) >= (y-h)) 
  845.                                  && (((-1) * (getHeight()/2) <= y))))) {
  846.       if (containsAngle(Math.PI/2)) {
  847.               return true;
  848.           }
  849.       if (containsAngle(Math.PI*(3/2))) {
  850.               return true;
  851.           }
  852.     }
  853.     
  854.     yint = Math.abs(Math.sqrt((1-(((x+w)*(x+w))
  855.                                       /((getWidth()/2)*(getWidth()/2))))
  856.                                   *((getHeight()/2)*(getHeight()/2))));
  857.  
  858.     intersect_angle = Math.abs(Math.atan((yint*squash)/(x+w)));
  859.  
  860.     if (((x+w) > 0) && (((yint >= (y-h)) && (yint <= y)) 
  861.                             || ((((-1) * yint) >= (y-h)) 
  862.                                 && (((-1) * yint) <= y)))) {    
  863.       if (containsAngle((intersect_angle/Math.PI) * 180)) {
  864.               return true;
  865.           }
  866.       if (containsAngle((((2*Math.PI) - intersect_angle)/Math.PI) * 180)) {
  867.               return true;
  868.           }
  869.     }
  870.  
  871.     if (((x+w) < 0) && (((yint >= (y-h)) && (yint <= y)) 
  872.                             || ((((-1) * yint) >= (y-h)) 
  873.                                 && (((-1) * yint) <= y)))) {
  874.             if (containsAngle((((Math.PI) - intersect_angle)/Math.PI) * 180)) {
  875.               return true;
  876.             }
  877.             if (containsAngle(((Math.PI + intersect_angle)/Math.PI) * 180)) {
  878.                 return true;
  879.             }
  880.     }
  881.       
  882.       }
  883.  
  884.       if (((y*y)/((getHeight()/2)*(getHeight()/2)) < 1)) {
  885.  
  886.     if ((y == 0) && ((((getWidth()/2) >= x) && ((getWidth()/2) <= (x+w))) 
  887.                          || ((((-1) * (getWidth()/2)) >= x)
  888.                              && (((-1) * (getWidth()/2)) <= (x+w))))) {
  889.       if (containsAngle(Math.PI)) {
  890.               return true;
  891.           }
  892.       if (containsAngle(Math.PI*2)) {
  893.               return true;
  894.           }
  895.     }
  896.     
  897.     xint = Math.abs(Math.sqrt((1-((y*y)
  898.                                       /((getHeight()/2)
  899.                                         *(getHeight()/2))))
  900.                                   *((getWidth()/2)*(getWidth()/2))));
  901.  
  902.     intersect_angle = Math.abs(Math.atan(y/(xint/squash))); 
  903.  
  904.     if ((y > 0) && (((xint >= x) && (xint <= (x+w))) 
  905.                         || ((((-1) * xint) >= x) 
  906.                             && (((-1) * xint) <= (x+w))))) {     
  907.       if (containsAngle((intersect_angle)/Math.PI * 180)) {
  908.               return true;
  909.           }
  910.       if (containsAngle(((Math.PI) - intersect_angle)/Math.PI * 180)) {
  911.               return true;
  912.           }
  913.     }
  914.  
  915.     if ((y < 0) &&  (((xint >= x) && (xint <= (x+w))) 
  916.                          || ((((-1) * xint) >= x) 
  917.                              && (((-1) * xint) <= (x+w))))) {
  918.       if (containsAngle(((Math.PI*2) - intersect_angle)/Math.PI * 180)) {
  919.               return true;
  920.           }
  921.       if (containsAngle((Math.PI + intersect_angle)/Math.PI * 180)) {
  922.               return true;
  923.           }
  924.     }
  925.       
  926.       }
  927.  
  928.       if ((((y-h)*(y-h))/((getHeight()/2)*(getHeight()/2)) < 1)) {
  929.     if (((y-h) == 0) && ((((getWidth()/2) >= x) 
  930.                               && ((getWidth()/2) <= (x+w))) 
  931.                              || ((((-1) * (getWidth()/2)) >= x) 
  932.                                  && (((-1) * (getWidth()/2)) <= (x+w))))) {
  933.       if (containsAngle(Math.PI)) {
  934.               return true;
  935.           }
  936.       if (containsAngle(Math.PI*2)) {
  937.               return true;
  938.           }
  939.     }
  940.     
  941.     xint = Math.abs(Math.sqrt((1-(((y-h)*(y-h))
  942.                                       /((getHeight()/2)*(getHeight()/2))))
  943.                                   *((getWidth()/2)*(getWidth()/2))));
  944.  
  945.     intersect_angle = Math.abs(Math.atan((y-h)/(xint/squash)));           
  946.  
  947.     if (((y-h) > 0) &&  (((xint >= x) && (xint <= (x+w))) 
  948.                              || ((((-1) * xint) >= x) 
  949.                                  && (((-1) * xint) <= (x+w))))) {      
  950.       if (containsAngle(intersect_angle/Math.PI * 180)) {
  951.               return true;
  952.           }
  953.       if (containsAngle(((Math.PI) - intersect_angle)/Math.PI * 180)) {
  954.               return true;
  955.           }
  956.     }
  957.     if (((y-h) < 0) && (((xint >= x) && (xint <= (x+w))) 
  958.                             || ((((-1) * xint) >= x) 
  959.                                 && (((-1) * xint) <= (x+w))))) {
  960.       if (containsAngle(((Math.PI*2) - intersect_angle)/Math.PI * 180)) {
  961.               return true;
  962.           }
  963.       if (containsAngle((Math.PI + intersect_angle)/Math.PI * 180)) {
  964.               return true;
  965.           }
  966.     }
  967.       
  968.       }
  969.       
  970.       return false;
  971.  
  972.   }
  973.     
  974.  
  975.     /**
  976.      * Test if the interior of the Shape entirely contains the given
  977.      * set of rectangular coordinates.
  978.      */
  979.     public boolean contains(double x, double y, double w, double h) {
  980.     return contains(x, y, w, h, null);
  981.     }
  982.  
  983.     /**
  984.      * Test if the interior of the Shape entirely contains the given
  985.      * Rectangle.
  986.      */
  987.     public boolean contains(Rectangle2D r) {
  988.     return contains(r.getX(), r.getY(), r.getWidth(), r.getHeight(), r);
  989.     }
  990.  
  991.     private boolean contains(double x, double y, double w, double h,
  992.                  Rectangle2D origrect) {
  993.     if (!(contains(x, y) &&
  994.           contains(x + w, y) &&
  995.           contains(x, y + h) &&
  996.           contains(x + w, y + h))) {
  997.         return false;
  998.     }
  999.     // If the shape is convex then we have done all the testing
  1000.     // we need.  Only PIE arcs can be concave and then only if
  1001.     // the angular extents are greater than 180 degrees.
  1002.     if (type != PIE || getAngleExtent() <= 180.0) {
  1003.         return true;
  1004.     }
  1005.     // For a PIE shape we have an additional test for the case where
  1006.     // the angular extents are greater than 180 degrees and all four
  1007.     // rectangular corners are inside the shape but one of the
  1008.     // rectangles edges spans across the "missing wedge" of the arc.
  1009.     // We can test for this case by checking if the rectangle intersects
  1010.     // either of the pie angle segments.
  1011.     if (origrect == null) {
  1012.         origrect = new Rectangle2D.Double(x, y, w, h);
  1013.     }
  1014.     double halfW = getWidth() / 2.0;
  1015.     double halfH = getHeight() / 2.0;
  1016.     double xc = getX() + halfW;
  1017.     double yc = getY() + halfH;
  1018.     double angle = toRadians(getAngleStart());
  1019.     double xe = xc + halfW * Math.cos(angle);
  1020.     double ye = yc + halfH * Math.sin(angle);
  1021.     if (origrect.intersectsLine(xc, yc, xe, ye)) {
  1022.         return false;
  1023.     }
  1024.     angle += toRadians(getAngleExtent());
  1025.     xe = xc + halfW * Math.cos(angle);
  1026.     ye = yc + halfH * Math.sin(angle);
  1027.     return !origrect.intersectsLine(xc, yc, xe, ye);
  1028.     }
  1029.  
  1030.     /**
  1031.      * Return an iteration object that defines the boundary of the
  1032.      * shape.
  1033.      */
  1034.     public PathIterator getPathIterator(AffineTransform at) {
  1035.     return new ArcIterator(this, at);
  1036.     }
  1037. }
  1038.