Polygon class

An arbitrary polygon with 3 or more vertices.

The vertices of the polygon are in the counter-clockwise order, so that the polygon's interior is in your left-hand side as you traverse the polygon's vertices.

A polygon can be either convex or not, the containsPoint method will work in both cases, however, the method used with convex polygon is faster.

Inheritance

Constructors

Polygon(List<Vector2> _vertices, {bool? convex})
Constructs the polygon from the given list of vertices.

Properties

aabb → Aabb2
The axis-aligned bounding box of the shape.
no setteroverride
center → Vector2
The central point of the shape.
no setteroverride
edges → List<Vector2>
The edges (sides) of the polygon.
no setter
hashCode → int
The hash code for this object.
no setterinherited
isClosed → bool
True if the shape is "closed", in the sense that it has an interior. For example, a closed shape can be filled with a paint.
no setterinherited
isConvex → bool
True if the shape is convex, i.e. a line segment connecting any two points of the shape would lie completely within the shape.
no setteroverride
perimeter → double
The length of the shape's boundary. For some more complicated shapes this can be computed approximately.
no setteroverride
runtimeType → Type
A representation of the runtime type of the object.
no setterinherited
vertices → List<Vector2>
The vertices (corners) of the polygon.
no setter

Methods

asPath() → Path
Converts the shape to a Path object, suitable for rendering on a canvas. If a particular geometric primitive cannot be represented as a Path faithfully, an approximate path can be returned.
override
containsPoint(Vector2 point) → bool
Returns true if the given point is inside the shape or on the boundary.
override
move(Vector2 offset) → void
Translates the shape by the specified offset vector, in-place.
override
nearestPoint(Vector2 point) → Vector2
Returns a point on the shape's boundary which is closest to the given point.
override
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
project(Transform2D transform, [Shape? target]) → Shape
Returns the result of applying an affine transformation to the shape.
override
randomPoint({Random? random, bool within = true}) → Vector2
Returns a random point within the shape if within is true (default) and otherwise a point along the edges of the shape. Do note that within=true also includes the edges.
override
support(Vector2 direction) → Vector2
Finds the intersection of this shape with another one, if it exists. Returns a point on the boundary that is furthest in the given direction.
override
toString() → String
A string representation of this object.
override

Operators

operator ==(Object other) → bool
The equality operator.
inherited

Static Methods

randomPointAlongEdges(List<Vector2> vertices, {Random? random}) → Vector2
Returns a random point on the vertices.