Package toxi.geom
Class Polygon2D
java.lang.Object
toxi.geom.Polygon2D
Container type for convex polygons. Implements
Shape2D
.-
Field Summary
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionadd
(float x, float y) Adds a new vertex to the polygon (builder pattern).Adds a new vertex to the polygon (builder pattern).center()
Centers the polygon around the world origin (0,0).center
(ReadonlyVec2D origin) Centers the polygon so that its new centroid is at the given point.boolean
Checks if the point is within the given shape.boolean
containsPolygon
(Polygon2D poly) copy()
Flips the ordering of the polygon's vertices.static Polygon2D
fromBaseEdge
(Vec2D baseA, Vec2D baseB, int res) Constructs a new regular polygon from the given base line/edge.static Polygon2D
fromEdgeLength
(float len, int res) Constructs a regular polygon from the given edge length and number of vertices.get
(int i) Returns the vertex at the given index.float
Computes the length of this polygon's apothem.float
getArea()
Computes the area of the polygon, provided it isn't self intersecting.Computes the bounding circle of the shape.Returns the polygon's bounding rect.Computes the polygon's centre of mass.float
Computes the polygon's circumference, the length of its perimeter.getEdges()
Returns a list ofLine2D
segments representing the polygon edges.int
Deprecated.int
Returns the number of polygon vertices.static float
getRadiusForEdgeLength
(float len, int res) Computes the radius of the circle the regular polygon with the desired edge length is inscribed inCreates a random point within the polygon.increaseVertexCount
(int count) Repeatedly inserts vertices as mid points of the longest edges until the new vertex count is reached.boolean
intersectsPolygon
(Polygon2D poly) Checks if the given polygon intersect this one by checking all edges for line intersections.boolean
boolean
Checks if the vertices of this polygon are in clockwise ordering by examining all vertices as a sequence of triangles.boolean
isConvex()
Checks if the polygon is convex.iterator()
offsetShape
(float distance) Moves each line segment of the polygon in/outward perpendicular by the given distance.reduceVertices
(float minEdgeLen) Reduces the number of vertices in the polygon based on the given minimum edge length.removeDuplicates
(float tolerance) Removes duplicate vertices from the polygon.rotate
(float theta) scale
(float scale) scale
(float x, float y) scale
(ReadonlyVec2D scale) scaleSize
(float scale) scaleSize
(float x, float y) scaleSize
(ReadonlyVec2D scale) smooth
(float amount, float baseWeight) Applies a laplacian-style smooth operation to all polygon vertices, causing sharp corners/angles to widen and results in a general smoother shape.boolean
Attempts to remove all internal self-intersections and creates a new polygon only consisting of perimeter vertices.Only needed forShape2D
interface.toString()
translate
(float x, float y) translate
(ReadonlyVec2D offset) Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, wait, wait, wait
Methods inherited from interface java.lang.Iterable
forEach, spliterator
-
Field Details
-
vertices
-
-
Constructor Details
-
Polygon2D
public Polygon2D() -
Polygon2D
-
Polygon2D
-
-
Method Details
-
fromBaseEdge
Constructs a new regular polygon from the given base line/edge.- Parameters:
baseA
- left point of the base edgebaseB
- right point of the base edgeres
- number of polygon vertices- Returns:
- polygon
-
fromEdgeLength
Constructs a regular polygon from the given edge length and number of vertices. This automatically computes the radius of the circle the polygon is inscribed in.More information: http://en.wikipedia.org/wiki/Regular_polygon#Radius
- Parameters:
len
- desired edge lengthres
- number of vertices- Returns:
- polygon
-
getRadiusForEdgeLength
public static float getRadiusForEdgeLength(float len, int res) Computes the radius of the circle the regular polygon with the desired edge length is inscribed in- Parameters:
len
- edge lengthres
- number of polygon vertices- Returns:
- radius
-
add
Adds a new vertex to the polygon (builder pattern).- Parameters:
x
-y
-- Returns:
- itself
-
add
Adds a new vertex to the polygon (builder pattern).- Parameters:
p
- vertex point to add- Returns:
- itself
-
center
Centers the polygon around the world origin (0,0).- Returns:
- itself
-
center
Centers the polygon so that its new centroid is at the given point.- Parameters:
origin
- new centroid or null to center around (0,0)- Returns:
- itself
-
containsPoint
Description copied from interface:Shape2D
Checks if the point is within the given shape.- Specified by:
containsPoint
in interfaceShape2D
- Returns:
- true, if inside
-
containsPolygon
-
copy
-
flipVertexOrder
Flips the ordering of the polygon's vertices.- Returns:
- itself
-
get
Returns the vertex at the given index. This function follows Python convention, in that if the index is negative, it is considered relative to the list end. Therefore the vertex at index -1 is the last vertex in the list.- Parameters:
i
- index- Returns:
- vertex
-
getApothem
public float getApothem()Computes the length of this polygon's apothem. This will only be valid if the polygon is regular. More info: http://en.wikipedia.org/wiki/Apothem- Returns:
- apothem length
-
getArea
public float getArea()Computes the area of the polygon, provided it isn't self intersecting. Code ported from: http://local.wasp.uwa.edu.au/~pbourke/geometry/polyarea/ -
getBoundingCircle
Description copied from interface:Shape2D
Computes the bounding circle of the shape.- Specified by:
getBoundingCircle
in interfaceShape2D
- Returns:
- circle
-
getBounds
Returns the polygon's bounding rect. -
getCentroid
Computes the polygon's centre of mass. Code ported from: http://local.wasp.uwa.edu.au/~pbourke/geometry/polyarea/- Returns:
- centroid point
-
getCircumference
public float getCircumference()Computes the polygon's circumference, the length of its perimeter.- Specified by:
getCircumference
in interfaceShape2D
- Returns:
- perimiter length
- See Also:
-
getClosestPointTo
-
getClosestVertexTo
-
getEdges
Returns a list ofLine2D
segments representing the polygon edges. -
getNumPoints
Deprecated.- See Also:
-
getNumVertices
public int getNumVertices()Returns the number of polygon vertices.- Returns:
- vertex count
-
getRandomPoint
Creates a random point within the polygon. This is only guaranteed to work with regular polygons.- Specified by:
getRandomPoint
in interfaceShape2D
- Returns:
- Vec2D
-
increaseVertexCount
Repeatedly inserts vertices as mid points of the longest edges until the new vertex count is reached.- Parameters:
count
- new vertex count- Returns:
- itself
-
intersectsPolygon
Checks if the given polygon intersect this one by checking all edges for line intersections.- Parameters:
poly
-- Returns:
- true, if polygons intersect.
-
intersectsRect
-
isClockwise
public boolean isClockwise()Checks if the vertices of this polygon are in clockwise ordering by examining all vertices as a sequence of triangles. The test relies on the fact that thegetArea()
method will produce negative results for ant-clockwise ordered polygons.- Returns:
- true, if clockwise
-
isConvex
public boolean isConvex()Checks if the polygon is convex.- Returns:
- true, if convex.
-
iterator
-
offsetShape
Moves each line segment of the polygon in/outward perpendicular by the given distance. New line segments and polygon vertices are created by computing the intersection points of the displaced segments. Choosing an too large displacement amount will result in deformation/undefined behavior with various self intersections. Should that happen, please try to clean up the shape using thetoOutline()
method.- Parameters:
distance
- offset/inset distance (negative for inset)- Returns:
- itself
-
reduceVertices
Reduces the number of vertices in the polygon based on the given minimum edge length. Only vertices with at least this distance between them will be kept.- Parameters:
minEdgeLen
-- Returns:
- itself
-
removeDuplicates
Removes duplicate vertices from the polygon. Only successive points are recognized as duplicates.- Parameters:
tolerance
- snap distance for finding duplicates- Returns:
- itself
-
rotate
-
scale
-
scale
-
scale
-
scaleSize
-
scaleSize
-
scaleSize
-
smooth
Applies a laplacian-style smooth operation to all polygon vertices, causing sharp corners/angles to widen and results in a general smoother shape. Let the current vertex be A and its neighbours P and Q, then A will be moved by a specified amount into the direction given by (P-A)+(Q-A). Additionally, and to avoid shrinking of the shape through repeated iteration of this procedure, the vector A - C (Polygon centroid) is added as counter force and a weight for its impact can be specified. To keep the average size of the polygon stable, this weight value should be ~1/2 of the smooth amount.- Parameters:
amount
- smooth amount (between 0 < x < 0.5)baseWeight
- counter weight (0 <= x < 1/2 * smooth amount)- Returns:
- itself
-
toMesh
-
toMesh
-
toOutline
public boolean toOutline()Attempts to remove all internal self-intersections and creates a new polygon only consisting of perimeter vertices. Ported from: http://alienryderflex.com/polygon_perimeter/- Returns:
- true, if process completed succcessfully.
-
toPolygon2D
Only needed forShape2D
interface. Returns itself.- Specified by:
toPolygon2D
in interfaceShape2D
- Returns:
- itself
-
toString
-
translate
-
translate
-