Representing 2D Polygons
Line Segments and the LineSegment
class
When dealing with the edge of polygons, we will use the class LineSegment
defined in LineaSegment.ts, which consists of a start point (Vec2) and an end point (Vec2).
To create a line segment, simply feed two points into it's constructor:
let segment = new LineSegment(V2(10,10), V2(20,5));
Note that the V2()
function here is the same as new Vec2()
.
You are welcome to add additional functionality to the LineSegment class, which you may find useful. For example, you may find it useful to write a getter that returns the homogeneous vector representation of the line containing the segment.
Polygons and the Polygon2D
class
For this assignment, we will represent 2D polygons using the class Polygon2D, which is a subclass of the VertexArray2D class in AniGraph. Under the hood, you can think of these classes as arrays of Vec2 objects with a bunch of convenience functions added. The polygon itself is represented by the set of Vec2 points and the set of edges that connect them. The edges aren't stored explicitly, though---we simply assume that every consecutive pair of vertices is connected by a numbered edge:
Some useful functions for accessing polygon data (which can be found in the VertexArray2D
class):
/**
* Returns the number of vertices in the polygon
* @returns {number}
*/
get nVerts():number{
return this.position.nVerts;
}
/**
* Returns the position of the vertex corresponding to the provided index
* @param index - the index of the vertex you want
* @returns {Vec2}
*/
vertexAt(index:number):Vec2{
return this.getPoint2DAt(index);
}
/**
* Returns a copy of the polygon with vertices that have been transformed by the provided matrix.
* @param {Mat3 | Mat4} m
* @returns {this}
* @constructor
*/
GetTransformedBy(m:Mat3|Mat4){
let rval = this.clone();
rval.ApplyMatrix(m);
return rval;
}
Cats, the Player, and the Polygon2DModel
class
The Polygon2D class represents geometry, which is an important property of various entities in our game (i.e., the player and cats).
But these entities, which are ANodeModels
in AniGraph, also have other properties like their current position or texture.
The Polygon2DModel
class holds these properties in addition to Polygon2D geometry and acts as a common parent to
players and cats.
For those of you familiar with the Model View Controller
framework, ANodeModel
s are the MVC model corresponding to a node in our scene graph.