@Test public void testPointInShape() { PolygonBuilder b = new PolygonBuilder(); b.addPoint(Vector2.fromXY(0, 0)); b.addPoint(Vector2.fromXY(1, 0)); b.addPoint(Vector2.fromXY(1, 1)); b.addPoint(Vector2.fromXY(0, 1)); Polygon p = b.build(); Assert.assertTrue(p.isPointInShape(Vector2.fromXY(0, 0))); Assert.assertTrue(p.isPointInShape(Vector2.fromXY(1, 0))); Assert.assertTrue(p.isPointInShape(Vector2.fromXY(1, 1))); Assert.assertTrue(p.isPointInShape(Vector2.fromXY(0, 1))); Assert.assertTrue(p.isPointInShape(Vector2.fromXY(0.5, 0.5))); Assert.assertFalse(p.isPointInShape(Vector2.fromXY(2, 2))); Assert.assertTrue(p.isPointInShape(Vector2.fromXY(2, 2), 1)); }
@Override public boolean isPointInShape(final IVector2 point, final double margin) { int j = points.length - 1; boolean oddNodes = false; for (int i = 0; i < points.length; i++) { final double x = point.x(); final double y = point.y(); final IVector2 a = getMarginPoint(points[i], margin + ACCURACY); final IVector2 b = getMarginPoint(points[j], margin + ACCURACY); final boolean yCheckAB = (a.y() < y) && (b.y() >= y); final boolean yCheckBA = (b.y() < y) && (a.y() >= y); final boolean xCheck = (a.x() <= x) || (b.x() <= x); if (yCheckBA || (yCheckAB && xCheck)) { oddNodes ^= ((a.x() + (((y - a.y()) / (b.y() - a.y())) * (b.x() - a.x()))) < x); } j = i; } return oddNodes; }
Polygon implements I2DShape { @Override public boolean isPointInShape(final IVector2 point, final double margin) { int j = points.length - 1; boolean oddNodes = false; for (int i = 0; i < points.length; i++) { final double x = point.x(); final double y = point.y(); final IVector2 a = getMarginPoint(points[i], margin + ACCURACY); final IVector2 b = getMarginPoint(points[j], margin + ACCURACY); final boolean yCheckAB = (a.y() < y) && (b.y() >= y); final boolean yCheckBA = (b.y() < y) && (a.y() >= y); final boolean xCheck = (a.x() <= x) || (b.x() <= x); if (yCheckBA || (yCheckAB && xCheck)) { oddNodes ^= ((a.x() + (((y - a.y()) / (b.y() - a.y())) * (b.x() - a.x()))) < x); } j = i; } return oddNodes; } }
Polygon implements I2DShape { @Override public boolean isPointInShape(final IVector2 point, final double margin) { int j = points.length - 1; boolean oddNodes = false; for (int i = 0; i < points.length; i++) { final double x = point.x(); final double y = point.y(); final IVector2 a = getMarginPoint(points[i], margin + ACCURACY); final IVector2 b = getMarginPoint(points[j], margin + ACCURACY); final boolean yCheckAB = (a.y() < y) && (b.y() >= y); final boolean yCheckBA = (b.y() < y) && (a.y() >= y); final boolean xCheck = (a.x() <= x) || (b.x() <= x); if (yCheckBA || (yCheckAB && xCheck)) { oddNodes ^= ((a.x() + (((y - a.y()) / (b.y() - a.y())) * (b.x() - a.x()))) < x); } j = i; } return oddNodes; } Polygon(final Collection<IVector2> polyPoints, final IVector2 centroid); }
Polygon implements I2DShape { @Override public boolean isPointInShape(final IVector2 point, final double margin) { int j = points.length - 1; boolean oddNodes = false; for (int i = 0; i < points.length; i++) { final double x = point.x(); final double y = point.y(); final IVector2 a = getMarginPoint(points[i], margin + ACCURACY); final IVector2 b = getMarginPoint(points[j], margin + ACCURACY); final boolean yCheckAB = (a.y() < y) && (b.y() >= y); final boolean yCheckBA = (b.y() < y) && (a.y() >= y); final boolean xCheck = (a.x() <= x) || (b.x() <= x); if (yCheckBA || (yCheckAB && xCheck)) { oddNodes ^= ((a.x() + (((y - a.y()) / (b.y() - a.y())) * (b.x() - a.x()))) < x); } j = i; } return oddNodes; } Polygon(final Collection<IVector2> polyPoints, final IVector2 centroid); @Override boolean isPointInShape(final IVector2 point, final double margin); @Override boolean isIntersectingWithLine(final ILine line); @Override IVector2 nearestPointOutside(final IVector2 point); @Override IVector2 nearestPointInside(final IVector2 point); IVector2 nearestPointOutside(final IVector2 point, final double margin); IVector2 nearestPointOutside(final IVector2 point, final IVector2 p2bl, final double margin); @Override List<IVector2> lineIntersections(final ILine line); List<IVector2> lineIntersections(final ILine line, final double margin); }
Polygon implements I2DShape { @Override public boolean isPointInShape(final IVector2 point, final double margin) { int j = points.length - 1; boolean oddNodes = false; for (int i = 0; i < points.length; i++) { final double x = point.x(); final double y = point.y(); final IVector2 a = getMarginPoint(points[i], margin + ACCURACY); final IVector2 b = getMarginPoint(points[j], margin + ACCURACY); final boolean yCheckAB = (a.y() < y) && (b.y() >= y); final boolean yCheckBA = (b.y() < y) && (a.y() >= y); final boolean xCheck = (a.x() <= x) || (b.x() <= x); if (yCheckBA || (yCheckAB && xCheck)) { oddNodes ^= ((a.x() + (((y - a.y()) / (b.y() - a.y())) * (b.x() - a.x()))) < x); } j = i; } return oddNodes; } Polygon(final Collection<IVector2> polyPoints, final IVector2 centroid); @Override boolean isPointInShape(final IVector2 point, final double margin); @Override boolean isIntersectingWithLine(final ILine line); @Override IVector2 nearestPointOutside(final IVector2 point); @Override IVector2 nearestPointInside(final IVector2 point); IVector2 nearestPointOutside(final IVector2 point, final double margin); IVector2 nearestPointOutside(final IVector2 point, final IVector2 p2bl, final double margin); @Override List<IVector2> lineIntersections(final ILine line); List<IVector2> lineIntersections(final ILine line, final double margin);  }