Example usage for java.awt Polygon contains

List of usage examples for java.awt Polygon contains

Introduction

In this page you can find the example usage for java.awt Polygon contains.

Prototype

public boolean contains(Rectangle2D r) 

Source Link

Usage

From source file:de.fhg.igd.mapviewer.waypoints.CustomWaypointPainter.java

/**
 * Find the way-points in a given polygon
 * //www .  j a v a  2  s  . c  om
 * @param poly the polygon
 * @return the way-points in the polygon area
 */
public Set<W> findWaypoints(final Polygon poly) {
    Rectangle viewPort = getMapKit().getMainMap().getViewportBounds();

    final int zoom = getMapKit().getMainMap().getZoom();
    final PixelConverter converter = getMapKit().getMainMap().getTileFactory().getTileProvider().getConverter();

    de.fhg.igd.geom.Point2D[] points = new de.fhg.igd.geom.Point2D[poly.npoints];

    // create a metamodel polygon
    for (int i = 0; i < poly.npoints; i++) {
        int worldX = viewPort.x + poly.xpoints[i];
        int worldY = viewPort.y + poly.ypoints[i];

        // convert to geo position
        GeoPosition pos = converter.pixelToGeo(new Point(worldX, worldY), zoom);

        // convert to common CRS
        try {
            pos = GeotoolsConverter.getInstance().convert(pos, SelectableWaypoint.COMMON_EPSG);
        } catch (IllegalGeoPositionException e) {
            log.warn("Error converting polygon point for query"); //$NON-NLS-1$
            return new HashSet<W>();
        }

        points[i] = new de.fhg.igd.geom.Point2D(pos.getX(), pos.getY());
    }

    final de.fhg.igd.geom.shape.Polygon verifyPolygon = new de.fhg.igd.geom.shape.Polygon(points);

    // we need a 3D search bounding box for the R-Tree
    BoundingBox searchBox = verifyPolygon.getBoundingBox();
    searchBox.setMinZ(-2.0);
    searchBox.setMaxZ(2.0);

    poly.translate(viewPort.x, viewPort.y);
    try {
        Set<W> wps = waypoints.query(searchBox, new Verifier<W, BoundingBox>() {

            @Override
            public boolean verify(W wp, BoundingBox second) {
                try {
                    Point2D wpPixel = converter.geoToPixel(wp.getPosition(), zoom);
                    int dx = (int) wpPixel.getX();
                    int dy = (int) wpPixel.getY();

                    poly.translate(-dx, -dy);
                    try {
                        Area area = wp.getMarker().getArea(zoom);
                        if (area != null) {
                            return area.containedIn(poly);
                        } else {
                            // something that has not been painted yet may
                            // not be selected
                            return false;
                        }
                    } finally {
                        poly.translate(dx, dy);
                    }
                } catch (IllegalGeoPositionException e) {
                    log.warn("Could not convert waypoint position to pixel", e);
                    // fall back to simple method
                    return verifyPolygon.contains(wp.getBoundingBox().toExtent());
                }
            }

        });

        if (wps == null) {
            return new HashSet<W>();
        } else {
            return wps;
        }
    } finally {
        poly.translate(-viewPort.x, -viewPort.y);
    }
}