1 /*************************************************************************** 2 qgsregularpolygon.h 3 -------------- 4 begin : May 2017 5 copyright : (C) 2017 by Loîc Bartoletti 6 email : lbartoletti at tuxfamily dot org 7 ***************************************************************************/ 8 9 /*************************************************************************** 10 * * 11 * This program is free software; you can redistribute it and/or modify * 12 * it under the terms of the GNU General Public License as published by * 13 * the Free Software Foundation; either version 2 of the License, or * 14 * (at your option) any later version. * 15 * * 16 ***************************************************************************/ 17 18 #ifndef QGSREGULARPOLYGON_H 19 #define QGSREGULARPOLYGON_H 20 21 #include <QString> 22 23 #include "qgis_core.h" 24 #include "qgspoint.h" 25 #include "qgspolygon.h" 26 #include "qgslinestring.h" 27 #include "qgscircle.h" 28 #include "qgstriangle.h" 29 30 31 /** 32 * \ingroup core 33 * \class QgsRegularPolygon 34 * \brief Regular Polygon geometry type. 35 * 36 * A regular polygon is a polygon that is equiangular (all angles are equal in measure) and equilateral (all sides have the same length). 37 * The regular polygon is defined by a center point with a number of sides/vertices, a radius and the first vertex. 38 * 39 * \since QGIS 3.0 40 */ 41 class CORE_EXPORT QgsRegularPolygon 42 { 43 public: 44 45 /** 46 * A regular polygon can be constructed inscribed in a circle or circumscribed about a circle. 47 * 48 */ 49 enum ConstructionOption 50 { 51 InscribedCircle, //!< Inscribed in a circle (the radius is the distance between the center and vertices) 52 CircumscribedCircle //!< Circumscribed about a circle (the radius is the distance from the center to the midpoints of the sides) 53 }; 54 55 /** 56 * Constructor for QgsRegularPolygon. 57 */ 58 QgsRegularPolygon() SIP_HOLDGIL = default; 59 60 /** 61 * Constructs a regular polygon by \a center and parameters for the first vertex. An empty regular polygon is returned if \a numberSides < 3 or \a ConstructionOption isn't valid. 62 * \param center The center of the regular polygon. 63 * \param radius Distance from the center and the first vertex or sides (see \a ConstructionOption). 64 * \param azimuth Angle in degrees started from the North to the first vertex. 65 * \param numberSides Number of sides of the regular polygon. 66 * \param circle Option to create the polygon. \see ConstructionOption 67 */ 68 QgsRegularPolygon( const QgsPoint ¢er, double radius, double azimuth, unsigned int numberSides, ConstructionOption circle ) SIP_HOLDGIL; 69 70 /** 71 * Constructs a regular polygon by \a center and another point. 72 * \param center The center of the regular polygon. 73 * \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. 74 * \param numberSides Number of sides of the regular polygon. 75 * \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). 76 */ 77 QgsRegularPolygon( const QgsPoint ¢er, const QgsPoint &pt1, unsigned int numberSides, ConstructionOption circle ) SIP_HOLDGIL; 78 79 /** 80 * Constructs a regular polygon by two points of the first side. 81 * \param pt1 The first vertex of the first side, also first vertex of the regular polygon. 82 * \param pt2 The second vertex of the first side. 83 * \param numberSides Number of sides of the regular polygon. 84 */ 85 QgsRegularPolygon( const QgsPoint &pt1, const QgsPoint &pt2, unsigned int numberSides ) SIP_HOLDGIL; 86 87 bool operator ==( const QgsRegularPolygon &rp ) const SIP_HOLDGIL; 88 bool operator !=( const QgsRegularPolygon &rp ) const SIP_HOLDGIL; 89 90 //! A regular polygon is empty if radius equal to 0 or number of sides < 3 91 bool isEmpty() const SIP_HOLDGIL; 92 93 /** 94 * Returns the center point of the regular polygon. 95 * \see setCenter() 96 */ center()97 QgsPoint center() const SIP_HOLDGIL { return mCenter; } 98 99 /** 100 * Returns the radius. 101 * This is also the radius of the circumscribing circle. 102 * \see apothem() 103 * \see setRadius() 104 */ radius()105 double radius() const SIP_HOLDGIL { return mRadius; } 106 107 /** 108 * Returns the first vertex (corner) of the regular polygon. 109 * \see setFirstVertex() 110 */ firstVertex()111 QgsPoint firstVertex() const SIP_HOLDGIL { return mFirstVertex; } 112 113 /** 114 * Returns the apothem of the regular polygon. 115 * The apothem is the radius of the inscribed circle. 116 * \see radius() 117 */ apothem()118 double apothem() const SIP_HOLDGIL { return mRadius * std::cos( M_PI / mNumberSides ); } 119 120 /** 121 * Returns the number of sides of the regular polygon. 122 * \see setNumberSides() 123 */ numberSides()124 unsigned int numberSides() const SIP_HOLDGIL { return mNumberSides; } 125 126 /** 127 * Sets the center point. 128 * Radius is unchanged. The first vertex is reprojected from the new center. 129 * \see center() 130 */ 131 void setCenter( const QgsPoint ¢er ) SIP_HOLDGIL; 132 133 /** 134 * Sets the radius. 135 * Center is unchanged. The first vertex is reprojected from the center with the new radius. 136 * \see radius() 137 */ 138 void setRadius( double radius ) SIP_HOLDGIL; 139 140 /** 141 * Sets the first vertex. 142 * Radius is unchanged. The center is reprojected from the new first vertex. 143 * \see firstVertex() 144 */ 145 void setFirstVertex( const QgsPoint &firstVertex ) SIP_HOLDGIL; 146 147 /** 148 * Sets the number of sides. 149 * If numberSides < 3, the number of sides is unchanged. 150 * \see numberSides() 151 */ 152 void setNumberSides( unsigned int numberSides ) SIP_HOLDGIL; 153 154 /** 155 * Returns a list including the vertices of the regular polygon. 156 */ 157 QgsPointSequence points() const; 158 159 /** 160 * Returns as a polygon. 161 */ 162 QgsPolygon *toPolygon() const SIP_FACTORY; 163 164 /** 165 * Returns as a linestring. 166 */ 167 QgsLineString *toLineString() const SIP_FACTORY; 168 169 /** 170 * Returns as a triangle. 171 * An empty triangle is returned if the regular polygon is empty or if the number of sides is different from 3. 172 */ 173 QgsTriangle toTriangle() const; 174 175 /** 176 * Returns a triangulation (vertices from sides to the center) of the regular polygon. 177 * An empty list is returned if the regular polygon is empty. 178 */ 179 QVector<QgsTriangle> triangulate() const; 180 181 /** 182 * Returns the inscribed circle 183 */ 184 QgsCircle inscribedCircle() const SIP_HOLDGIL; 185 186 /** 187 * Returns the circumscribed circle 188 */ 189 QgsCircle circumscribedCircle() const SIP_HOLDGIL; 190 191 /** 192 * Returns a string representation of the regular polygon. 193 * Members will be truncated to the specified precision. 194 */ 195 QString toString( int pointPrecision = 17, int radiusPrecision = 17, int anglePrecision = 2 ) const; 196 197 /** 198 * Returns the measure of the interior angles in degrees. 199 */ 200 double interiorAngle() const SIP_HOLDGIL; 201 202 /** 203 * Returns the measure of the central angle (the angle subtended at the center of the polygon by one of its sides) in degrees. 204 */ 205 double centralAngle() const SIP_HOLDGIL; 206 207 /** 208 * Returns the area. 209 * Returns 0 if the regular polygon is empty. 210 */ 211 double area() const SIP_HOLDGIL; 212 213 /** 214 * Returns the perimeter. 215 * Returns 0 if the regular polygon is empty. 216 */ 217 double perimeter() const SIP_HOLDGIL; 218 219 /** 220 * Returns the length of a side. 221 * Returns 0 if the regular polygon is empty. 222 */ 223 double length() const SIP_HOLDGIL; 224 225 private: 226 QgsPoint mCenter; 227 QgsPoint mFirstVertex; 228 unsigned int mNumberSides = 0; 229 double mRadius = 0.0; 230 231 /** 232 * Convenient method to convert an apothem to a radius. 233 */ 234 double apothemToRadius( double apothem, unsigned int numberSides ) const; 235 236 /** 237 * Convenient method for interiorAngle used by constructors. 238 */ 239 double interiorAngle( unsigned int nbSides ) const; 240 241 /** 242 * Convenient method for centralAngle used by constructors. 243 */ 244 double centralAngle( unsigned int nbSides ) const; 245 246 }; 247 248 #endif // QGSREGULARPOLYGON_H 249