@Test public void testContainsPolygon() { Polygon polygon = new Polygon(new Point [] { new Point(100f, 100f), new Point(150f, 100f), new Point(150f, 150f), new Point(100f, 150f) }); Polygon containedPolygon = new Polygon(new Point [] { new Point(125f, 125f), new Point(130f, 125f), new Point(130f, 130f), new Point(125f, 130f) }); Polygon nonContainedPolygon = new Polygon(new Point [] { new Point(200f, 200f), new Point(250f, 200f), new Point(250f, 250f), new Point(200f, 250f) }); Assert.assertEquals(true, polygon.contains(containedPolygon)); Assert.assertEquals(false, polygon.contains(nonContainedPolygon)); }
@Override public boolean contains(float x, float y) { if (isRectangle) { return triangleContains(x, y, vertices[0], vertices[1], vertices[2], vertices[3], vertices[6], vertices[7]) || triangleContains(x, y, vertices[6], vertices[7], vertices[2], vertices[3], vertices[4], vertices[5]); } int intersects = 0; for (int i = 0; i < vertices.length; i += 2) { float x1 = vertices[i]; float y1 = vertices[i + 1]; float x2 = vertices[(i + 2) % vertices.length]; float y2 = vertices[(i + 3) % vertices.length]; if (((y1 <= y && y < y2) || (y2 <= y && y < y1)) && x < ((x2 - x1) / (y2 - y1) * (y - y1) + x1)) intersects++; } return (intersects & 1) == 1; }
Polygon extends Shape { @Override public boolean contains(float x, float y) { if (isRectangle) { return triangleContains(x, y, vertices[0], vertices[1], vertices[2], vertices[3], vertices[6], vertices[7]) || triangleContains(x, y, vertices[6], vertices[7], vertices[2], vertices[3], vertices[4], vertices[5]); } int intersects = 0; for (int i = 0; i < vertices.length; i += 2) { float x1 = vertices[i]; float y1 = vertices[i + 1]; float x2 = vertices[(i + 2) % vertices.length]; float y2 = vertices[(i + 3) % vertices.length]; if (((y1 <= y && y < y2) || (y2 <= y && y < y1)) && x < ((x2 - x1) / (y2 - y1) * (y - y1) + x1)) intersects++; } return (intersects & 1) == 1; } }
Polygon extends Shape { @Override public boolean contains(float x, float y) { if (isRectangle) { return triangleContains(x, y, vertices[0], vertices[1], vertices[2], vertices[3], vertices[6], vertices[7]) || triangleContains(x, y, vertices[6], vertices[7], vertices[2], vertices[3], vertices[4], vertices[5]); } int intersects = 0; for (int i = 0; i < vertices.length; i += 2) { float x1 = vertices[i]; float y1 = vertices[i + 1]; float x2 = vertices[(i + 2) % vertices.length]; float y2 = vertices[(i + 3) % vertices.length]; if (((y1 <= y && y < y2) || (y2 <= y && y < y1)) && x < ((x2 - x1) / (y2 - y1) * (y - y1) + x1)) intersects++; } return (intersects & 1) == 1; } Polygon(Geometry geometry, float[] vertices);  Polygon(float[] vertices);  Polygon(Vector2[] points); }
Polygon extends Shape { @Override public boolean contains(float x, float y) { if (isRectangle) { return triangleContains(x, y, vertices[0], vertices[1], vertices[2], vertices[3], vertices[6], vertices[7]) || triangleContains(x, y, vertices[6], vertices[7], vertices[2], vertices[3], vertices[4], vertices[5]); } int intersects = 0; for (int i = 0; i < vertices.length; i += 2) { float x1 = vertices[i]; float y1 = vertices[i + 1]; float x2 = vertices[(i + 2) % vertices.length]; float y2 = vertices[(i + 3) % vertices.length]; if (((y1 <= y && y < y2) || (y2 <= y && y < y1)) && x < ((x2 - x1) / (y2 - y1) * (y - y1) + x1)) intersects++; } return (intersects & 1) == 1; } Polygon(Geometry geometry, float[] vertices);  Polygon(float[] vertices);  Polygon(Vector2[] points); @Override void dispose(); boolean isSameAs(Polygon polygon); Polygon lerp(Polygon target, float alpha); void lerp(Polygon result, Polygon target, float alpha); static void lerp(Polygon result, Polygon from, Polygon target, float alpha); @Override Shape copy(); @Override boolean contains(float x, float y); @Override boolean contains(Vector2 vector2); @Override boolean contains(Sizeable shape); boolean contains(Polygon polygon); @Override boolean intersects(Sizeable shape); boolean intersects(Polygon polygon); boolean intersects(Triangle triangle); boolean intersects(Rectangle rectangle); boolean intersects(Circle circle); @Override boolean intersects(LineSegment lineSegment); @Override boolean intersectsLineSegment(Vector2 pointA, Vector2 pointB); @Override boolean intersectsLineSegment(float x1, float y1, float x2, float y2); @Override float getWidth(); @Override float getHeight(); @Override float getDistanceTo(float x, float y); void addPoint(float x, float y); void addPoint(Vector2 point); void removePoint(float x, float y); void removePoint(Vector2 point); @Override int getNumberOfSides(); @Override void draw(Graphics g); @Override void fill(Graphics g); float[] getVertices(); void setVertices(float[] vertices); void setVertices(Vector2[] vertices); @Override float getRotation(); @Override void setRotation(float degrees); @Override void rotate(float degrees); @Override void setRotationAround(float centerX, float centerY, float degrees); @Override void rotateAround(float centerX, float centerY, float degrees); @Override float getX(); @Override float getY(); float getX(int index); float getY(int index); @Override float getCenterX(); @Override float getCenterY(); @Override void setCenter(float x, float y); @Override void setCenterX(float x); @Override void setCenterY(float y); float getMinX(); float getMinY(); float getMaxX(); float getMaxY(); ShortArray getTriangles(); @Override void setX(float x); @Override void setY(float y); @Override void setXY(float x, float y); @Override void setRadius(float radius); @Override void scale(float scale); void set(Polygon polygon); @Override void translate(float translateX, float translateY); @Override EdgeIterator edgeIterator(); boolean isEquilateral(); boolean isEquilateral(float tolerance); boolean isRectangle(); @Override boolean isCircle(); @Override Polygon getPolygon(); @Override int hashCode(); @Override boolean equals(Object obj); @Override String toString(); }
Polygon extends Shape { @Override public boolean contains(float x, float y) { if (isRectangle) { return triangleContains(x, y, vertices[0], vertices[1], vertices[2], vertices[3], vertices[6], vertices[7]) || triangleContains(x, y, vertices[6], vertices[7], vertices[2], vertices[3], vertices[4], vertices[5]); } int intersects = 0; for (int i = 0; i < vertices.length; i += 2) { float x1 = vertices[i]; float y1 = vertices[i + 1]; float x2 = vertices[(i + 2) % vertices.length]; float y2 = vertices[(i + 3) % vertices.length]; if (((y1 <= y && y < y2) || (y2 <= y && y < y1)) && x < ((x2 - x1) / (y2 - y1) * (y - y1) + x1)) intersects++; } return (intersects & 1) == 1; } Polygon(Geometry geometry, float[] vertices);  Polygon(float[] vertices);  Polygon(Vector2[] points); @Override void dispose(); boolean isSameAs(Polygon polygon); Polygon lerp(Polygon target, float alpha); void lerp(Polygon result, Polygon target, float alpha); static void lerp(Polygon result, Polygon from, Polygon target, float alpha); @Override Shape copy(); @Override boolean contains(float x, float y); @Override boolean contains(Vector2 vector2); @Override boolean contains(Sizeable shape); boolean contains(Polygon polygon); @Override boolean intersects(Sizeable shape); boolean intersects(Polygon polygon); boolean intersects(Triangle triangle); boolean intersects(Rectangle rectangle); boolean intersects(Circle circle); @Override boolean intersects(LineSegment lineSegment); @Override boolean intersectsLineSegment(Vector2 pointA, Vector2 pointB); @Override boolean intersectsLineSegment(float x1, float y1, float x2, float y2); @Override float getWidth(); @Override float getHeight(); @Override float getDistanceTo(float x, float y); void addPoint(float x, float y); void addPoint(Vector2 point); void removePoint(float x, float y); void removePoint(Vector2 point); @Override int getNumberOfSides(); @Override void draw(Graphics g); @Override void fill(Graphics g); float[] getVertices(); void setVertices(float[] vertices); void setVertices(Vector2[] vertices); @Override float getRotation(); @Override void setRotation(float degrees); @Override void rotate(float degrees); @Override void setRotationAround(float centerX, float centerY, float degrees); @Override void rotateAround(float centerX, float centerY, float degrees); @Override float getX(); @Override float getY(); float getX(int index); float getY(int index); @Override float getCenterX(); @Override float getCenterY(); @Override void setCenter(float x, float y); @Override void setCenterX(float x); @Override void setCenterY(float y); float getMinX(); float getMinY(); float getMaxX(); float getMaxY(); ShortArray getTriangles(); @Override void setX(float x); @Override void setY(float y); @Override void setXY(float x, float y); @Override void setRadius(float radius); @Override void scale(float scale); void set(Polygon polygon); @Override void translate(float translateX, float translateY); @Override EdgeIterator edgeIterator(); boolean isEquilateral(); boolean isEquilateral(float tolerance); boolean isRectangle(); @Override boolean isCircle(); @Override Polygon getPolygon(); @Override int hashCode(); @Override boolean equals(Object obj); @Override String toString();  }