Packagestarling.geom
Classpublic class Polygon
InheritancePolygon Inheritance Object

A polygon describes a closed two-dimensional shape bounded by a number of straight line segments.

The vertices of a polygon form a closed path (i.e. the last vertex will be connected to the first). It is recommended to provide the vertices in clockwise order. Self-intersecting paths are not supported and will give wrong results on triangulation, area calculation, etc.



Public Properties
 PropertyDefined By
  area : Number
[read-only] Calculates the total area of the polygon.
Polygon
  isConvex : Boolean
[read-only] Indicates if the polygon is convex.
Polygon
  isSimple : Boolean
[read-only] Indicates if the polygon's line segments are not self-intersecting.
Polygon
  numTriangles : int
[read-only] Returns the number of triangles that will be required when triangulating the polygon.
Polygon
  numVertices : int
Returns the total number of vertices spawning up the polygon.
Polygon
Public Methods
 MethodDefined By
  
Polygon(vertices:Array = null)
Creates a Polygon with the given coordinates.
Polygon
  
addVertices(... args):void
Adds vertices to the polygon.
Polygon
  
Creates a clone of this polygon.
Polygon
  
contains(x:Number, y:Number):Boolean
Figures out if the given coordinates lie within the polygon.
Polygon
  
containsPoint(point:Point):Boolean
Figures out if the given point lies within the polygon.
Polygon
  
copyToVertexData(target:VertexData = null, targetVertexID:int = 0, attrName:String = position):void
Copies all vertices to a 'VertexData' instance, beginning at a certain target index.
Polygon
  
createCircle(x:Number, y:Number, radius:Number):Polygon
[static] Creates a circle with optimized implementations of triangulation, hitTest, etc.
Polygon
  
createEllipse(x:Number, y:Number, radiusX:Number, radiusY:Number):Polygon
[static] Creates an ellipse with optimized implementations of triangulation, hitTest, etc.
Polygon
  
createRectangle(x:Number, y:Number, width:Number, height:Number):Polygon
[static] Creates a rectangle with optimized implementations of triangulation, hitTest, etc.
Polygon
  
getVertex(index:int, out:Point = null):Point
Returns the coordinates of a certain vertex.
Polygon
  
reverse():void
Reverses the order of the vertices.
Polygon
  
setVertex(index:int, x:Number, y:Number):void
Moves a given vertex to a certain position or adds a new vertex at the end.
Polygon
  
toString():String
Creates a string that contains the values of all included points.
Polygon
  
triangulate(indexData:IndexData = null, offset:int = 0):IndexData
Calculates a possible representation of the polygon via triangles.
Polygon
Property Detail
areaproperty
area:Number  [read-only]

Calculates the total area of the polygon.


Implementation
    public function get area():Number
isConvexproperty 
isConvex:Boolean  [read-only]

Indicates if the polygon is convex. In a convex polygon, the vector between any two points inside the polygon lies inside it, as well.


Implementation
    public function get isConvex():Boolean
isSimpleproperty 
isSimple:Boolean  [read-only]

Indicates if the polygon's line segments are not self-intersecting. Beware: this is a brute-force implementation with O(n^2).


Implementation
    public function get isSimple():Boolean
numTrianglesproperty 
numTriangles:int  [read-only]

Returns the number of triangles that will be required when triangulating the polygon.


Implementation
    public function get numTriangles():int
numVerticesproperty 
numVertices:int

Returns the total number of vertices spawning up the polygon. Assigning a value that's smaller than the current number of vertices will crop the path; a bigger value will fill up the path with zeros.


Implementation
    public function get numVertices():int
    public function set numVertices(value:int):void
Constructor Detail
Polygon()Constructor
public function Polygon(vertices:Array = null)

Creates a Polygon with the given coordinates.

Parameters
vertices:Array (default = null) — an array that contains either 'Point' instances or alternating 'x' and 'y' coordinates.
Method Detail
addVertices()method
public function addVertices(... args):void

Adds vertices to the polygon. Pass either a list of 'Point' instances or alternating 'x' and 'y' coordinates.

Parameters

... args

clone()method 
public function clone():Polygon

Creates a clone of this polygon.

Returns
Polygon
contains()method 
public function contains(x:Number, y:Number):Boolean

Figures out if the given coordinates lie within the polygon.

Parameters

x:Number
 
y:Number

Returns
Boolean
containsPoint()method 
public function containsPoint(point:Point):Boolean

Figures out if the given point lies within the polygon.

Parameters

point:Point

Returns
Boolean
copyToVertexData()method 
public function copyToVertexData(target:VertexData = null, targetVertexID:int = 0, attrName:String = position):void

Copies all vertices to a 'VertexData' instance, beginning at a certain target index.

Parameters

target:VertexData (default = null)
 
targetVertexID:int (default = 0)
 
attrName:String (default = position)

createCircle()method 
public static function createCircle(x:Number, y:Number, radius:Number):Polygon

Creates a circle with optimized implementations of triangulation, hitTest, etc.

Parameters

x:Number
 
y:Number
 
radius:Number

Returns
Polygon
createEllipse()method 
public static function createEllipse(x:Number, y:Number, radiusX:Number, radiusY:Number):Polygon

Creates an ellipse with optimized implementations of triangulation, hitTest, etc.

Parameters

x:Number
 
y:Number
 
radiusX:Number
 
radiusY:Number

Returns
Polygon
createRectangle()method 
public static function createRectangle(x:Number, y:Number, width:Number, height:Number):Polygon

Creates a rectangle with optimized implementations of triangulation, hitTest, etc.

Parameters

x:Number
 
y:Number
 
width:Number
 
height:Number

Returns
Polygon
getVertex()method 
public function getVertex(index:int, out:Point = null):Point

Returns the coordinates of a certain vertex.

Parameters

index:int
 
out:Point (default = null)

Returns
Point
reverse()method 
public function reverse():void

Reverses the order of the vertices. Note that some methods of the Polygon class require the vertices in clockwise order.

setVertex()method 
public function setVertex(index:int, x:Number, y:Number):void

Moves a given vertex to a certain position or adds a new vertex at the end.

Parameters

index:int
 
x:Number
 
y:Number

toString()method 
public function toString():String

Creates a string that contains the values of all included points.

Returns
String
triangulate()method 
public function triangulate(indexData:IndexData = null, offset:int = 0):IndexData

Calculates a possible representation of the polygon via triangles. The resulting IndexData instance will reference the polygon vertices as they are saved in this Polygon instance, optionally incremented by the given offset.

If you pass an indexData object, the new indices will be appended to it. Otherwise, a new instance will be created.

Parameters

indexData:IndexData (default = null)
 
offset:int (default = 0)

Returns
IndexData