Sofia API Reference

  • Package Index
  • Class Index
  • Packages
  • sofia.app
  • sofia.content
  • sofia.data
  • sofia.graphics
    • Interfaces
    • Joint
    • PropertyTransformer
    • Classes
    • AbstractJoint
    • Anchor
    • Color
    • CoordinateSystem
    • DirectionalPad
    • DistanceJoint
    • ElasticInInterpolator
    • ElasticInOutInterpolator
    • ElasticOutInterpolator
    • FillableShape
    • FillableShape.Animator
    • Geometry
    • Image
    • LineShape
    • MotionStep
    • OvalShape
    • PointAndAnchor
    • Polygon
    • PolygonShape
    • Predicate
    • RectangleShape
    • RevoluteJoint
    • Shape
    • Shape.Animator
    • Shape.Filter
    • ShapeField
    • ShapeFilter
    • ShapeSet
    • ShapeView
    • SizeF
    • StrokedShape
    • StrokedShape.Animator
    • TextShape
    • TextShape.Animator
    • Timings
    • ViewEdges
    • ZIndexComparator
    • Enums
    • RepeatMode
    • ShapeMotion
  • sofia.util
  • sofia.view
  • sofia.widget

public class
Polygon

extends Object
implements Iterable<T>

Inheritance

  • java.lang.Object
    • sofia.graphics.Polygon

Class Overview

This class represents a polygon as a list of vertices. It only provides the abstract geometric representation, and methods to alter or decompose them. To actually draw a polygon on the screen, use the PolygonShape class.


Summary

Public Constructors
Polygon()
Initializes a new, empty polygon.
Polygon(float... xyArray)
Initializes a new polygon from a list of vertices.
Polygon(Polygon source)
Initializes a new polygon that is a copy of the specified polygon.
Public Methods
void add(int index, PointF point)
Inserts a vertex at the specified location in the polygon.
boolean add(PointF point)
Adds a vertex to the end of the polygon.
void addFrom(Polygon source, int start, int end)
Adds a sequence of vertices from another polygon to the end of this one.
float area()
Gets the area of the polygon.
PointF centroid()
Gets the centroid of the polygon.
List<Polygon> convexDecomposition()

Computes the convex decomposition of this polygon, with the added restriction that each polygon in the decomposition contains no more than eight vertices.

PointF get(int index)
Gets the vertex at the specified index.
Iterator<PointF> iterator()
Gets an iterator that can be used to iterate over the vertices in the polygon.
PointF remove(int index)
Removes the vertex at the specified index from the polygon.
PointF set(int index, PointF point)
Replaces the vertex at the specified index with a different point.
Polygon simplify(float tolerance)
Computes a simplified version of this polygon, by trying to eliminate vertices that are within a specified tolerance if the perpendicular distance is measured between them and a hypothetical line between two nearby vertices.
int size()
Gets the number of vertices in this polygon.
Polygon slice(int start, int end)
Methods inherited from class java.lang.Object
Object clone()
boolean equals(Object arg0)
void finalize()
final Class<?> getClass()
int hashCode()
final void notify()
final void notifyAll()
String toString()
final void wait()
final void wait(long arg0, int arg1)
final void wait(long arg0)
Methods inherited from interface java.lang.Iterable
abstract Iterator<T> iterator()

Public Constructors

public Polygon ()

Initializes a new, empty polygon.

public Polygon (float... xyArray)

Initializes a new polygon from a list of vertices. The polygon is automatically closed.

Parameters
xyArray
a list of floats that represent the x- and y-coordinates of the vertices of the polygon
Throws
IllegalArgumentException
if an odd number of floats is provided
public Polygon (Polygon source)

Initializes a new polygon that is a copy of the specified polygon.

Parameters
source
the polygon to copy

Public Methods

public void add (int index, PointF point)

Inserts a vertex at the specified location in the polygon.

Parameters
index
the index where the vertex should be inserted
point
the vertex to insert
public boolean add (PointF point)

Adds a vertex to the end of the polygon.

Parameters
point
the vertex to add to the polygon
Returns

always true

public void addFrom (Polygon source, int start, int end)

Adds a sequence of vertices from another polygon to the end of this one.

Parameters
source
the polygon to copy the vertices from
start
the starting index of the vertices to copy
end
the ending index of the vertices to copy (this vertex is NOT included)
public float area ()

Gets the area of the polygon.

Returns

the area of the polygon

public PointF centroid ()

Gets the centroid of the polygon.

Returns

the centroid of the polygon

public List<Polygon> convexDecomposition ()

Computes the convex decomposition of this polygon, with the added restriction that each polygon in the decomposition contains no more than eight vertices.

The result is cached once it is computed, so multiple calls to this method will not incur performance penalties unless the vertices in the polygon change.

Returns

the List of polygons that make up the receiver's convex decomposition

public PointF get (int index)

Gets the vertex at the specified index. This accessor is circular; negative indices and indices greater than the size of the polygon are wrapped appropriately.

Parameters
index
the index of the vertex to get
Returns

the vertex at the specified index

public Iterator<PointF> iterator ()

Gets an iterator that can be used to iterate over the vertices in the polygon.

Returns

an Iterator that can be used to iterate over the veritces in the polygon

public PointF remove (int index)

Removes the vertex at the specified index from the polygon.

Parameters
index
the index of the vertex to remove
Returns

the vertex that was removed

public PointF set (int index, PointF point)

Replaces the vertex at the specified index with a different point. This mutator is circular; negative indices and indices greater than the size of the polygon are wrapped appropriately.

Parameters
index
the index of the vertex to replace
point
the new vertex
Returns

the previous vertex at the specified index

public Polygon simplify (float tolerance)

Computes a simplified version of this polygon, by trying to eliminate vertices that are within a specified tolerance if the perpendicular distance is measured between them and a hypothetical line between two nearby vertices. The higher the tolerance, the fewer points the resulting polygon will have. The original polygon is not modified.

Parameters
tolerance
the maximum distance
Returns

the simplified polygon

public int size ()

Gets the number of vertices in this polygon.

Returns

the number of vertices in this polygon

public Polygon slice (int start, int end)