Java Code Examples for java.awt.Polygon#intersects()

The following examples show how to use java.awt.Polygon#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: Measure.java    From audiveris with GNU Affero General Public License v3.0 6 votes vote down vote up
/**
 * Look up for a potential rest interleaved between the given stemmed chords
 *
 * @param left  the chord on the left of the area
 * @param right the chord on the right of the area
 * @return the rest found, or null otherwise
 */
public RestInter lookupRest (AbstractChordInter left,
                             AbstractChordInter right)
{
    // Define the area limited by the left and right chords with their stems
    // and check for intersection with a rest note
    Polygon polygon = new Polygon();
    polygon.addPoint(left.getHeadLocation().x, left.getHeadLocation().y);
    polygon.addPoint(left.getTailLocation().x, left.getTailLocation().y);
    polygon.addPoint(right.getTailLocation().x, right.getTailLocation().y);
    polygon.addPoint(right.getHeadLocation().x, right.getHeadLocation().y);

    for (RestChordInter restChord : getRestChords()) {
        for (Inter inter : restChord.getMembers()) {
            Rectangle box = inter.getBounds();

            if (polygon.intersects(box.x, box.y, box.width, box.height)) {
                return (RestInter) inter;
            }
        }
    }

    return null;
}
 
Example 2
Source File: PageXmlUtils.java    From TranskribusCore with GNU General Public License v3.0 6 votes vote down vote up
public static boolean doesIntersect(TextLineType tl, String baseline) {
		final String linePoints = tl.getCoords().getPoints();
		Polygon linePoly = PointStrUtils.buildPolygon(linePoints);
		Polygon baselinePoly = PointStrUtils.buildPolygon(baseline);
//		logger.debug(linePoly.getBounds2D().toString());
//		logger.debug(baselinePoly.getBounds2D().toString());
		Rectangle2D baselineRect = baselinePoly.getBounds2D();
		if(baselineRect.getHeight() == 0) {
			/*
			 * if the baseline is horizontal, the boundRect includes no area and thus
			 * there will not be an intersection...
			 */
			baselineRect.setRect(
					baselineRect.getX(), 
					baselineRect.getY(), 
					baselineRect.getWidth(), 
					1); //blow this up to be height 1
		}
		return linePoly.intersects(baselineRect);
	}
 
Example 3
Source File: TerritoryOverLayDrawable.java    From triplea with GNU General Public License v3.0 6 votes vote down vote up
@Override
public void draw(
    final Rectangle bounds,
    final GameData data,
    final Graphics2D graphics,
    final MapData mapData) {
  final Territory territory = data.getMap().getTerritory(territoryName);
  final List<Polygon> polygons = mapData.getPolygons(territory);
  graphics.setColor(color);
  for (final Polygon polygon : polygons) {
    if (!polygon.intersects(bounds) && !polygon.contains(bounds)) {
      continue;
    }

    final Polygon translatedPolygon = Util.translatePolygon(polygon, -bounds.x, -bounds.y);
    if (operation == Operation.FILL) {
      graphics.fillPolygon(translatedPolygon);
    } else {
      graphics.drawPolygon(translatedPolygon);
    }
  }
}
 
Example 4
Source File: TerritoryDrawable.java    From triplea with GNU General Public License v3.0 6 votes vote down vote up
protected static void draw(
    final Rectangle bounds,
    final Graphics2D graphics,
    final MapData mapData,
    final Territory territory,
    final Paint territoryPaint) {
  final List<Polygon> polygons = mapData.getPolygons(territory);
  for (final Polygon polygon : polygons) {
    if (!polygon.intersects(bounds) && !polygon.contains(bounds)) {
      continue;
    }

    final Polygon translatedPolygon = Util.translatePolygon(polygon, -bounds.x, -bounds.y);
    graphics.setPaint(territoryPaint);
    graphics.fillPolygon(translatedPolygon);
    graphics.setColor(Color.BLACK);
    graphics.drawPolygon(translatedPolygon);
  }
}
 
Example 5
Source File: SeaZoneOutlineDrawable.java    From triplea with GNU General Public License v3.0 6 votes vote down vote up
@Override
public void draw(
    final Rectangle bounds,
    final GameData data,
    final Graphics2D graphics,
    final MapData mapData) {
  final Territory territory = data.getMap().getTerritory(territoryName);
  final List<Polygon> polys = mapData.getPolygons(territory);
  graphics.setColor(Color.BLACK);
  for (final Polygon polygon : polys) {
    if (!polygon.intersects(bounds) && !polygon.contains(bounds)) {
      continue;
    }

    graphics.drawPolygon(Util.translatePolygon(polygon, -bounds.x, -bounds.y));
  }
}
 
Example 6
Source File: SystemsBuilder.java    From libreveris with GNU Lesser General Public License v3.0 5 votes vote down vote up
private BrokenLine refineBorder (BrokenLine border,
                                 SystemInfo prevSystem,
                                 SystemInfo system)
{
    // Define the inter-system yellow zone
    int yellowDy = sheet.getScale().toPixels(constants.yellowZoneHalfHeight);
    Polygon polygon = new Polygon();
    Point left = border.getPoint(0);
    Point right = border.getPoint(1);
    polygon.addPoint(left.x, left.y - yellowDy);
    polygon.addPoint(right.x, right.y - yellowDy);
    polygon.addPoint(right.x, right.y + yellowDy);
    polygon.addPoint(left.x, left.y + yellowDy);

    // Look for glyphs intersected by this yellow zone
    List<Glyph> intersected = new ArrayList<>();

    for (Glyph glyph : nest.getActiveGlyphs()) {
        if (polygon.intersects(glyph.getBounds())) {
            intersected.add(glyph);
        }
    }

    logger.debug("S#{}-{} : {}{}", prevSystem.getId(),
            system.getId(), polygon.getBounds(),
            Glyphs.toString(" inter:", intersected));

    // If the yellow zone is empty, keep the border
    // Otherwise, use the more complex approach
    if (intersected.isEmpty()) {
        return border;
    } else {
        return new BorderBuilder(sheet, prevSystem, system).buildBorder();
    }
}
 
Example 7
Source File: Chord.java    From libreveris with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * Look up for a potential rest interleaved between the given stemed
 * chords
 *
 * @param left  the chord on the left of the area
 * @param right the chord on the right of the area
 * @return the rest found, or null otherwise
 */
public static Note lookupRest (Chord left,
                               Chord right)
{
    // Define the area limited by the left and right chords with their stems
    // and check for intersection with a rest note
    Polygon polygon = new Polygon();
    polygon.addPoint(left.headLocation.x, left.headLocation.y);
    polygon.addPoint(left.tailLocation.x, left.tailLocation.y);
    polygon.addPoint(right.tailLocation.x, right.tailLocation.y);
    polygon.addPoint(right.headLocation.x, right.headLocation.y);

    for (TreeNode node : left.getMeasure().getChords()) {
        Chord chord = (Chord) node;

        // Not interested in the bounding chords
        if ((chord == left) || (chord == right)) {
            continue;
        }

        for (TreeNode n : chord.getNotes()) {
            Note note = (Note) n;

            // Interested in rest notes only
            if (note.isRest()) {
                Rectangle box = note.getBox();

                if (polygon.intersects(box.x, box.y, box.width, box.height)) {
                    return note;
                }
            }
        }
    }

    return null;
}
 
Example 8
Source File: Displayable.java    From TrakEM2 with GNU General Public License v3.0 5 votes vote down vote up
/** Link the Patch objects that lay underneath the bounding box of this Displayable, so that they cannot be dragged independently.
 *  @return whether the locking state changed. */
public boolean linkPatches() {
	// find the patches that don't lay under other profiles of this profile's linking group, and make sure they are unlinked. This will unlink any Patch objects under this Profile:
	unlinkAll(Patch.class);

	// scan the Display and link Patch objects that lay under this Profile's bounding box:

	// catch all displayables of the current Layer
	final ArrayList<Displayable> al = layer.getDisplayables(Patch.class);

	// this bounding box:
	final Polygon perimeter = getPerimeter(); //displaced by this object's position!
	if (null == perimeter) return false; // happens when a profile with zero points is deleted

	// for each Patch, check if it underlays this profile's bounding box
	Rectangle box = new Rectangle();
	boolean must_lock = false;
	for (final Displayable displ : al) {
		// stupid java, Polygon cannot test for intersection with another Polygon !! //if (perimeter.intersects(displ.getPerimeter())) // TODO do it yourself: check if a Displayable intersects another Displayable
		if (perimeter.intersects(displ.getBoundingBox(box))) {
			// Link the patch
			this.link(displ);
			if (displ.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: Pipe.java    From TrakEM2 with GNU General Public License v3.0 5 votes vote down vote up
@Override
synchronized public boolean isRoughlyInside(final Layer layer, final Rectangle r) {
	final Polygon[] pols = getSubPerimeters(layer); // in world coords
	if (null == pols) return false;
	for (final Polygon pol : pols) {
		if (pol.intersects(r)) return true;
	}
	return false;
}
 
Example 10
Source File: Geometry.java    From rcrs-server with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
public static boolean boundingTest(Polygon p,int x,int y,int w,int h){
	rect.setBounds(x,y,w,h);
	return p.intersects(rect);		
}
 
Example 11
Source File: Chord.java    From libreveris with GNU Lesser General Public License v3.0 4 votes vote down vote up
/**
 * Look up for all chords interleaved between the given stemed chords
 *
 * @param left  the chord on the left of the area
 * @param right the chord on the right of the area
 * @return the collection of interleaved chords, which may be empty
 */
public static SortedSet<Chord> lookupInterleavedChords (Chord left,
                                                        Chord right)
{
    SortedSet<Chord> found = new TreeSet<>(Chord.byAbscissa);

    if ((left == null) || (right == null)) {
        return found; // Safer
    }

    // Define the area limited by the left and right chords with their stems
    // and check for intersection with a rest note
    // More precisely, we use the area half on the tail side.
    // And we check that the interleaved chords have the same stem dir
    Polygon polygon = new Polygon();
    polygon.addPoint(left.getHeadLocation().x, left.getCenter().y);
    polygon.addPoint(left.getTailLocation().x, left.getTailLocation().y);
    polygon.addPoint(right.getTailLocation().x, right.getTailLocation().y);
    polygon.addPoint(right.getHeadLocation().x, right.getCenter().y);

    for (TreeNode node : left.getMeasure().getChords()) {
        Chord chord = (Chord) node;

        // Not interested in the bounding chords (TBC)
        if ((chord == left) || (chord == right)) {
            continue;
        }

        // Additional check on stem dir, if left & right agree
        if (left.getStemDir() == right.getStemDir()) {
            if (chord.getReferencePoint() == null
                || (chord.getStemDir() != 0 && chord.getStemDir() != left.getStemDir())) {
                continue;
            }
        }

        Rectangle box = chord.getBox();

        if (polygon.intersects(box.x, box.y, box.width, box.height)) {
            found.add(chord);
        }
    }

    return found;
}