qgsregularpolygon.h 8.14 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248
/***************************************************************************
                         qgsregularpolygon.h
                         --------------
    begin                : May 2017
    copyright            : (C) 2017 by Loîc Bartoletti
    email                : lbartoletti at tuxfamily dot org
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/

#ifndef QGSREGULARPOLYGON_H
#define QGSREGULARPOLYGON_H

#include <QString>

#include "qgis_core.h"
#include "qgspoint.h"
#include "qgspolygon.h"
#include "qgslinestring.h"
#include "qgscircle.h"
#include "qgstriangle.h"


/**
 * \ingroup core
 * \class QgsRegularPolygon
 * \brief 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.
 *
 * \since QGIS 3.0
 */
class CORE_EXPORT QgsRegularPolygon
{
  public:

    /**
     * A regular polygon can be constructed inscribed in a circle or circumscribed about a circle.
     *
     */
    enum ConstructionOption
    {
      InscribedCircle, //<! Inscribed in a circle (the radius is the distance between the center and vertices)
      CircumscribedCircle //<! Circumscribed about a circle (the radius is the distance from the center to the midpoints of the sides)
    };

    /**
     * Constructor for QgsRegularPolygon.
     */
    QgsRegularPolygon() = default;

    /**
     * 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.
     * \param center The center of the regular polygon.
     * \param radius Distance from the center and the first vertex or sides (see \a ConstructionOption).
     * \param azimuth Angle in degrees started from the North to the first vertex.
     * \param numberSides Number of sides of the regular polygon.
     * \param circle Option to create the polygon. \see ConstructionOption
     */
    QgsRegularPolygon( const QgsPoint &center, double radius, double azimuth, unsigned int numberSides, ConstructionOption circle );

    /**
     * Constructs a regular polygon by \a 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).
     */
    QgsRegularPolygon( const QgsPoint &center, const QgsPoint &pt1, unsigned int numberSides, ConstructionOption circle );

    /**
     * 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.
     */
    QgsRegularPolygon( const QgsPoint &pt1, const QgsPoint &pt2, unsigned int numberSides );

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

    //! A regular polygon is empty if radius equal to 0 or number of sides < 3
    bool isEmpty() const;

    /**
     * Returns the center point of the regular polygon.
     * \see setCenter()
     */
    QgsPoint center() const { return mCenter; }

    /**
     * Returns the radius.
     * This is also the radius of the circumscribing circle.
     * \see apothem()
     * \see setRadius()
     */
    double radius() const { return mRadius; }

    /**
     * Returns the first vertex (corner) of the regular polygon.
     * \see setFirstVertex()
     */
    QgsPoint firstVertex() const { return mFirstVertex; }

    /**
     * Returns the apothem of the regular polygon.
     * The apothem is the radius of the inscribed circle.
     * \see radius()
     */
    double apothem() const { return mRadius * std::cos( M_PI / mNumberSides ); }

    /**
     * Returns the number of sides of the regular polygon.
     * \see setNumberSides()
     */
    unsigned int numberSides() const { return mNumberSides; }

    /**
     * Sets the center point.
     * Radius is unchanged. The first vertex is reprojected from the new center.
     * \see center()
     */
    void setCenter( const QgsPoint &center );

    /**
     * Sets the radius.
     * Center is unchanged. The first vertex is reprojected from the center with the new radius.
     * \see radius()
     */
    void setRadius( double radius );

    /**
     * Sets the first vertex.
     * Radius is unchanged. The center is reprojected from the new first vertex.
     * \see firstVertex()
     */
    void setFirstVertex( const QgsPoint &firstVertex );

    /**
     * Sets the number of sides.
     * If numberSides < 3, the number of sides is unchanged.
     * \see numberSides()
     */
    void setNumberSides( unsigned int numberSides );

    /**
     * Returns a list including the vertices of the regular polygon.
     */
    QgsPointSequence points() const;

    /**
     * Returns as a polygon.
     */
    QgsPolygon *toPolygon() const SIP_FACTORY;

    /**
     * Returns as a linestring.
     */
    QgsLineString *toLineString() const SIP_FACTORY;

    /**
     * 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.
     */
    QgsTriangle toTriangle() const;

    /**
     * Returns a triangulation (vertices from sides to the center) of the regular polygon.
     * An empty list is returned if the regular polygon is empty.
     */
    QVector<QgsTriangle> triangulate() const;

    /**
     * Returns the inscribed circle
     */
    QgsCircle inscribedCircle() const;

    /**
     * Returns the circumscribed circle
     */
    QgsCircle circumscribedCircle() const;

    /**
     * Returns a string representation of the regular polygon.
     * Members will be truncated to the specified precision.
     */
    QString toString( int pointPrecision = 17, int radiusPrecision = 17, int anglePrecision = 2 ) const;

    /**
     * Returns the measure of the interior angles in degrees.
     */
    double interiorAngle() const;

    /**
     * Returns the measure of the central angle (the angle subtended at the center of the polygon by one of its sides) in degrees.
     */
    double centralAngle() const;

    /**
     * Returns the area.
     * Returns 0 if the regular polygon is empty.
     */
    double area() const;

    /**
     * Returns the perimeter.
     * Returns 0 if the regular polygon is empty.
     */
    double perimeter() const;

    /**
     * Returns the length of a side.
     * Returns 0 if the regular polygon is empty.
     */
    double length() const;

  private:
    QgsPoint mCenter;
    QgsPoint mFirstVertex;
    unsigned int mNumberSides = 0;
    double mRadius = 0.0;

    /**
     * Convenient method to convert an apothem to a radius.
     */
    double apothemToRadius( double apothem, unsigned int numberSides ) const;

    /**
     * Convenient method for interiorAngle used by constructors.
     */
    double interiorAngle( unsigned int nbSides ) const;

    /**
     * Convenient method for centralAngle used by constructors.
     */
    double centralAngle( unsigned int nbSides ) const;

};

#endif // QGSREGULARPOLYGON_H