Java Code Examples for java.awt.geom.Area#intersects()

The following examples show how to use java.awt.geom.Area#intersects() . 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: Bucket.java    From TrakEM2 with GNU General Public License v3.0 6 votes vote down vote up
/** Remove from wherever it is, then test if it's in that bucket, otherwise re-add. */
synchronized final void updatePosition(final Displayable d, final Layer layer, final HashMap<Displayable,HashSet<Bucket>> db_map) {
	final HashSet<Bucket> hs = db_map.get(d);
	final Area a = d.getAreaForBucket(layer);
	final int stack_index = d.getBucketable().getDisplayableList().indexOf(d);
	if (null != hs) {
		for (final Iterator<Bucket> it = hs.iterator(); it.hasNext(); ) {
			final Bucket bu = it.next();
			if (null != a && a.intersects(bu.x, bu.y, bu.w, bu.h)) continue; // bu.intersects(box)) continue; // no change of bucket: lower-right corner still within the bucket
			// else, remove
			bu.map.remove(stack_index);
			it.remove();
		}
	}
	// insert wherever appropriate, if not there
	if (null != a) this.put(stack_index, d, layer, a, db_map);
}
 
Example 2
Source File: SheetScanner.java    From audiveris with GNU Affero General Public License v3.0 6 votes vote down vote up
/**
 * Erase from text image the glyphs that intersect or touch a staff core area.
 * <p>
 * (We also tried to remove too small glyphs, but this led to poor recognition by OCR)
 *
 * @param glyphs all the glyph instances in image
 * @param cores  all staves cores
 */
private void eraseBorderGlyphs (List<Glyph> glyphs,
                                List<Area> cores)
{
    for (Glyph glyph : glyphs) {
        // Check position WRT staves cores
        Rectangle glyphBox = glyph.getBounds();
        glyphBox.grow(1, 1); // To catch touching glyphs (on top of intersecting ones)

        for (Area core : cores) {
            if (core.intersects(glyphBox)) {
                glyph.getRunTable().render(g, glyph.getTopLeft());

                break;
            }
        }
    }
}
 
Example 3
Source File: CurvesBuilder.java    From audiveris with GNU Affero General Public License v3.0 6 votes vote down vote up
/**
 * Report all erased inters in provided system that intersect the provided box.
 *
 * @param system    the system being processed
 * @param box       the box to be intersected
 * @param crossable true for crossable, false for non-crossable
 * @return the collection of erased inter instances with proper crossable characteristic
 */
private Set<Inter> erasedInters (SystemInfo system,
                                 Rectangle box,
                                 boolean crossable)
{
    Set<Inter> found = new LinkedHashSet<>();
    List<Inter> inters = skeleton.getErasedInters(crossable).get(system);

    for (Inter inter : inters) {
        if (inter.getBounds().intersects(box)) {
            Area area = inter.getArea();

            if ((area == null) || area.intersects(box)) {
                found.add(inter);
            }
        }
    }

    return found;
}
 
Example 4
Source File: SystemManager.java    From audiveris with GNU Affero General Public License v3.0 6 votes vote down vote up
/**
 * Report the systems that intersect the provided rectangle
 *
 * @param rect  the provided rectangle
 * @param found (output) list to be populated (allocated if null)
 * @return the containing systems info, perhaps empty but not null
 */
public List<SystemInfo> getSystemsOf (Rectangle2D rect,
                                      List<SystemInfo> found)
{
    if (found != null) {
        found.clear();
    } else {
        found = new ArrayList<>();
    }

    for (SystemInfo system : systems) {
        Area area = system.getArea();

        if ((area != null) && area.intersects(rect)) {
            found.add(system);
        }
    }

    return found;
}
 
Example 5
Source File: PdfGraphics2D.java    From itext2 with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * @see Graphics2D#hit(Rectangle, Shape, boolean)
 */
public boolean hit(Rectangle rect, Shape s, boolean onStroke) {
    if (onStroke) {
        s = stroke.createStrokedShape(s);
    }
    s = transform.createTransformedShape(s);
    Area area = new Area(s);
    if (clip != null)
        area.intersect(clip);
    return area.intersects(rect.x, rect.y, rect.width, rect.height);
}
 
Example 6
Source File: Bucket.java    From TrakEM2 with GNU General Public License v3.0 5 votes vote down vote up
private final void putIn(final int stack_index, final Displayable d, final Area a, final HashMap<Displayable,HashSet<Bucket>> db_map) {
	if (!a.intersects(x, y, w, h)) return;
	// there will be at least one now
	this.empty = false;
	if (null != children) {
		for (final Bucket bu : children) bu.putIn(stack_index, d, a, db_map);
	} else if (null != map) {
		map.put(stack_index, d);
		putToBucketMap(d, db_map); // the db_map
	}
}
 
Example 7
Source File: AreaList.java    From TrakEM2 with GNU General Public License v3.0 5 votes vote down vote up
@Override
public boolean intersects(final Layer layer, final Rectangle r) {
	Object ob = ht_areas.get(layer.getId());
	if (null == ob) return false;
	if (AreaList.UNLOADED == ob) {
		ob = loadLayer(layer.getId());
		if (null == ob) return false;
	}
	final Area a = ((Area)ob).createTransformedArea(this.at);
	return a.intersects(r.x, r.y, r.width, r.height);
}
 
Example 8
Source File: AreaList.java    From TrakEM2 with GNU General Public License v3.0 5 votes vote down vote up
@Override
public boolean linkPatches() {
	unlinkAll(Patch.class);
	// cheap way: intersection of the patches' bounding box with the area
	Rectangle r = new Rectangle();
	boolean must_lock = false;
	for (final Map.Entry<Long,Area> e : ht_areas.entrySet()) {
		final Layer la = this.layer_set.getLayer(e.getKey());
		if (null == la) {
			Utils.log2("AreaList.linkPatches: ignoring null layer for id " + e.getKey());
			continue;
		}
		final Area area = e.getValue().createTransformedArea(this.at);
		for (final Patch d : la.getAll(Patch.class)) {
			r = d.getBoundingBox(r);
			if (area.intersects(r)) {
				link(d, true);
				if (d.locked) must_lock = true;
			}
		}
	}

	// set the locked flag to this and all linked ones
	if (must_lock && !locked) {
		setLocked(true);
		return true;
	}

	return false;
}
 
Example 9
Source File: PdfGraphics2D.java    From pentaho-reporting with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * @see Graphics2D#hit(Rectangle, Shape, boolean)
 */
@Override
public boolean hit( final Rectangle rect, Shape s, final boolean onStroke ) {
  if ( onStroke ) {
    s = stroke.createStrokedShape( s );
  }
  s = transform.createTransformedShape( s );
  final Area area = new Area( s );
  if ( clip != null ) {
    area.intersect( clip );
  }
  return area.intersects( rect.x, rect.y, rect.width, rect.height );
}
 
Example 10
Source File: AbstractGraphics2D.java    From pumpernickel with MIT License 5 votes vote down vote up
@Override
public boolean hit(Rectangle rect, Shape s, boolean onStroke) {
	if (onStroke) {
		return hit(rect, getStroke().createStrokedShape(s), false);
	}

	Area area = new Area(s);
	return area.intersects(rect);
}
 
Example 11
Source File: VectorGraphics2D.java    From pumpernickel with MIT License 5 votes vote down vote up
@Override
public boolean hit(Rectangle rect, Shape s, boolean onStroke) {
	if (onStroke) {
		return hit(rect, getStroke().createStrokedShape(s), false);
	}

	Area area = new Area(s);
	return area.intersects(rect);
}
 
Example 12
Source File: Glyphs.java    From audiveris with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * Look up in a collection of glyph instances for <b>all</b> glyph
 * instances intersected by a provided area.
 *
 * @param collection the collection of glyph instances to be browsed
 * @param area       the intersecting area
 * @return the glyph instances found, which may be an empty list
 */
public static Set<Glyph> intersectedGlyphs (Collection<? extends Glyph> collection,
                                            Area area)
{
    Set<Glyph> set = new LinkedHashSet<>();

    for (Glyph glyph : collection) {
        if (area.intersects(glyph.getBounds())) {
            set.add(glyph);
        }
    }

    return set;
}
 
Example 13
Source File: NoteHeadsBuilder.java    From audiveris with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * Build the list of areas around connectors and frozen barlines.
 *
 * @return the bar-centered areas
 */
private List<Area> getBarAreas (Area area)
{
    List<Area> kept = new ArrayList<>();
    for (Area r : systemBarAreas) {
        if (area.intersects(r.getBounds())) {
            kept.add(r);
        }
    }
    return kept;
}
 
Example 14
Source File: NoteHeadsBuilder.java    From audiveris with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * Check whether the provided rectangle would intersect area of frozen
 * barline/connector.
 *
 * @param rect provided rectangle
 * @return true if area hit
 */
private boolean barInvolved (Rectangle rect)
{
    for (Area a : barAreas) {
        if (a.intersects(rect)) {
            return true;
        }
    }

    return false;
}
 
Example 15
Source File: SlurLinker.java    From audiveris with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * Retrieve the best head embraced by the slur side.
 *
 * @param slur   the provided slur
 * @param side   desired side
 * @param area   lookup area on slur side
 * @param chords candidate chords on desired side
 * @return the map of heads found, perhaps empty, with their data
 */
private Map<Inter, SlurHeadLink> lookup (SlurInter slur,
                                         HorizontalSide side,
                                         Area area,
                                         List<Inter> chords)
{
    final Map<Inter, SlurHeadLink> found = new HashMap<>();
    final SlurInfo info = slur.getInfo();
    final Point end = info.getEnd(side == LEFT);
    final Point target = getTargetPoint(slur, side);
    final Point2D bisUnit = info.getBisUnit();

    // Look for intersected chords
    for (Inter chordInter : chords) {
        AbstractChordInter chord = (AbstractChordInter) chordInter;
        Rectangle chordBox = chord.getBounds();

        if (area.intersects(chordBox)) {
            // Check the chord contains at least one suitable head on desired slur side
            HeadInter head = selectBestHead(slur, chord, end, target, bisUnit, area);

            if (head != null) {
                found.put(chord, SlurHeadLink.create(target, side, chord, head));
            }
        }
    }

    return found;
}
 
Example 16
Source File: PdfGraphics2D.java    From gcs with Mozilla Public License 2.0 5 votes vote down vote up
/**
 * @see Graphics2D#hit(Rectangle, Shape, boolean)
 */
@Override
public boolean hit(Rectangle rect, Shape s, boolean onStroke) {
	if (onStroke) {
		s = stroke.createStrokedShape(s);
	}
	s = transform.createTransformedShape(s);
	Area area = new Area(s);
	if (clip != null) {
		area.intersect(clip);
	}
	return area.intersects(rect.x, rect.y, rect.width, rect.height);
}
 
Example 17
Source File: Inters.java    From audiveris with GNU Affero General Public License v3.0 4 votes vote down vote up
/**
 * Lookup the provided list of interpretations for those whose bounds
 * intersect the given area.
 *
 * @param inters the list of interpretations to search for
 * @param order  if the list is already sorted by some order, this may speedup the search
 * @param area   the intersecting area
 * @return the intersected interpretations found, perhaps empty but not null
 */
public static List<Inter> intersectedInters (List<Inter> inters,
                                             GeoOrder order,
                                             Area area)
{
    List<Inter> found = new ArrayList<>();
    Rectangle bounds = area.getBounds();
    double xMax = bounds.getMaxX();
    double yMax = bounds.getMaxY();

    for (Inter inter : inters) {
        if (inter.isRemoved()) {
            continue;
        }

        Rectangle iBox = inter.getBounds();

        if (area.intersects(iBox)) {
            found.add(inter);
        } else {
            switch (order) {
            case BY_ABSCISSA:

                if (iBox.x > xMax) {
                    return found;
                }

                break;

            case BY_ORDINATE:

                if (iBox.y > yMax) {
                    return found;
                }

                break;

            case NONE:
            }
        }
    }

    return found;
}
 
Example 18
Source File: PDFVisibleTextStripper.java    From testarea-pdfbox2 with Apache License 2.0 3 votes vote down vote up
/**
 * <p>
 * Due to different rounding of numbers in the clip path and text transformations,
 * it can happen that the glyph origin is exactly on the border of the clip path
 * but the {@link Area#contains(double, double)} method of the determined clip
 * path returns false for the determined origin coordinates.
 * </p>
 * <p>
 * To fix this, this method generates a small rectangle around the (glyph origin)
 * coordinates and checks whether this rectangle intersects the (clip path) area.
 * </p>
 */
protected boolean contains(Area area, float x, float y) {
    if (useFatGlyphOrigin) {
        double length = .0002;
        double up = 1.0001;
        double down = .9999;
        return area.intersects(x < 0 ? x*up : x*down, y < 0 ? y*up : y*down, Math.abs(x*length), Math.abs(y*length));
    } else
        return area.contains(x, y);
}
 
Example 19
Source File: AbstractGraphics2D.java    From pentaho-reporting with GNU Lesser General Public License v2.1 3 votes vote down vote up
/**
 * Checks whether or not the specified <code>Shape</code> intersects the specified {@link java.awt.Rectangle}, which
 * is in device space. If <code>onStroke</code> is false, this method checks whether or not the interior of the
 * specified <code>Shape</code> intersects the specified <code>Rectangle</code>. If <code>onStroke</code> is
 * <code>true</code>, this method checks whether or not the <code>Stroke</code> of the specified <code>Shape</code>
 * outline intersects the specified <code>Rectangle</code>. The rendering attributes taken into account include the
 * <code>Clip</code>, <code>Transform</code>, and <code>Stroke</code> attributes.
 *
 * @param rect
 *          the area in device space to check for a hit
 * @param s
 *          the <code>Shape</code> to check for a hit
 * @param onStroke
 *          flag used to choose between testing the stroked or the filled shape. If the flag is <code>true</code>, the
 *          <code>Stroke</code> oultine is tested. If the flag is <code>false</code>, the filled <code>Shape</code> is
 *          tested.
 * @return <code>true</code> if there is a hit; <code>false</code> otherwise.
 * @see #setStroke
 * @see #fill
 * @see #draw
 * @see #transform
 * @see #setTransform
 * @see #clip
 * @see #setClip
 */
public boolean hit( final Rectangle rect, Shape s, final boolean onStroke ) {
  if ( onStroke ) {
    s = getStroke().createStrokedShape( s );
  }
  s = getTransform().createTransformedShape( s );
  final Area area = new Area( s );
  final Shape clip = getClip();
  if ( clip != null ) {
    area.intersect( new Area( clip ) );
  }
  return area.intersects( rect.x, rect.y, rect.width, rect.height );
}
 
Example 20
Source File: PDFGraphics.java    From jpexs-decompiler with GNU General Public License v3.0 votes vote down vote up
/**
   * @see Graphics2D#hit(Rectangle, Shape, boolean)
   */
  public boolean hit(Rectangle rect, Shape s, boolean onStroke) {
      if (onStroke) {
          s = stroke.createStrokedShape(s);
      }
      s = transform.createTransformedShape(s);
      Area area = new Area(s);
      if (clip != null)
          area.intersect(clip);
      return area.intersects(rect.x, rect.y, rect.width, rect.height);
  }