Here you can find the source of inside_polygon(float[] xpts, float[] ypts, double ptx, double pty)
Parameter | Description |
---|---|
xpts | horizontal pixel window points of polygon. |
ypts | vertical pixel window points of polygon. |
ptx | horizontal pixel window points of location |
pty | vertical pixel window points of location. |
public final static boolean inside_polygon(float[] xpts, float[] ypts, double ptx, double pty)
//package com.java2s; import java.awt.geom.Point2D; public class Main { /**/*from w ww . j av a 2 s . c om*/ * Tests if a point is inside a polygon. * <p> * * @param xpts horizontal pixel window points of polygon. * @param ypts vertical pixel window points of polygon. * @param ptx horizontal pixel window points of location * @param pty vertical pixel window points of location. * @return boolean */ public final static boolean inside_polygon(float[] xpts, float[] ypts, double ptx, double pty) { int j, inside_flag = 0; int numverts = xpts.length; if (numverts <= 2) return false; Point2D vtx0 = new Point2D.Float(), vtx1 = new Point2D.Float(); double dv0; // prevents OVERFLOW!! int crossings = 0; boolean xflag0 = false, yflag0 = false, yflag1 = false; vtx0.setLocation(xpts[numverts - 1], ypts[numverts - 1]); // get test bit for above/below Y axis yflag0 = ((dv0 = vtx0.getY() - pty) >= 0); for (j = 0; j < numverts; j++) { if ((j & 0x1) != 0) { // HACK - slightly changed vtx0.setLocation(xpts[j], ypts[j]); yflag0 = ((dv0 = vtx0.getY() - pty) >= 0); } else { vtx1.setLocation(xpts[j], ypts[j]); yflag1 = (vtx1.getY() >= pty); } /* * check if points not both above/below X axis - can't hit ray */ if (yflag0 != yflag1) { /* check if points on same side of Y axis */ if ((xflag0 = (vtx0.getX() >= ptx)) == (vtx1.getX() >= ptx)) { if (xflag0) crossings++; } else { crossings += ((vtx0.getX() - dv0 * (vtx1.getX() - vtx0.getX()) / (vtx1.getY() - vtx0.getY())) >= ptx) ? 1 : 0; } } inside_flag = crossings & 0x01; } return (inside_flag != 0); } }