Polygon2D

Polygon2D

A polygon in two dimensions. Can be complex.

Constructor

new Polygon2D(points)

Source:
Implements:
Parameters:
Name Type Description
points Array.<module:shared.Point2D> The points that make up the polygon, given in order (clockwise and counter-clockwise are both fine).

Members

centroid :Array.<module:shared.Point2D>

Description:
  • Store the centroid of the polygon for quick hit tests.
Source:
Store the centroid of the polygon for quick hit tests.
Type:

points :Array.<module:shared.Point2D>

Description:
  • A closed list of the points making up this polygon. "Closed" here means that the first and last entries of the list are the same. Closing the polygon in this manner is handled by the constructor.
Source:
A closed list of the points making up this polygon. "Closed" here means that the first and last entries of the list are the same. Closing the polygon in this manner is handled by the constructor.
Type:

radius :number

Description:
  • Save the maximum radius of the polygon for quick hit tests.
Source:
Save the maximum radius of the polygon for quick hit tests.
Type:
  • number

Methods

contains(p) → {boolean}

Description:
  • Determines if a point is inside the polygon. Rules for deciding whether a point is inside the polygon: 1. If it is clearly outside, return false. 2. If it is clearly inside, return true. 3. If it is on a left or bottom edge, return true. 4. If it is on a right or top edge, return false. 5. If it is on a lower-left vertex, return true. 6. If it is on a lower-right, upper-left, or upper-right vertex, return false. Uses the winding number method for robust and efficient point-in-polygon detection.
Source:
Implements:
See:
Parameters:
Name Type Description
p Array.<module:shared.Point2D> Point to test.
Returns:
true if the point is inside the polygon, false otherwise.
Type
boolean

winding_number(point) → {number}

Description:
  • Winding number test for a point in a polygon
Source:
See:
Parameters:
Name Type Description
point Array.<module:shared.Point2D> The point to test.
Returns:
The winding number (=0 only when P is outside)
Type
number