Core 1.0
Public Member Functions | Static Public Member Functions | Protected Attributes | Friends | List of all members
Polygon2 Class Reference

Polygons in the plane. More...

#include <polygon.h>

Inheritance diagram for Polygon2:
Convex2

Public Member Functions

 Polygon2 ()
 Create an empty polygon.
 
 Polygon2 (Vector2 *, int)
 Create a polygon given an array of points. More...
 
 Polygon2 (const Vector2 &, const Vector2 &, const Vector2 &)
 Create a triangle. More...
 
 Polygon2 (const Vector2 &, const Vector2 &, const Vector2 &, const Vector2 &)
 Create a quadrangle. More...
 
 Polygon2 (const QVector< Vector > &)
 Create a polygon. More...
 
 Polygon2 (const QVector< Vector2 > &)
 Create a polygon. More...
 
 Polygon2 (const Triangle2 &)
 Create a triangle. More...
 
 Polygon2 (const Box2 &)
 Create a polygon from a box. More...
 
 Polygon2 (const Hexagon2 &)
 Create a hexagon. More...
 
 Polygon2 (const Pentagon2 &)
 Create a pentagon. More...
 
 Polygon2 (const Quadrangle2 &)
 Create a quadrangle. More...
 
 Polygon2 (const Polygonal &)
 Create a planar polygon for a polygon. More...
 
Box2 GetBox () const
 Compute the bounding box of the polygon. More...
 
void Translate (const Vector2 &)
 Translate the polygon by a given vector. More...
 
void Scale (const double &)
 Scale the polygon by a given factor. More...
 
void Rotate (const Matrix2 &)
 Rotate the polygon. More...
 
void Append (const Vector2 &)
 Add a vertex to the polygon. More...
 
Vector2Vertex (int)
 Read write access to the i-th point. More...
 
Vector2 Vertex (int) const
 Read only access to the i-th point. More...
 
QVector< Vector2Vertices ()
 Get the array of vertices.
 
const QVector< Vector2 > & Vertices () const
 Get the array of vertices.
 
Vector2 Edge (int) const
 Return the i-th edge, starting from the i-th vertex. More...
 
int Size () const
 Return the number of vertices of the polygon.
 
double Length () const
 Compute the perimeter of the polygon.
 
bool IsConvex () const
 Check whether the polygon is convex.
 
Vector2 PointAtLength (const double &) const
 Return the position of the point on the polygon at a given length from the starting point. More...
 
Vector2 NormalAtLength (const double &) const
 Return the normal at the position of the point on the polygon. More...
 
bool Inside (const Vector2 &) const
 Check if a point is inside or outside of the polygon. More...
 
double R (const Vector2 &) const
 Compute the squared distance between a point and a polygon. More...
 
double RC (const Vector2 &) const
 Compute the squared distance between a point and the contour of the polygon. More...
 
double R (const Line2 &) const
 Compute the distance from a polygon to a line. More...
 
double Signed (const Vector2 &) const
 Compute the signed distance between a point and a polygon. More...
 
bool IntersectSegment (const Segment2 &) const
 Test if a segment intersects the polygon. More...
 
double Area () const
 Compute the area of the polygon.
 
Vector2 Centroid () const
 Compute the centroid of the polygon. More...
 
Vector2 Center () const
 Compute the barycenter of the polygon. More...
 
void Subdivide (int, QVector< Vector2 > &, QVector< int > &) const
 Create an n-adic subdivision of a polygon. More...
 
void Expand (const double &)
 Expand the polygon by a given distance. More...
 
void Draw (QGraphicsScene &, const QPen &=QPen(), const QBrush &=QBrush()) const
 Draw a polygon. More...
 
Polygon2 Resampled (const double &) const
 Resample the edges of the polygon. More...
 
QVector< int > EarClip () const
 Compute a simple polygon triangulation using a ear clipping method. More...
 

Static Public Member Functions

static bool Inside (const QVector< Polygon2 > &, const Vector2 &)
 Test if a point is inside the union of a set of polygons. More...
 
static double R (const QVector< Polygon2 > &, const Vector2 &)
 Compute the squared distance between a point and a set of polygons. More...
 

Protected Attributes

QVector< Vector2q
 Array of vertices.
 

Friends

std::ostream & operator<< (std::ostream &, const Polygon2 &)
 Overloaded stream operator. More...
 

Detailed Description

Polygons in the plane.

Note that polygons can be non-convex or even self intersecting.

Constructor & Destructor Documentation

◆ Polygon2() [1/11]

Polygon2::Polygon2 ( Vector2 a,
int  n 
)
explicit

Create a polygon given an array of points.

Note that the points should be provided in trigonometric order.

Parameters
aArray of vertices.
nSize.

◆ Polygon2() [2/11]

Polygon2::Polygon2 ( const Vector2 a,
const Vector2 b,
const Vector2 c 
)
explicit

Create a triangle.

Parameters
a,b,cPoints.

◆ Polygon2() [3/11]

Polygon2::Polygon2 ( const Vector2 a,
const Vector2 b,
const Vector2 c,
const Vector2 d 
)
explicit

Create a quadrangle.

Parameters
a,b,c,dPoints.

◆ Polygon2() [4/11]

Polygon2::Polygon2 ( const QVector< Vector > &  p)
explicit

Create a polygon.

Parameters
pArray of points.

◆ Polygon2() [5/11]

Polygon2::Polygon2 ( const QVector< Vector2 > &  p)
explicit

Create a polygon.

Parameters
pArray of points.

◆ Polygon2() [6/11]

Polygon2::Polygon2 ( const Triangle2 t)
explicit

Create a triangle.

Parameters
tTriangle.

◆ Polygon2() [7/11]

Polygon2::Polygon2 ( const Box2 box)
explicit

Create a polygon from a box.

See also
Polygon2(const Hexagon2&)
Parameters
boxThe box.

◆ Polygon2() [8/11]

Polygon2::Polygon2 ( const Hexagon2 hexagon)
explicit

Create a hexagon.

See also
Polygon2(const Box2&)
Parameters
hexagonHexagon.

◆ Polygon2() [9/11]

Polygon2::Polygon2 ( const Pentagon2 pentagon)
explicit

Create a pentagon.

See also
Polygon2(const Box2&)
Parameters
pentagonPentagon.

◆ Polygon2() [10/11]

Polygon2::Polygon2 ( const Quadrangle2 quadrangle)
explicit

Create a quadrangle.

See also
Polygon2(const Box2&)
Parameters
quadrangleQuadrangle.

◆ Polygon2() [11/11]

Polygon2::Polygon2 ( const Polygonal p)
explicit

Create a planar polygon for a polygon.

Parameters
pArray of points.

Member Function Documentation

◆ Append()

void Polygon2::Append ( const Vector2 p)
inline

Add a vertex to the polygon.

Parameters
pPoint.

◆ Center()

Vector2 Polygon2::Center ( ) const

Compute the barycenter of the polygon.

Note that the barycenter is not the same as the centroid.

See also
Centroid

◆ Centroid()

Vector2 Polygon2::Centroid ( ) const

Compute the centroid of the polygon.

Note that the centroid is not the same as the barycenter.

See also
Barycenter

◆ Draw()

void Polygon2::Draw ( QGraphicsScene &  scene,
const QPen &  pen = QPen(),
const QBrush &  brush = QBrush() 
) const

Draw a polygon.

Parameters
sceneGraphics scene.
penThe pen.
brushThe brush.

◆ EarClip()

QVector< int > Polygon2::EarClip ( ) const

Compute a simple polygon triangulation using a ear clipping method.

Returns
Indices triplet which define triangles.

◆ Edge()

Vector2 Polygon2::Edge ( int  i) const
inline

Return the i-th edge, starting from the i-th vertex.

Parameters
iIndex.

◆ Expand()

void Polygon2::Expand ( const double &  e)

Expand the polygon by a given distance.

Parameters
eExpansion factor.

◆ GetBox()

Box2 Polygon2::GetBox ( ) const

Compute the bounding box of the polygon.

The polygon should have some vertices, otherwise the result is undefined.

◆ Inside() [1/2]

bool Polygon2::Inside ( const QVector< Polygon2 > &  s,
const Vector2 p 
)
static

Test if a point is inside the union of a set of polygons.

Polygons should be non-intersecting. Polygons inside others will be considered as holes.

Parameters
sSet of polygons.
pPoint.

◆ Inside() [2/2]

bool Polygon2::Inside ( const Vector2 p) const

Check if a point is inside or outside of the polygon.

Parameters
pPoint.

◆ IntersectSegment()

bool Polygon2::IntersectSegment ( const Segment2 s) const

Test if a segment intersects the polygon.

Parameters
sSegment.

◆ NormalAtLength()

Vector2 Polygon2::NormalAtLength ( const double &  length) const

Return the normal at the position of the point on the polygon.

Parameters
lengthPerimeter length from the starting point.

◆ PointAtLength()

Vector2 Polygon2::PointAtLength ( const double &  length) const

Return the position of the point on the polygon at a given length from the starting point.

Parameters
lengthDistance on the perimeter of the polygon. It should be less than the perimeter of the polygon, still a modulo operation is performed inside.

◆ R() [1/3]

double Polygon2::R ( const Line2 line) const

Compute the distance from a polygon to a line.

Parameters
lineLine.

◆ R() [2/3]

double Polygon2::R ( const QVector< Polygon2 > &  s,
const Vector2 p 
)
static

Compute the squared distance between a point and a set of polygons.

Parameters
pPoint.
sSet of polygons.

◆ R() [3/3]

double Polygon2::R ( const Vector2 p) const

Compute the squared distance between a point and a polygon.

Parameters
pPoint.

◆ RC()

double Polygon2::RC ( const Vector2 p) const

Compute the squared distance between a point and the contour of the polygon.

Parameters
pPoint.

◆ Resampled()

Polygon2 Polygon2::Resampled ( const double &  x) const

Resample the edges of the polygon.

Generate points separated by a given input distance on the edges.

Parameters
xDistance

◆ Rotate()

void Polygon2::Rotate ( const Matrix2 r)

Rotate the polygon.

Parameters
rRotation matrix.

◆ Scale()

void Polygon2::Scale ( const double &  s)

Scale the polygon by a given factor.

Parameters
sScaling factor.

◆ Signed()

double Polygon2::Signed ( const Vector2 p) const

Compute the signed distance between a point and a polygon.

Parameters
pPoint.

◆ Subdivide()

void Polygon2::Subdivide ( int  n,
QVector< Vector2 > &  vertex,
QVector< int > &  index 
) const

Create an n-adic subdivision of a polygon.

Note that the polygon should be star-shaped.

This function first computes the barycenter of the polygon and uses it to create triangles which are subdivided.

Parameters
nSubdivision level.
vertexArray of points.
indexArray of indexes defining the triangles.

◆ Translate()

void Polygon2::Translate ( const Vector2 t)

Translate the polygon by a given vector.

Parameters
tVector.

◆ Vertex() [1/2]

Vector2 & Polygon2::Vertex ( int  i)
inline

Read write access to the i-th point.

Parameters
iIndex.

◆ Vertex() [2/2]

Vector2 Polygon2::Vertex ( int  i) const
inline

Read only access to the i-th point.

Parameters
iIndex.

Friends And Related Function Documentation

◆ operator<<

std::ostream & operator<< ( std::ostream &  s,
const Polygon2 p 
)
friend

Overloaded stream operator.

Parameters
sStream.
pPolygon.