Search in sources :

Example 46 with Polygon

use of java.awt.Polygon in project TrakEM2 by trakem2.

the class AreaUtils method singularInterpolation.

/**
 * Extract the Area of the image for the given pixel value.
 *  ThresholdToSelection is way faster than this, just use it.
 *  It's BROKEN do not use.
 */
/*
	static public final Area extractArea(final ImageProcessor ip, final float val) {
		final int height = ip.getHeight();
		final int width = ip.getWidth();

		final Area area = new Area();
		final ArrayList<Area> segments = new ArrayList<Area>();
		final Rectangle box = new Rectangle(0, 0, 1, 1);

		for (int y=0; y<height; y++) {
			float prev = ip.getPixelValue(0, y);
			box.x = 0;
			box.y = y;
			box.width = val == prev ? 1 : 0;

			for (int x=1; x<width; x++) {

				float pix = ip.getPixelValue(x, y);

				if (val == pix) {
					if (pix == prev) {
						box.width++;
						continue;
					}
					// Else, add previous one
					segments.add(new Area(box));
					// ... and start a new box
					box.x = x;
					box.y = y;
					box.width = 1;
					prev = pix;
				}
			}

			// At end of line, add the last
			segments.add(new Area(box));

			// Join a few
			if (segments.size() > 32) {
				final Area a = new Area(segments.get(0));
				final int len = segments.size();
				for (int i=1; i<len; i++) a.add(segments.get(i));
				area.add(a);
				segments.clear();
			}
		}

		if (segments.size() > 0) {
			final Area a = new Area(segments.get(0));
			final int len = segments.size();
			for (int i=1; i<len; i++) a.add(segments.get(i));
			area.add(a);
		}

		return area;
	}
	*/
/**
 * Interpolate areas only if they are made of a single shape each.
 *  Assumes that areas are in the same coordinate system.
 * @throws Exception
 */
public static final Area[] singularInterpolation(final Area a1, final Area a2, final int nInterpolates) throws Exception {
    if (!a1.isSingular() || !a2.isSingular()) {
        return null;
    }
    final VectorString2D vs1 = M.asVectorString2D(M.getPolygons(a1).iterator().next(), 0);
    final VectorString2D vs2 = M.asVectorString2D(M.getPolygons(a2).iterator().next(), 1);
    final Editions ed = new Editions(vs1, vs2, Math.min(vs1.getAverageDelta(), vs2.getAverageDelta()), true);
    final double[][][] d = SkinMaker.getMorphedPerimeters(vs1, vs2, nInterpolates, ed);
    final Area[] a = new Area[d.length];
    for (int i = 0; i < d.length; i++) {
        final double[] x = d[i][0];
        final double[] y = d[i][1];
        final int[] xi = new int[x.length];
        final int[] yi = new int[y.length];
        for (int k = 0; k < x.length; k++) {
            xi[k] = (int) x[k];
            yi[k] = (int) y[k];
        }
        a[i] = new Area(new Polygon(xi, yi, xi.length));
    }
    return a;
}
Also used : Area(java.awt.geom.Area) Editions(ini.trakem2.vector.Editions) VectorString2D(ini.trakem2.vector.VectorString2D) Polygon(java.awt.Polygon)

Example 47 with Polygon

use of java.awt.Polygon in project TrakEM2 by trakem2.

the class AreaTree method findEventReceiver.

private AreaNode findEventReceiver(final Collection<Node<Area>> nodes, final int lx, final int ly, final Layer layer, final double mag, final InputEvent ie) {
    Area brush = null;
    try {
        brush = AreaWrapper.makeMouseBrush(ProjectToolbar.getBrushSize(), mag).createTransformedArea(this.at.createInverse());
    } catch (final Exception e) {
        IJError.print(e);
        return null;
    }
    // Try to find an area onto which the point intersects, or the brush diameter
    synchronized (node_layer_map) {
        AreaNode closest = null;
        double min_dist = Double.MAX_VALUE;
        for (final AreaNode an : (Collection<AreaNode>) (Collection) nodes) {
            // nodes are the nodes in the current layer
            if (brush.contains(an.x, an.y) || M.intersects(an.getData(), brush)) {
                return an;
            }
            if (null == an.aw)
                continue;
            // Look inside holes, for filling
            final Collection<Polygon> pols = M.getPolygons(an.getData());
            for (final Polygon pol : pols) {
                if (pol.contains(lx, ly)) {
                    return an;
                }
            }
            // If erasing, find the closest area to the brush
            if (ie.isAltDown()) {
                for (final Polygon pol : pols) {
                    for (int i = 0; i < pol.npoints; i++) {
                        final double sqdist = Math.pow(lx - pol.xpoints[i], 2) + Math.pow(ly - pol.ypoints[i], 2);
                        if (sqdist < min_dist) {
                            closest = an;
                            min_dist = sqdist;
                        }
                    }
                }
            }
        }
        if (null != closest)
            return closest;
    }
    /* // IT'S CONFUSING when there's more than one node per layer
		if (null != receiver && layer == receiver.la) {
			Rectangle srcRect = Display.getFront().getCanvas().getSrcRect();
			if (receiver.getData().createTransformedArea(this.at).intersects(srcRect)) {
				// paint on last area, its in this layer and within current view
				return receiver;
			}
		}
		*/
    return null;
}
Also used : Area(java.awt.geom.Area) Collection(java.util.Collection) Polygon(java.awt.Polygon) NoninvertibleTransformException(java.awt.geom.NoninvertibleTransformException) Point(java.awt.Point)

Example 48 with Polygon

use of java.awt.Polygon in project TrakEM2 by trakem2.

the class AreaWrapper method fillHoles.

public void fillHoles() {
    Polygon pol = new Polygon();
    for (PathIterator pit = area.getPathIterator(null); !pit.isDone(); ) {
        float[] coords = new float[6];
        int seg_type = pit.currentSegment(coords);
        switch(seg_type) {
            case PathIterator.SEG_MOVETO:
            case PathIterator.SEG_LINETO:
                pol.addPoint((int) coords[0], (int) coords[1]);
                break;
            case PathIterator.SEG_CLOSE:
                area.add(new Area(pol));
                // prepare next:
                pol = new Polygon();
                break;
            default:
                Utils.log2("WARNING: unhandled seg type.");
                break;
        }
        pit.next();
        if (pit.isDone()) {
            break;
        }
    }
}
Also used : Area(java.awt.geom.Area) PathIterator(java.awt.geom.PathIterator) Polygon(java.awt.Polygon) FloatPolygon(ij.process.FloatPolygon) Point(java.awt.Point)

Example 49 with Polygon

use of java.awt.Polygon in project runelite by runelite.

the class CannonOverlay method drawDoubleHitSpots.

/**
 * Draw the double hit spots on a 6 by 6 grid around the cannon
 * @param startTile The position of the cannon
 */
private void drawDoubleHitSpots(Graphics2D graphics, LocalPoint startTile, Color color) {
    for (int x = -3; x <= 3; x++) {
        for (int y = -3; y <= 3; y++) {
            if (y != 1 && x != 1 && y != -1 && x != -1) {
                continue;
            }
            // Ignore center square
            if (y >= -1 && y <= 1 && x >= -1 && x <= 1) {
                continue;
            }
            int xPos = startTile.getX() - (x * LOCAL_TILE_SIZE);
            int yPos = startTile.getY() - (y * LOCAL_TILE_SIZE);
            LocalPoint marker = new LocalPoint(xPos, yPos);
            Polygon poly = Perspective.getCanvasTilePoly(client, marker);
            if (poly == null) {
                continue;
            }
            OverlayUtil.renderPolygon(graphics, poly, color);
        }
    }
}
Also used : LocalPoint(net.runelite.api.coords.LocalPoint) Polygon(java.awt.Polygon) LocalPoint(net.runelite.api.coords.LocalPoint) Point(net.runelite.api.Point)

Example 50 with Polygon

use of java.awt.Polygon in project runelite by runelite.

the class RoguesDenOverlay method render.

@Override
public Dimension render(Graphics2D graphics) {
    if (!plugin.isHasGem()) {
        return null;
    }
    LocalPoint playerLocation = client.getLocalPlayer().getLocalLocation();
    plugin.getObstaclesHull().forEach((obstacle, tile) -> {
        if (tile.getPlane() == client.getPlane() && obstacle.getLocalLocation().distanceTo(playerLocation) < MAX_DISTANCE) {
            Polygon p = tile.getGameObjects()[0].getConvexHull();
            if (p != null) {
                graphics.setColor(Color.CYAN);
                graphics.drawPolygon(p);
            }
        }
    });
    plugin.getObstaclesTile().forEach((obstacle, tile) -> {
        if (tile.getPlane() == client.getPlane() && obstacle.getLocalLocation().distanceTo(playerLocation) < MAX_DISTANCE) {
            Polygon p = obstacle.getCanvasTilePoly();
            if (p != null) {
                graphics.setColor(Color.CYAN);
                graphics.drawPolygon(p);
            }
        }
    });
    return null;
}
Also used : LocalPoint(net.runelite.api.coords.LocalPoint) Polygon(java.awt.Polygon)

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