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(double x, double y) 

Source Link

Usage

From source file:de.uniwue.dmir.heatmap.util.Test.java

public static void main(String[] args) throws Exception {

    GeoPolygonFactoryBean geoPolygonFactoryBean = new GeoPolygonFactoryBean(
            new ClassPathResource("spring/example/points/areas/polygon-turin-p3.json"));

    GeoPolygon geoPolygon = geoPolygonFactoryBean.getObject();

    IDistanceFunction<GeoCoordinates> distanceFunction = new GreatCircleDistance.Haversine();

    EquidistantProjectionTileSizeFactoryBean equidistantProjectionTileSizeFactoryBean = new EquidistantProjectionTileSizeFactoryBean(
            10., 10., false, geoPolygon.getGeoBoundingBox(), distanceFunction);

    TileSize tileSize = equidistantProjectionTileSizeFactoryBean.getObject();

    EquidistantProjection equidistantProjection = new EquidistantProjection(geoPolygon.getGeoBoundingBox(),
            tileSize);/*from w ww .  j av a 2s .  c o  m*/

    PolygonFromGeoPolygonFacoryBean polygonFromGeoPolygonFacoryBean = new PolygonFromGeoPolygonFacoryBean(
            geoPolygon, new TileCoordinates(0, 0, 0), equidistantProjection);

    Polygon polygon = polygonFromGeoPolygonFacoryBean.getObject();

    int pixelsInsidePolygon = 0;
    for (int x = 0; x < tileSize.getWidth(); x++) {
        for (int y = 0; y < tileSize.getHeight(); y++) {
            if (polygon.contains(x, y)) {
                pixelsInsidePolygon++;
            }
        }
    }

    System.out.println(pixelsInsidePolygon + " / " + tileSize.getWidth() * tileSize.getHeight());

    System.out.println(pixelsInsidePolygon * 10. * 10 / 1000 / 1000 + " / "
            + tileSize.getWidth() * tileSize.getHeight() * 10. / 1000 / 1000);
}

From source file:orchestration.path.RectShape.java

@Override
public boolean collidesWith(PlannerShape shape) {
    Polygon otherPoly = shape.getPolygon();

    // quick collision check
    if (!otherPoly.intersects(poly.getBounds()))
        return false;

    // check all line segments for intersection
    for (int i = 0; i < poly.npoints; i++) {
        Vector3D p = createPoint(poly.xpoints[i], poly.ypoints[i]);
        Vector3D r = createLine(p, poly.xpoints[(i + i) % poly.npoints], poly.xpoints[(i + i) % poly.npoints]);

        for (int j = 0; j < otherPoly.npoints; j++) {
            Vector3D q = createPoint(otherPoly.xpoints[i], otherPoly.ypoints[i]);
            Vector3D s = createLine(q, otherPoly.xpoints[(i + i) % otherPoly.npoints],
                    otherPoly.xpoints[(i + i) % otherPoly.npoints]);

            if (testIntersection(p, r, q, s))
                return true;
        }/*from   w  w w .j  ava 2s  .c  o  m*/
    }

    // check if one polygon completely contains the other
    if (otherPoly.contains(poly.xpoints[0], poly.ypoints[0]))
        return true;
    else if (poly.contains(otherPoly.xpoints[0], otherPoly.ypoints[0]))
        return true;

    return false;
}