31 mNumberSides = numSides;
37 mRadius = std::fabs(
radius );
38 mFirstVertex = mCenter.project( mRadius, azimuth );
43 mRadius = apothemToRadius( std::fabs(
radius ), numSides );
44 mFirstVertex = mCenter.project( mRadius, azimuth -
centralAngle( numSides ) / 2 );
58 mNumberSides = numSides;
65 mRadius =
center.distance( pt1 );
70 mRadius = apothemToRadius(
center.distance( pt1 ), numSides );
71 const double azimuth =
center.azimuth( pt1 );
73 mFirstVertex = mCenter.project( mRadius, azimuth -
centralAngle( numSides ) / 2 );
86 mNumberSides = numSides;
88 const double azimuth = pt1.
azimuth( pt2 );
92 const double angle = ( 180 - ( 360 / numSides ) ) / 2.0;
93 const double hypothenuse =
length / std::cos( angle * M_PI / 180 );
96 mCenter = pt1.
project( hypothenuse, azimuth + angle );
98 mRadius = std::fabs( hypothenuse );
104 return ( ( mCenter == rp.mCenter ) &&
105 ( mFirstVertex == rp.mFirstVertex ) &&
106 ( mNumberSides == rp.mNumberSides )
117 return ( ( mNumberSides < 3 ) ||
118 ( mCenter.isEmpty() ) ||
119 ( mFirstVertex.isEmpty() ) ||
120 ( mCenter == mFirstVertex )
126 const double azimuth = mFirstVertex.isEmpty() ? 0 : mCenter.azimuth( mFirstVertex );
129 mFirstVertex =
center.project( mRadius, azimuth );
134 mRadius = std::fabs(
radius );
135 const double azimuth = mFirstVertex.isEmpty() ? 0 : mCenter.azimuth( mFirstVertex );
137 mFirstVertex = mCenter.project( mRadius, azimuth );
142 const double azimuth = mCenter.azimuth( mFirstVertex );
145 mCenter = mFirstVertex.project( mRadius, azimuth );
152 mNumberSides = numSides;
164 double azimuth = mCenter.azimuth( mFirstVertex );
169 while ( n <= mNumberSides )
171 pts.push_back( mCenter.project( mRadius, azimuth ) );
172 azimuth += azimuth_add;
173 if ( ( azimuth_add > 0 ) && ( azimuth > 180.0 ) )
186 auto polygon = std::make_unique<QgsPolygon>();
189 return polygon.release();
194 return polygon.release();
199 auto ext = std::make_unique<QgsLineString>();
202 return ext.release();
208 ext->setPoints( pts );
209 ext->addVertex( pts.at( 0 ) );
211 return ext.release();
216 if (
isEmpty() || ( mNumberSides != 3 ) )
224 return QgsTriangle( pts.at( 0 ), pts.at( 1 ), pts.at( 2 ) );
229 QVector<QgsTriangle> l_tri;
239 while ( n < mNumberSides - 1 )
241 l_tri.append(
QgsTriangle( pts.at( n ), pts.at( n + 1 ), mCenter ) );
244 l_tri.append(
QgsTriangle( pts.at( n ), pts.at( 0 ), mCenter ) );
265 rep = QStringLiteral(
"Empty" );
267 rep = QStringLiteral(
"RegularPolygon (Center: %1, First Vertex: %2, Radius: %3, Azimuth: %4)" )
268 .arg( mCenter.asWkt( pointPrecision ), 0,
's' )
269 .arg( mFirstVertex.asWkt( pointPrecision ), 0,
's' )
271 .arg(
qgsDoubleToString( mCenter.azimuth( mFirstVertex ), anglePrecision ), 0,
'f' );
285 return ( mRadius * mRadius * mNumberSides * std::sin(
centralAngle() * M_PI / 180.0 ) ) / 2;
295 return length() * mNumberSides;
305 return mRadius * 2 * std::sin( M_PI / mNumberSides );
308double QgsRegularPolygon::apothemToRadius(
const double apothem,
const unsigned int numSides )
const
310 return apothem / std::cos( M_PI / numSides );
315 return ( nbSides - 2 ) * 180 / nbSides;
320 return 360.0 / nbSides;
static QgsPoint midpoint(const QgsPoint &pt1, const QgsPoint &pt2)
Returns a middle point between points pt1 and pt2.
Line string geometry type, with support for z-dimension and m-values.
Point geometry type, with support for z-dimension and m-values.
double azimuth(const QgsPoint &other) const
Calculates Cartesian azimuth between this point and other one (clockwise in degree,...
double distance(double x, double y) const
Returns the Cartesian 2D distance between this point and a specified x, y coordinate.
QgsPoint project(double distance, double azimuth, double inclination=90.0) const
Returns a new point which corresponds to this point projected by a specified distance with specified ...
QgsPointSequence points() const
Returns a list including the vertices of the regular polygon.
QString toString(int pointPrecision=17, int radiusPrecision=17, int anglePrecision=2) const
Returns a string representation of the regular polygon.
QgsRegularPolygon()=default
Constructor for QgsRegularPolygon.
double interiorAngle() const
Returns the measure of the interior angles in degrees.
QgsPoint center() const
Returns the center point of the regular polygon.
QgsCircle circumscribedCircle() const
Returns the circumscribed circle.
QgsPoint firstVertex() const
Returns the first vertex (corner) of the regular polygon.
QgsLineString * toLineString() const
Returns as a linestring.
ConstructionOption
A regular polygon can be constructed inscribed in a circle or circumscribed about a circle.
@ CircumscribedCircle
Circumscribed about a circle (the radius is the distance from the center to the midpoints of the side...
@ InscribedCircle
Inscribed in a circle (the radius is the distance between the center and vertices).
double length() const
Returns the length of a side.
double perimeter() const
Returns the perimeter.
QgsTriangle toTriangle() const
Returns as a triangle.
double radius() const
Returns the radius.
void setNumberSides(unsigned int numberSides)
Sets the number of sides.
void setCenter(const QgsPoint ¢er)
Sets the center point.
QgsPolygon * toPolygon() const
Returns as a polygon.
bool operator==(const QgsRegularPolygon &rp) const
bool operator!=(const QgsRegularPolygon &rp) const
double area() const
Returns the area.
bool isEmpty() const
A regular polygon is empty if radius equal to 0 or number of sides < 3.
double apothem() const
Returns the apothem of the regular polygon.
void setRadius(double radius)
Sets the radius.
QVector< QgsTriangle > triangulate() const
Returns a triangulation (vertices from sides to the center) of the regular polygon.
double centralAngle() const
Returns the measure of the central angle (the angle subtended at the center of the polygon by one of ...
void setFirstVertex(const QgsPoint &firstVertex)
Sets the first vertex.
QgsCircle inscribedCircle() const
Returns the inscribed circle.
QString qgsDoubleToString(double a, int precision=17)
Returns a string representation of a double.
QVector< QgsPoint > QgsPointSequence