Search in sources :

Example 91 with Polygon

use of java.awt.Polygon in project hale by halestudio.

the class CustomWaypointPainter method findWaypoints.

/**
 * Find the way-points in a given polygon
 *
 * @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) {
            // $NON-NLS-1$
            log.warn("Error converting polygon point for query");
            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 {
                            // 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);
    }
}
Also used : Rectangle(java.awt.Rectangle) IllegalGeoPositionException(org.jdesktop.swingx.mapviewer.IllegalGeoPositionException) Point(java.awt.Point) Point(java.awt.Point) Area(de.fhg.igd.mapviewer.marker.area.Area) Point2D(java.awt.geom.Point2D) BoundingBox(de.fhg.igd.geom.BoundingBox) PixelConverter(org.jdesktop.swingx.mapviewer.PixelConverter) GeoPosition(org.jdesktop.swingx.mapviewer.GeoPosition) Polygon(java.awt.Polygon) HashSet(java.util.HashSet)

Example 92 with Polygon

use of java.awt.Polygon in project hale by halestudio.

the class CircleMarker method paintMarker.

/**
 * @see SimpleMarker#paintMarker(Object)
 */
@Override
protected Area paintMarker(T context) {
    int maxSize = Math.max(2 * 11, size + 2);
    Graphics2D g = beginPainting(maxSize, maxSize, maxSize / 2, maxSize / 2);
    try {
        g.setPaint(getPaintColor(context));
        g.fillOval(-size / 2, -size / 2, size, size);
        g.setColor(getBorderColor(context));
        g.drawOval(-size / 2 - 1, -size / 2 - 1, size + 1, size + 1);
        if (showMarker(context)) {
            g.setPaint(getMarkerColor(context));
            Polygon triangle = new Polygon();
            triangle.addPoint(0, 0);
            triangle.addPoint(-11, -11);
            triangle.addPoint(11, -11);
            g.fill(triangle);
            return new PolygonArea(new Polygon(new int[] { -11, 11 + 1, size / 2 + 1, -size / 2 - 1 }, new int[] { -11, -11, size / 2 + 1, size / 2 + 1 }, 4));
        } else
            return new PolygonArea(new Polygon(new int[] { -size / 2 - 1, size / 2 + 1, size / 2 + 1, -size / 2 - 1 }, new int[] { -size / 2 - 1, -size / 2 - 1, size / 2 + 1, size / 2 + 1 }, 4));
    } finally {
        endPainting(g);
    }
}
Also used : PolygonArea(de.fhg.igd.mapviewer.marker.area.PolygonArea) Polygon(java.awt.Polygon) Graphics2D(java.awt.Graphics2D)

Example 93 with Polygon

use of java.awt.Polygon in project hale by halestudio.

the class LabelMarker method paintMarker.

/**
 * @see SimpleMarker#paintMarker(java.lang.Object)
 */
@Override
protected Area paintMarker(T context) {
    String name = getName(context);
    Graphics2D dummy = getGraphicsDummy();
    int width = (int) dummy.getFontMetrics().getStringBounds(name, dummy).getWidth();
    Graphics2D g = beginPainting(width + 10, 30, width / 2 + 5, 0);
    try {
        g.setPaint(getPaintColor(context));
        Polygon triangle = new Polygon();
        triangle.addPoint(0, 0);
        triangle.addPoint(11, 11);
        triangle.addPoint(-11, 11);
        g.fill(triangle);
        g.fillRoundRect(-width / 2 - 5, 10, width + 10, 20, 10, 10);
        // g.setColor(borderColor);
        // g.drawRoundRect(-width/2 -5, 10, width+10, 20, 10, 10);
        // bounding polygon
        // (0,0), (11,11), (width/2 + 5, 10), (width/2 + 5, 30),
        // (-width/2 - 5, 30), (-width/2 - 5, 10), (-11, 11)
        Polygon bounds = new Polygon(new int[] { 0, 11, width / 2 + 5, width / 2 + 5, -width / 2 - 5, -width / 2 - 5, -11 }, new int[] { 0, 11, 10, 30, 30, 10, 11 }, 7);
        // draw text w/ shadow
        g.setPaint(Color.BLACK);
        // shadow
        g.drawString(name, -width / 2 - 1, 26 - 1);
        // g.drawString(name, -width/2-1, 26-1); //shadow
        g.setPaint(Color.WHITE);
        // text
        g.drawString(name, -width / 2, 26);
        return new PolygonArea(bounds);
    } finally {
        endPainting(g);
    }
}
Also used : PolygonArea(de.fhg.igd.mapviewer.marker.area.PolygonArea) Polygon(java.awt.Polygon) Graphics2D(java.awt.Graphics2D)

Example 94 with Polygon

use of java.awt.Polygon in project litiengine by gurkenlabs.

the class LightSource method getObstructedVisionArea.

private static Area getObstructedVisionArea(final IEntity mob, final Point2D center) {
    final Polygon shadowPolygon = new Polygon();
    final Ellipse2D shadowEllipse = getShadowEllipse(mob);
    final Rectangle2D bounds = shadowEllipse.getBounds2D();
    // radius of Entity's bounding circle
    final float r = (float) bounds.getWidth() / 2f;
    final float ry = (float) bounds.getHeight() / 2f;
    // get relative center of entity
    final Point2D relativeCenter = Game.world().camera().getViewportLocation(new Point((int) (bounds.getX() + r), (int) (bounds.getY() + ry)));
    final double cx = relativeCenter.getX();
    final double cy = relativeCenter.getY();
    // get direction from light to entity center
    final double dx = cx - center.getX();
    final double dy = cy - center.getY();
    // get euclidean distance from entity to center
    // avoid sqrt for performance
    final double distSq = dx * dx + dy * dy;
    // normalize the direction to a unit vector
    final float len = (float) Math.sqrt(distSq);
    double nx = dx;
    double ny = dy;
    if (len != 0) {
        // avoid division by 0
        nx /= len;
        ny /= len;
    }
    // get perpendicular of unit vector
    final double px = -ny;
    final double py = nx;
    // our perpendicular points in either direction from radius
    final Point2D.Double pointA = new Point2D.Double(cx - px * r, cy - py * ry);
    final Point2D.Double pointB = new Point2D.Double(cx + px * r, cy + py * ry);
    // project the points by our SHADOW_EXTRUDE amount
    final Point2D pointC = GeometricUtilities.project(center, pointA, OBSTRUCTED_VISION_RADIUS);
    final Point2D pointD = GeometricUtilities.project(center, pointB, OBSTRUCTED_VISION_RADIUS);
    // construct a polygon from our points
    shadowPolygon.reset();
    shadowPolygon.addPoint((int) pointA.getX(), (int) pointA.getY());
    shadowPolygon.addPoint((int) pointB.getX(), (int) pointB.getY());
    shadowPolygon.addPoint((int) pointD.getX(), (int) pointD.getY());
    shadowPolygon.addPoint((int) pointC.getX(), (int) pointC.getY());
    final Point2D shadowRenderLocation = Game.world().camera().getViewportLocation(new Point2D.Double(shadowEllipse.getX(), shadowEllipse.getY()));
    final Ellipse2D relativeEllipse = new Ellipse2D.Double(shadowRenderLocation.getX(), shadowRenderLocation.getY(), shadowEllipse.getWidth(), shadowEllipse.getHeight());
    final Area ellipseArea = new Area(relativeEllipse);
    final Area shadowArea = new Area(shadowPolygon);
    shadowArea.add(ellipseArea);
    return shadowArea;
}
Also used : Area(java.awt.geom.Area) Point2D(java.awt.geom.Point2D) Rectangle2D(java.awt.geom.Rectangle2D) Point(java.awt.Point) Polygon(java.awt.Polygon) Ellipse2D(java.awt.geom.Ellipse2D)

Example 95 with Polygon

use of java.awt.Polygon in project OsmAnd-tools by osmandapp.

the class CoastlinesLayer method getPolygon.

public Polygon getPolygon(int tileX, int tileY, int z) {
    int pixX = (int) (MapUtils.getPixelShiftX(map.getZoom(), MapUtils.getLongitudeFromTile(z, tileX), map.getLongitude(), map.getTileSize()) + map.getCenterPointX());
    int pixY = (int) (MapUtils.getPixelShiftY(map.getZoom(), MapUtils.getLatitudeFromTile(z, tileY), map.getLatitude(), map.getTileSize()) + map.getCenterPointY());
    int pixsX = (int) (MapUtils.getPixelShiftX(map.getZoom(), MapUtils.getLongitudeFromTile(z, tileX + 1), map.getLongitude(), map.getTileSize()) + map.getCenterPointX());
    int pixsY = (int) (MapUtils.getPixelShiftY(map.getZoom(), MapUtils.getLatitudeFromTile(z, tileY + 1), map.getLatitude(), map.getTileSize()) + map.getCenterPointY());
    return new Polygon(new int[] { pixX, pixsX, pixsX, pixX, pixX }, new int[] { pixY, pixY, pixsY, pixsY, pixY }, 5);
}
Also used : Polygon(java.awt.Polygon) Point(java.awt.Point)

Aggregations

Polygon (java.awt.Polygon)192 Point (java.awt.Point)53 Rectangle (java.awt.Rectangle)30 Graphics2D (java.awt.Graphics2D)28 Area (java.awt.geom.Area)22 ArrayList (java.util.ArrayList)21 BasicStroke (java.awt.BasicStroke)20 Color (java.awt.Color)19 Paint (java.awt.Paint)15 Point2D (java.awt.geom.Point2D)13 AffineTransform (java.awt.geom.AffineTransform)12 Rectangle2D (java.awt.geom.Rectangle2D)12 Shape (java.awt.Shape)11 LocalPoint (net.runelite.api.coords.LocalPoint)11 Point (net.runelite.api.Point)10 FloatPolygon (ij.process.FloatPolygon)9 Font (java.awt.Font)8 List (java.util.List)8 FontMetrics (java.awt.FontMetrics)7 Stroke (java.awt.Stroke)7