Package toxi.geom

Class Polygon2D

java.lang.Object
toxi.geom.Polygon2D
All Implemented Interfaces:
Iterable<Vec2D>, Shape2D

public class Polygon2D extends Object implements Shape2D, Iterable<Vec2D>
Container type for convex polygons. Implements Shape2D.
  • Field Details

  • Constructor Details

    • Polygon2D

      public Polygon2D()
    • Polygon2D

      public Polygon2D(List<Vec2D> points)
    • Polygon2D

      public Polygon2D(Vec2D... points)
  • Method Details

    • fromBaseEdge

      public static Polygon2D fromBaseEdge(Vec2D baseA, Vec2D baseB, int res)
      Constructs a new regular polygon from the given base line/edge.
      Parameters:
      baseA - left point of the base edge
      baseB - right point of the base edge
      res - number of polygon vertices
      Returns:
      polygon
    • fromEdgeLength

      public static Polygon2D fromEdgeLength(float len, int res)
      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 length
      res - 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 length
      res - number of polygon vertices
      Returns:
      radius
    • add

      public Polygon2D add(float x, float y)
      Adds a new vertex to the polygon (builder pattern).
      Parameters:
      x -
      y -
      Returns:
      itself
    • add

      public Polygon2D add(Vec2D p)
      Adds a new vertex to the polygon (builder pattern).
      Parameters:
      p - vertex point to add
      Returns:
      itself
    • center

      public Polygon2D center()
      Centers the polygon around the world origin (0,0).
      Returns:
      itself
    • center

      public Polygon2D center(ReadonlyVec2D origin)
      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

      public boolean containsPoint(ReadonlyVec2D p)
      Description copied from interface: Shape2D
      Checks if the point is within the given shape.
      Specified by:
      containsPoint in interface Shape2D
      Returns:
      true, if inside
    • containsPolygon

      public boolean containsPolygon(Polygon2D poly)
    • copy

      public Polygon2D copy()
    • flipVertexOrder

      public Polygon2D flipVertexOrder()
      Flips the ordering of the polygon's vertices.
      Returns:
      itself
    • get

      public Vec2D get(int i)
      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/
      Specified by:
      getArea in interface Shape2D
      Returns:
      polygon area
    • getBoundingCircle

      public Circle getBoundingCircle()
      Description copied from interface: Shape2D
      Computes the bounding circle of the shape.
      Specified by:
      getBoundingCircle in interface Shape2D
      Returns:
      circle
    • getBounds

      public Rect getBounds()
      Returns the polygon's bounding rect.
      Specified by:
      getBounds in interface Shape2D
      Returns:
      bounding rect
      See Also:
    • getCentroid

      public Vec2D 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 interface Shape2D
      Returns:
      perimiter length
      See Also:
    • getClosestPointTo

      public Vec2D getClosestPointTo(ReadonlyVec2D p)
    • getClosestVertexTo

      public Vec2D getClosestVertexTo(ReadonlyVec2D p)
    • getEdges

      public List<Line2D> getEdges()
      Returns a list of Line2D segments representing the polygon edges.
      Specified by:
      getEdges in interface Shape2D
      Returns:
      list of lines
    • getNumPoints

      @Deprecated public int getNumPoints()
      Deprecated.
      See Also:
    • getNumVertices

      public int getNumVertices()
      Returns the number of polygon vertices.
      Returns:
      vertex count
    • getRandomPoint

      public Vec2D getRandomPoint()
      Creates a random point within the polygon. This is only guaranteed to work with regular polygons.
      Specified by:
      getRandomPoint in interface Shape2D
      Returns:
      Vec2D
    • increaseVertexCount

      public Polygon2D increaseVertexCount(int count)
      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

      public boolean intersectsPolygon(Polygon2D poly)
      Checks if the given polygon intersect this one by checking all edges for line intersections.
      Parameters:
      poly -
      Returns:
      true, if polygons intersect.
    • intersectsRect

      public boolean intersectsRect(Rect r)
    • 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 the getArea() 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

      public Iterator<Vec2D> iterator()
      Specified by:
      iterator in interface Iterable<Vec2D>
    • offsetShape

      public Polygon2D offsetShape(float distance)
      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 the toOutline() method.
      Parameters:
      distance - offset/inset distance (negative for inset)
      Returns:
      itself
    • reduceVertices

      public Polygon2D reduceVertices(float minEdgeLen)
      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

      public Polygon2D removeDuplicates(float tolerance)
      Removes duplicate vertices from the polygon. Only successive points are recognized as duplicates.
      Parameters:
      tolerance - snap distance for finding duplicates
      Returns:
      itself
    • rotate

      public Polygon2D rotate(float theta)
    • scale

      public Polygon2D scale(float scale)
    • scale

      public Polygon2D scale(float x, float y)
    • scale

      public Polygon2D scale(ReadonlyVec2D scale)
    • scaleSize

      public Polygon2D scaleSize(float scale)
    • scaleSize

      public Polygon2D scaleSize(float x, float y)
    • scaleSize

      public Polygon2D scaleSize(ReadonlyVec2D scale)
    • smooth

      public Polygon2D 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. 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

      public Mesh3D toMesh(Mesh3D mesh)
    • toMesh

      public Mesh3D toMesh(Mesh3D mesh, Vec2D centroid2D, float extrude)
    • 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

      public Polygon2D toPolygon2D()
      Only needed for Shape2D interface. Returns itself.
      Specified by:
      toPolygon2D in interface Shape2D
      Returns:
      itself
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • translate

      public Polygon2D translate(float x, float y)
    • translate

      public Polygon2D translate(ReadonlyVec2D offset)