Poly
Extends :: Shape :: Obj
A class that defines a polygon with a set of vertices. These vertices can be relative to a polygon's centroid, or relative to canvas (0,0), depending on how you instantiate it.
This class must be accessed through the iio package.
Constructors
These functions are used to instantiate new instances of the Poly class. Constructor functions must be preceded by the new keyword.
iio.Poly( Array vertices )
- creates a polygon with the given with the vertices. The vertices will be treated as absolute canvas positions (relative to canvas 0,0).
- the array of vertices should be a list of coordinates, defined either in x,y coordinates, or as a Vector.
//create a triangle
var poly = new iio.Poly([io.canvas.center,
,50,50
,200,0]);
iio.Poly( Vector position, Array vertices )
iio.Poly( Number: x, y, Array vertices )
- creates a polygon with the given vertices. The vertices will be treated as relative to the given position.
//create a polygon with vertices relative
//to canvas center
var polygon = new iio.Poly(io.canvas.center,
,[-30,30
,50,50
,0,-30]));
Graphics Properties
The following properties can are attached by the iio Graphics Engine. Note that these properties are 'undefined' by default, so you must define them with their set functions or set their values directly.
Kinematics Properties
The following properties can are attached by the iio Kinematics Engine. Note that these properties are 'undefined' by default, so you must define them with their set functions or set their values directly.
Properties
The new data added by the Poly class.
.vertices :: Array
- the vertices of this polygon.
//get a polygon's vertices var w = poly.vertices; //get a polygon's second vertex var v = poly.vertices[1];
.width :: Number
- the width of this polygon, measured in pixels.
//get a polygon's width var w = polygon.width;
.height :: Number
- the height of this polygon, measured in pixels.
//get a polygon's height var h = polygon.height;
.originToLeft :: Number
- the horizontal distance from this polygon's centroid to its left-most coordinate, measured in pixels.
- note that this value gets set once on object creation, and does not take rotation into account.
var OTL = polygon.originToLeft;
.originToTop :: Number
- the vertical distance from this polygon's centroid to its top-most coordinate, measured in pixels.
- note that this value gets set once on object creation, and does not take rotation into account.
var OTT = polygon.originToTop;
Kinematics Functions
The following functions can are attached by the iio Kinematics Engine. Note that you must call the enableKinematics function before you can utilize any of these functions.
Functions
These functions are added by the Poly class, and available to all instantiated Poly objects.
.clone()
:: Returns Poly
- returns a new Poly with the same properties as this one. Makes a hard copy of the object.
.contains( Vector point )
.contains( Number: x, y )
:: Returns Boolean
- returns true if the given point is inside this polygon. Returns false otherwise.
//alert when a polygon is clicked
io.canvas.addEventListener('mousedown', function(event){
if (polygon.contains(io.getEventPosition(event)))
alert('you clicked the polygon');
});
.getTrueVertices()
:: Returns Array
- returns an array with vertices that account for object rotation and are relative to canvas 0,0.
//get vertices relative to poly center var relativeVertices = poly.vertices; //get true poly vertices var trueVertices = poly.getTrueVertices();