Java Code Examples for java.awt.geom.Rectangle2D#createIntersection()

The following examples show how to use java.awt.geom.Rectangle2D#createIntersection() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: EdmConverter.java    From phoebus with Eclipse Public License 1.0 6 votes vote down vote up
/** Do two widgets overlap, no matter which one covers the other?
 *  @param widget
 *  @param other
 *  @return Do the widgets overlap by a considerable amount?
 */
private boolean doWidgetsOverlap(final Widget widget, final Widget other)
{
    final Rectangle2D w = new Rectangle2D.Double(widget.propX().getValue(),
                                                 widget.propY().getValue(),
                                                 widget.propWidth().getValue(),
                                                 widget.propHeight().getValue());
    final Rectangle2D o = new Rectangle2D.Double(other.propX().getValue(),
                                                 other.propY().getValue(),
                                                 other.propWidth().getValue(),
                                                 other.propHeight().getValue());
    final Rectangle2D common = w.createIntersection(o);
    if (common.getWidth() <= 0  ||  common.getHeight() <= 0)
        return false;

    final int overlap = (int) (common.getWidth() * common.getHeight());
    final int avg_area = (int) (w.getWidth() * w.getHeight() +
                                o.getWidth() * o.getHeight()) / 2;
    // Overlap by at least a 5th??
    return overlap > avg_area / 5;
}
 
Example 2
Source File: EdmConverter.java    From phoebus with Eclipse Public License 1.0 6 votes vote down vote up
/** Does one widget cover the other?
 *  @param bottom
 *  @param top
 *  @return Does top widget cover the one at the bottom by a considerable amount?
 */
private boolean isWidgetCovered(final Widget bottom, final Widget top)
{
    final Rectangle2D w = new Rectangle2D.Double(bottom.propX().getValue(),
                                                 bottom.propY().getValue(),
                                                 bottom.propWidth().getValue(),
                                                 bottom.propHeight().getValue());
    final Rectangle2D o = new Rectangle2D.Double(top.propX().getValue(),
                                                 top.propY().getValue(),
                                                 top.propWidth().getValue(),
                                                 top.propHeight().getValue());
    final Rectangle2D common = w.createIntersection(o);
    if (common.getWidth() <= 0  ||  common.getHeight() <= 0)
        return false;

    final int overlap = (int) (common.getWidth() * common.getHeight());
    final int bottom_area = (int) (w.getWidth() * w.getHeight());
    // Overlap at least half of the bottom?
    return overlap > bottom_area / 2;
}
 
Example 3
Source File: EpsGraphics2D.java    From osp with GNU General Public License v3.0 4 votes vote down vote up
/**
 * Appends the commands required to draw a shape on the EPS document.
 */
private void draw(Shape s, String action) {
  if(s!=null) {
    if(!_transform.isIdentity()) {
      s = _transform.createTransformedShape(s);
    }
    // Update the bounds.
    if(!action.equals("clip")) {                                  //$NON-NLS-1$
      Rectangle2D shapeBounds = s.getBounds2D();
      Rectangle2D visibleBounds = shapeBounds;
      if(_clip!=null) {
        Rectangle2D clipBounds = _clip.getBounds2D();
        visibleBounds = shapeBounds.createIntersection(clipBounds);
      }
      float lineRadius = _stroke.getLineWidth()/2;
      float minX = (float) visibleBounds.getMinX()-lineRadius;
      float minY = (float) visibleBounds.getMinY()-lineRadius;
      float maxX = (float) visibleBounds.getMaxX()+lineRadius;
      float maxY = (float) visibleBounds.getMaxY()+lineRadius;
      _document.updateBounds(minX, -minY);
      _document.updateBounds(maxX, -maxY);
    }
    append("newpath");                                            //$NON-NLS-1$
    int type = 0;
    float[] coords = new float[6];
    PathIterator it = s.getPathIterator(null);
    float x0 = 0;
    float y0 = 0;
    while(!it.isDone()) {
      type = it.currentSegment(coords);
      float x1 = coords[0];
      float y1 = -coords[1];
      float x2 = coords[2];
      float y2 = -coords[3];
      float x3 = coords[4];
      float y3 = -coords[5];
      if(type==PathIterator.SEG_CLOSE) {
        append("closepath");                                      //$NON-NLS-1$
      } else if(type==PathIterator.SEG_CUBICTO) {
        append(x1+" "+y1+" "+x2+" "+y2+" "+x3+" "+y3+" curveto"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$ //$NON-NLS-6$
        x0 = x3;
        y0 = y3;
      } else if(type==PathIterator.SEG_LINETO) {
        append(x1+" "+y1+" lineto");                                    //$NON-NLS-1$ //$NON-NLS-2$
        x0 = x1;
        y0 = y1;
      } else if(type==PathIterator.SEG_MOVETO) {
        append(x1+" "+y1+" moveto");                                    //$NON-NLS-1$ //$NON-NLS-2$
        x0 = x1;
        y0 = y1;
      } else if(type==PathIterator.SEG_QUADTO) {
        // Convert the quad curve into a cubic.
        float _x1 = x0+2/3f*(x1-x0);
        float _y1 = y0+2/3f*(y1-y0);
        float _x2 = x1+1/3f*(x2-x1);
        float _y2 = y1+1/3f*(y2-y1);
        float _x3 = x2;
        float _y3 = y2;
        append(_x1+" "+_y1+" "+_x2+" "+_y2+" "+_x3+" "+_y3+" curveto"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$ //$NON-NLS-6$
        x0 = _x3;
        y0 = _y3;
      } else if(type==PathIterator.WIND_EVEN_ODD) {
        // Ignore.
      } else if(type==PathIterator.WIND_NON_ZERO) {
        // Ignore.
      }
      it.next();
    }
    append(action);
    append("newpath"); //$NON-NLS-1$
  }
}