Class: Polygon

graphics.geom. Polygon


new Polygon()

A simple location in a two-dimensional coordinate system, where x represents the horizontal axis and y represents the vertical axis.

The Polygon class represents a simple location in a two-dimensional coordinate system, where x represents the horizontal axis and y represents the vertical axis.

In the constructor of the Polygon class, the points can be set from a variety of formats:

  • An array of Vector2D or Point objects : [new Point(x1,y1),new Vector2D(x2,y2)].
  • An array of objects with public x/y properties.
  • An array of paired numbers that represent point coordinates: [x1,y1,x2,y2,...].
  • An array of arrays with two elements representing x/y coordinates: [[x1, y1], [x2, y2], ...].

Parameters:
Name Type Argument Description
... * <optional>

The optional points to register the polygon representation.

Example
var p1 = new Polygon({x:10,y:10},{x:20,y:10},{x:20,y:20},{x:10,y:20}) ;
var p2 = new Polygon(10,10,20,20,20,20,10,20) ;
var p3 = new Polygon([{x:10,y:10},{x:20,y:10},{x:20,y:20},{x:10,y:20}]) ;
var p4 = new Polygon([new Vector2D(10,10),new Vector2D(20,10),new Vector2D(20,20),new Vector2D(10,20)]) ;

Members


<readonly> area :number

Indicates the area measure of the polygon.

Type:
  • number

closed :boolean

Indicates if the polygon is closed or not.

Type:
  • boolean

<readonly> flattened :Array

The array collection reference of all points who defines the polygon.

Type:

<readonly> flattened :number

Indicates the number of elements registered in the points collection.

Type:
  • number

<readonly> flattened :boolean

Indicates if the polygon collection is flattened.

Type:
  • boolean

Methods


clone()

Returns a shallow copy of the object.

Returns:

a shallow copy of the object.


contains(x, y)

Checks whether the specified x and y coordinates are contained within this polygon.

Parameters:
Name Type Description
x number

The X value of the coordinate to evaluates.

y number

The Y value of the coordinate to evaluates.

See:
Returns:

True if the coordinates are within this polygon, otherwise false.

Type
boolean

copyFrom(source)

Copies all of vector data from the source Polygon object into the calling Polygon object.

Parameters:
Name Type Description
source graphics.geom.Polygon

The Polygon object from which to copy the data.

Returns:

The current graphics.geom.Polygon reference.


equals()

Compares the passed-in object with this object for equality.

Returns:

true if the the specified object is equal with this object.

Type
boolean

flatten()

Flattens this polygon so the points are a sequence of numbers. Any Point objects found are removed and replaced with two numbers and set the flattened property to true.

Example
var polygon = new Polygon({x:10,y:10},{x:20,y:10},{x:20,y:20},{x:10,y:20}) ;
polygon.flatten() ;
trace(polygon.points) ; // 10,10,20,20,20,20,10,20
trace(polygon.flattened) ; // true

setTo( [points])

Sets this Polygon to the given points.

All the points can be set from a variety of formats:

  • An array of Vector2D or Point objects : [new Point(x1,y1),new Vector2D(x2,y2)].
  • An array of objects with public x/y properties.
  • An array of paired numbers that represent point coordinates: [x1,y1,x2,y2,...].
  • An array of arrays with two elements representing x/y coordinates: [[x1, y1], [x2, y2], ...].

Parameters:
Name Type Argument Description
points * <optional>

All the points to defines the polygon representation.

Example
var polygon = new Polygon() ;

polygon.setTo({x:10,y:10},{x:20,y:10},{x:20,y:20},{x:10,y:20}) ;
trace( polygon.points ) ;

polygon.setTo(10,10,20,20,20,20,10,20) ;
trace( polygon.points ) ;

polygon.setTo([{x:10,y:10},{x:20,y:10},{x:20,y:20},{x:10,y:20}]) ;
trace( polygon.points ) ;

polygon.setTo([new Vector2D(10,10),new Vector2D(20,10),new Vector2D(20,20),new Vector2D(10,20)]) ;
trace( polygon.points ) ;

toArray( [output])

Export all the points as an array of flat numbers, following the sequence [x1,y1,x2,y2,x3,y3,...].

Parameters:
Name Type Argument Default Description
output Array <optional>
null

The optional Array to fill, if this argument is null, a new Array is created.

Returns:

The Array representation of this Polygon.


toObject()

Returns the Object representation of this object.

Returns:

the Object representation of this object.


toString()

Returns the string representation of this instance.

Returns:

The string representation of this instance.

Type
string