public class Polygon2D extends java.lang.Object implements java.lang.Iterable<Point2D>
NOTE: This class requires inclusion of the StdDraw and the JTS geometry libraries.
Constructor and Description |
---|
Polygon2D()
Construct an empty polygon.
|
Polygon2D(java.util.List<Point2D> points)
Construct a polygon from a List of Point2D.
|
Polygon2D(Point2D[] points)
Construct a polygon from an array of points.
|
Polygon2D(Polygon2D poly)
Copy constructor - makes a copy of the specified polygon.
|
Modifier and Type | Method and Description |
---|---|
void |
addPoint(Point2D p)
Add a point to this polygon.
|
Point2D[] |
asPointsArray()
This polygon as an array of points.
|
boolean |
contains(Point2D p)
Test if a specified point is contained inside this polygon.
Uses JTS method: boolean Geometry.contains(Geometry g) |
boolean |
contains(Point2D p1,
Point2D p2)
Test if a specified line segment is entirely contained inside this polygon.
Uses JTS method: boolean Geometry.contains(Geometry g) |
boolean |
disjoint(Point2D p1,
Point2D p2)
Test if the specified line segment points are disjoint from this polygon.
Returns true if none of the line segment points lie on the perimeter or the interior of this polygon. Uses JTS method: boolean Geometry.disjoint(Geometry g) |
void |
draw()
Draw perimeter of this polygon - uses the StdDraw library.
|
void |
draw(java.awt.Color clr)
Draw perimeter of this polygon with a given color - uses the StdDraw library.
|
void |
draw(java.awt.Color clr,
double penWidth)
Draw perimeter of this polygon with a given color and pen width - uses the StdDraw library.
|
void |
drawFilled()
Draw and fill this polygon - uses the StdDraw library.
|
void |
drawFilled(java.awt.Color clr)
Draw and fill this polygon with a given color - uses the StdDraw library.
|
Point2D |
get(int indx)
Get a specific vertex of the polygon.
|
Polygon2D |
getHull()
Get the convex hull of this polygon.
Note: returned hull is not a closed polygon (first point is not repeated as the last point). Uses JTS method: Geometry.convexHull() |
boolean |
intersects(Point2D p1,
Point2D p2)
Test if a specified line segment intersects this polygon.
Returns true if this polygon and the specified line segment have at least one point in common. Uses JTS method: boolean Geometry.intersects(Geometry g) |
java.util.Iterator<Point2D> |
iterator()
Iterator supporting for-each style iteration over all points in this polygon.
|
int |
size()
The number of vertices of this polygon.
|
java.lang.String |
toString()
Get a string representation of this polygon's points.
|
boolean |
touches(Point2D p)
Test if a specified point touches this polygon.
Uses JTS method: boolean Geometry.touches(Geometry g) |
boolean |
touches(Point2D p1,
Point2D p2)
Test if a specified line segment touches this polygon.
Returns true if this polygon and the line segment have at least one point in common, but none of the line segment points lie in the interior of the polygon perimeter. Uses JTS method: boolean Geometry.touches(Geometry g) |
public Polygon2D()
public Polygon2D(java.util.List<Point2D> points)
points
- List of Point2D referencespublic Polygon2D(Point2D[] points)
points
- array of Point2D referencespublic Polygon2D(Polygon2D poly)
poly
- the polygon to copypublic void addPoint(Point2D p)
p
- the point to add to this polygonpublic Point2D[] asPointsArray()
public boolean contains(Point2D p)
p
- point to testpublic boolean contains(Point2D p1, Point2D p2)
p1
- first end point of line segmentp2
- second end point of line segmentpublic boolean disjoint(Point2D p1, Point2D p2)
p1
- first end point of line segmentp2
- second end point of line segmentpublic void draw()
public void draw(java.awt.Color clr)
clr
- the pen color to usepublic void draw(java.awt.Color clr, double penWidth)
clr
- the pen color to usepenWidth
- width of the pen to usepublic void drawFilled()
public void drawFilled(java.awt.Color clr)
clr
- the pen color to usepublic Point2D get(int indx)
indx
- index of point to getpublic Polygon2D getHull()
public boolean intersects(Point2D p1, Point2D p2)
p1
- first point on linep2
- second point on linepublic java.util.Iterator<Point2D> iterator()
for (Point2D pnt : poly) { pnt.draw(); } // poly a Polygon2D object
iterator
in interface java.lang.Iterable<Point2D>
public int size()
public java.lang.String toString()
toString
in class java.lang.Object
public boolean touches(Point2D p)
p
- point to testpublic boolean touches(Point2D p1, Point2D p2)
p1
- first end point of line segmentp2
- second end point of line segment