Skip to content

Commit

Permalink
New geometry class: QgsRegularPolygon (#4502)
Browse files Browse the repository at this point in the history
  • Loading branch information
lbartoletti authored and nyalldawson committed May 10, 2017
1 parent 55cb855 commit 13c1318
Show file tree
Hide file tree
Showing 16 changed files with 1,095 additions and 18 deletions.
1 change: 1 addition & 0 deletions python/core/core.sip
Expand Up @@ -393,6 +393,7 @@
%Include geometry/qgspointv2.sip
%Include geometry/qgspolygon.sip
%Include geometry/qgsrectangle.sip
%Include geometry/qgsregularpolygon.sip
%Include geometry/qgssurface.sip
%Include geometry/qgstriangle.sip
%Include geometry/qgswkbtypes.sip
Expand Down
6 changes: 3 additions & 3 deletions python/core/geometry/qgsellipse.sip
Expand Up @@ -189,11 +189,11 @@ The circumference of the ellipse using first approximation of Ramanujan.
:rtype: list of QgsPointV2
%End

virtual void points( QgsPointSequence &pts, unsigned int segments = 36 ) const;
virtual QgsPointSequence points( unsigned int segments = 36 ) const;
%Docstring
Returns a list of points into ``pts``, with segmentation from ``segments``.
\param pts List of points returned.
Returns a list of points with segmentation from ``segments``.
\param segments Number of segments used to segment geometry.
:rtype: QgsPointSequence
%End

virtual QgsPolygonV2 *toPolygon( unsigned int segments = 36 ) const /Factory/;
Expand Down
232 changes: 232 additions & 0 deletions python/core/geometry/qgsregularpolygon.sip
@@ -0,0 +1,232 @@
/************************************************************************
* This file has been generated automatically from *
* *
* src/core/geometry/qgsregularpolygon.h *
* *
* Do not edit manually ! Edit header and run scripts/sipify.pl again *
************************************************************************/






class QgsRegularPolygon
{
%Docstring
Regular Polygon geometry type.

A regular polygon is a polygon that is equiangular (all angles are equal in measure) and equilateral (all sides have the same length).
The regular polygon is defined by a center point with a number of sides/vertices, a radius and the first vertex.

.. versionadded:: 3.0
%End

%TypeHeaderCode
#include "qgsregularpolygon.h"
%End
public:

enum ConstructionOption
{
InscribedCircle,
CircumscribedCircle
};

QgsRegularPolygon();

QgsRegularPolygon( const QgsPointV2 &center, const double radius, const double azimuth, const int numberSides, const ConstructionOption circle );
%Docstring
Constructs a regular polygon by ``center`` and parameters for the first vertex. An empty regular polygon is returned if ``numberSides`` < 3 or ``ConstructionOption`` isn't valid.
\param center The center of the regular polygon.
\param radius Distance from the center and the first vertex or sides (see ``ConstructionOption``).
\param azimuth Angle in degrees started from the North to the first vertex.
\param numberSides Number of sides of the regular polygon.
.. seealso:: ConstructionOption
%End

QgsRegularPolygon( const QgsPointV2 &center, const QgsPointV2 &pt1, const int numberSides, const ConstructionOption circle );
%Docstring
Constructs a regular polygon by ``center`` and another point.
\param center The center of the regular polygon.
\param pt1 The first vertex if the polygon is inscribed in circle or the midpoint of a side if the polygon is circumscribed about circle.
\param numberSides Number of sides of the regular polygon.
\param circle Option to create the polygon inscribed in circle (the radius is the distance between the center and vertices) or circumscribed about circle (the radius is the distance from the center to the midpoints of the sides).
%End

QgsRegularPolygon( const QgsPointV2 &pt1, const QgsPointV2 &pt2, const int numberSides );
%Docstring
Constructs a regular polygon by two points of the first side.
\param pt1 The first vertex of the first side, also first vertex of the regular polygon.
\param pt2 The second vertex of the first side.
\param numberSides Number of sides of the regular polygon.
%End

bool operator ==( const QgsRegularPolygon &rp ) const;
bool operator !=( const QgsRegularPolygon &rp ) const;

bool isEmpty() const;
%Docstring
A regular polygon is empty if radius equal to 0 or number of sides < 3
:rtype: bool
%End

QgsPointV2 center() const;
%Docstring
Returns the center point of the regular polygon.
.. seealso:: setCenter()
:rtype: QgsPointV2
%End

double radius() const;
%Docstring
Returns the radius.
This is also the radius of the circumscribing circle.
.. seealso:: apothem()
.. seealso:: setRadius()
:rtype: float
%End

QgsPointV2 firstVertex() const;
%Docstring
Returns the first vertex (corner) of the regular polygon.
.. seealso:: setFirstVertex()
:rtype: QgsPointV2
%End

double apothem() const;
%Docstring
Returns the apothem of the regular polygon.
The apothem is the radius of the inscribed circle.
.. seealso:: radius()
:rtype: float
%End

int numberSides() const;
%Docstring
Returns the number of sides of the regular polygon.
.. seealso:: setNumberSides()
:rtype: int
%End

void setCenter( const QgsPointV2 &center );
%Docstring
Sets the center point.
Radius is unchanged. The first vertex is reprojected from the new center.
.. seealso:: center()
%End

void setRadius( const double radius );
%Docstring
Sets the radius.
Center is unchanged. The first vertex is reprojected from the center with the new radius.
.. seealso:: radius()
%End

void setFirstVertex( const QgsPointV2 &firstVertex );
%Docstring
Sets the first vertex.
Radius is unchanged. The center is reprojected from the new first vertex.
.. seealso:: firstVertex()
%End

void setNumberSides( const int numberSides );
%Docstring
Sets the number of sides.
If numberSides < 3, the number of sides is unchanged.
.. seealso:: numberSides()
%End

QgsPointSequence points( ) const;
%Docstring
Returns a list including the vertices of the regular polygon.
:rtype: QgsPointSequence
%End

QgsPolygonV2 *toPolygon( ) const /Factory/;
%Docstring
Returns as a polygon.
:rtype: QgsPolygonV2
%End

QgsLineString *toLineString( ) const /Factory/;
%Docstring
Returns as a linestring.
:rtype: QgsLineString
%End

QgsTriangle toTriangle( ) const;
%Docstring
Returns as a triangle.
An empty triangle is returned if the regular polygon is empty or if the number of sides is different from 3.
:rtype: QgsTriangle
%End

QList<QgsTriangle> triangulate( ) const;
%Docstring
Returns a triangulation (vertices from sides to the center) of the regular polygon.
An empty list is returned if the regular polygon is empty.
:rtype: list of QgsTriangle
%End

QgsCircle inscribedCircle( ) const;
%Docstring
Returns the inscribed circle
:rtype: QgsCircle
%End

QgsCircle circumscribedCircle( ) const;
%Docstring
Returns the circumscribed circle
:rtype: QgsCircle
%End

QString toString( int pointPrecision = 17, int radiusPrecision = 17, int anglePrecision = 2 ) const;
%Docstring
Returns a string representation of the regular polygon.
Members will be truncated to the specified precision.
:rtype: str
%End

double interiorAngle( ) const;
%Docstring
Returns the measure of the interior angles in degrees.
:rtype: float
%End

double centralAngle( ) const;
%Docstring
Returns the measure of the central angle (the angle subtended at the center of the polygon by one of its sides) in degrees.
:rtype: float
%End

double area( ) const;
%Docstring
Returns the area.
Returns 0 if the regular polygon is empty.
:rtype: float
%End

double perimeter( ) const;
%Docstring
Returns the perimeter.
Returns 0 if the regular polygon is empty.
:rtype: float
%End

double length( ) const;
%Docstring
Returns the length of a side.
Returns 0 if the regular polygon is empty.
:rtype: float
%End

};

/************************************************************************
* This file has been generated automatically from *
* *
* src/core/geometry/qgsregularpolygon.h *
* *
* Do not edit manually ! Edit header and run scripts/sipify.pl again *
************************************************************************/
8 changes: 8 additions & 0 deletions python/core/geometry/qgstriangle.sip
Expand Up @@ -49,6 +49,14 @@ class QgsTriangle : QgsPolygonV2
\param p3 third point
%End

bool operator==( const QgsTriangle &other ) const;
%Docstring
:rtype: bool
%End
bool operator!=( const QgsTriangle &other ) const;
%Docstring
:rtype: bool
%End

virtual QString geometryType() const;
virtual QgsTriangle *clone() const /Factory/;
Expand Down
4 changes: 2 additions & 2 deletions resources/function_help/json/make_ellipse
Expand Up @@ -5,8 +5,8 @@
"variableLenArguments": true,
"arguments": [
{"arg":"center", "description": "center point of the ellipse"},
{"arg":"semi-major axis", "description": "semi-major axis of the ellipse"},
{"arg":"semi-minor axis", "description": "semi-minor axis of the ellipse"},
{"arg":"semi_major_axis", "description": "semi-major axis of the ellipse"},
{"arg":"semi_minor_axis", "description": "semi-minor axis of the ellipse"},
{"arg":"azimuth", "description": "orientation of the ellipse"},
{"arg":"segment", "description": "optional argument for polygon segmentation. By default this value is 36"}],
"examples": [ { "expression":"geom_to_wkt(make_ellipse(make_point(10,10), 5, 2, 90, 4))", "returns":"'Polygon ((15 10, 10 8, 5 10, 10 12, 15 10))'"},
Expand Down
14 changes: 14 additions & 0 deletions resources/function_help/json/make_regular_polygon
@@ -0,0 +1,14 @@
{
"name": "make_regular_polygon",
"type": "function",
"description": "Creates a regular polygon.",
"variableLenArguments": true,
"arguments": [
{"arg":"center", "description": "center of the regular polygon"},
{"arg":"radius", "description": "second point. The first if the regular polygon is inscribed. The midpoint of the first side if the regular polygon is circumscribed."},
{"arg":"number_sides", "description": "Number of sides/edges of the regular polygon"},
{"arg":"circle", "description": "Optional argument to construct the regular polygon. By default this value is 0. Value can be 0 (inscribed) or 1 (circumscribed)"}],
"examples": [ { "expression":"geom_to_wkt(make_regular_polygon(make_point(0,0), make_point(0,5), 5))", "returns":"'Polygon ((0 5, 4.76 1.55, 2.94 -4.05, -2.94 -4.05, -4.76 1.55, 0 5))'"},
{ "expression":"geom_to_wkt(make_regular_polygon(make_point(0,0), project(make_point(0,0), 4.0451, radians(36)), 5))", "returns":"'Polygon ((0 5, 4.76 1.55, 2.94 -4.05, -2.94 -4.05, -4.76 1.55, 0 5))'"}
]
}
2 changes: 2 additions & 0 deletions src/core/CMakeLists.txt
Expand Up @@ -406,6 +406,7 @@ SET(QGIS_CORE_SRCS
geometry/qgspointv2.cpp
geometry/qgspolygon.cpp
geometry/qgsrectangle.cpp
geometry/qgsregularpolygon.cpp
geometry/qgstriangle.cpp
geometry/qgswkbptr.cpp
geometry/qgswkbtypes.cpp
Expand Down Expand Up @@ -974,6 +975,7 @@ SET(QGIS_CORE_HDRS
geometry/qgspointv2.h
geometry/qgspolygon.h
geometry/qgsrectangle.h
geometry/qgsregularpolygon.h
geometry/qgstriangle.h
geometry/qgssurface.h
geometry/qgswkbptr.h
Expand Down
11 changes: 7 additions & 4 deletions src/core/geometry/qgsellipse.cpp
Expand Up @@ -184,12 +184,13 @@ QVector<QgsPointV2> QgsEllipse::quadrant() const
return quad;
}

void QgsEllipse::points( QgsPointSequence &pts, unsigned int segments ) const
QgsPointSequence QgsEllipse::points( unsigned int segments ) const
{
pts.clear();
QgsPointSequence pts;

if ( segments < 3 )
{
return;
return pts;
}


Expand All @@ -214,6 +215,8 @@ void QgsEllipse::points( QgsPointSequence &pts, unsigned int segments ) const
mSemiMinorAxis * sin( *it ) * cos( azimuth );
pts.push_back( QgsPointV2( pType, x, y, z, m ) );
}

return pts;
}

QgsPolygonV2 *QgsEllipse::toPolygon( unsigned int segments ) const
Expand All @@ -238,7 +241,7 @@ QgsLineString *QgsEllipse::toLineString( unsigned int segments ) const
}

QgsPointSequence pts;
points( pts, segments );
pts = points( segments );

ext->setPoints( pts );

Expand Down
5 changes: 2 additions & 3 deletions src/core/geometry/qgsellipse.h
Expand Up @@ -176,11 +176,10 @@ class CORE_EXPORT QgsEllipse
*/
virtual QVector<QgsPointV2> quadrant() const;

/** Returns a list of points into \a pts, with segmentation from \a segments.
* \param pts List of points returned.
/** Returns a list of points with segmentation from \a segments.
* \param segments Number of segments used to segment geometry.
*/
virtual void points( QgsPointSequence &pts, unsigned int segments = 36 ) const;
virtual QgsPointSequence points( unsigned int segments = 36 ) const;

/** Returns a segmented polygon.
* \param segments Number of segments used to segment geometry.
Expand Down

0 comments on commit 13c1318

Please sign in to comment.