下面列出了java.awt.geom.Area#intersects ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* 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;
}
}
}
}
/**
* 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;
}
/**
* 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;
}
/** 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);
}
/**
* @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);
}
/**
* @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);
}
/**
* 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;
}
/**
* 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;
}
/**
* 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;
}
/**
* 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;
}
@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);
}
@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);
}
/**
* @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 );
}
@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;
}
@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);
}
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
}
}
/**
* 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;
}
/**
* <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);
}
/**
* 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 );
}
/**
* @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);
}