29using namespace Qt::StringLiterals;
31void QgsEllipse::normalizeAxis()
53 const double dist_p1p2 = pt1.
distance( pt2 );
54 const double dist_p1p3 = pt1.
distance( pt3 );
55 const double dist_p2p3 = pt2.
distance( pt3 );
57 const double dist = dist_p1p3 + dist_p2p3;
61 const double axis_a = dist / 2.0;
62 const double axis_b = std::sqrt( std::pow( axis_a, 2.0 ) - std::pow( dist_p1p2 / 2.0, 2.0 ) );
72 const double axis_a = std::fabs( pt2.
x() - pt1.
x() ) / 2.0;
73 const double axis_b = std::fabs( pt2.
y() - pt1.
y() ) / 2.0;
83 const double axis_a = std::fabs( pt1.
x() -
center.x() );
84 const double axis_b = std::fabs( pt1.
y() -
center.y() );
96 const double axis_a =
center.distance( pt1 );
100 const double axis_b =
center.distance( pp );
159 return std::numeric_limits<double>::quiet_NaN();
173 return M_PI * ( 3 * ( a + b ) - std::sqrt( 10 * a * b + 3 * ( a * a + b * b ) ) );
190 pointsInternal( segments, x, y, z, m );
191 const bool hasZ = !z.empty();
192 const bool hasM = !m.empty();
193 pts.reserve( x.size() );
194 for (
int i = 0; i < x.size(); ++i )
196 pts.append(
QgsPoint( x[i], y[i], hasZ ? z[i] : std::numeric_limits< double >::quiet_NaN(), hasM ? m[i] : std::numeric_limits< double >::quiet_NaN() ) );
201void QgsEllipse::pointsInternal(
unsigned int segments, QVector<double> &x, QVector<double> &y, QVector<double> &z, QVector<double> &m )
const
208 const double centerX =
mCenter.x();
209 const double centerY =
mCenter.y();
210 const double centerZ =
mCenter.z();
211 const double centerM =
mCenter.m();
212 const bool hasZ =
mCenter.is3D();
213 const bool hasM =
mCenter.isMeasure();
215 std::vector<double> t( segments );
218 for (
unsigned int i = 0; i < segments; ++i )
220 t[i] = 2 * M_PI - ( ( 2 * M_PI ) / segments * i );
223 x.resize( segments );
224 y.resize( segments );
226 z.resize( segments );
228 m.resize( segments );
229 double *xOut = x.data();
230 double *yOut = y.data();
231 double *zOut = hasZ ? z.data() :
nullptr;
232 double *mOut = hasM ? m.data() :
nullptr;
234 const double cosAzimuth = std::cos(
azimuth );
235 const double sinAzimuth = std::sin(
azimuth );
236 for (
double it : t )
238 const double cosT { std::cos( it ) };
239 const double sinT { std::sin( it ) };
251 auto polygon = std::make_unique<QgsPolygon>();
254 return polygon.release();
259 return polygon.release();
274 pointsInternal( segments, x, y, z, m );
279 x.append( x.at( 0 ) );
280 y.append( y.at( 0 ) );
282 z.append( z.at( 0 ) );
284 m.append( m.at( 0 ) );
296 const double angle =
mAzimuth * M_PI / 180.0;
303 const double halfHeight = std::sqrt( ux * ux + uy * uy );
304 const double halfWidth = std::sqrt( vx * vx + vy * vy );
318 rep = u
"Ellipse (Center: %1, Semi-Major Axis: %2, Semi-Minor Axis: %3, Azimuth: %4)"_s.arg(
mCenter.asWkt( pointPrecision ), 0,
's' )
328 auto ombb = std::make_unique<QgsPolygon>();
331 return ombb.release();
334 const QVector<QgsPoint> q =
quadrant();
344 ombb->setExteriorRing( ext );
346 return ombb.release();
virtual double eccentricity() const
The eccentricity of the ellipse.
virtual QgsPolygon * toPolygon(unsigned int segments=36) const
Returns a segmented polygon.
virtual bool operator==(const QgsEllipse &elp) const
QgsPoint center() const
Returns the center point.
virtual QVector< QgsPoint > foci() const
Two foci of the ellipse.
virtual QString toString(int pointPrecision=17, int axisPrecision=17, int azimuthPrecision=2) const
returns a string representation of the ellipse.
static QgsEllipse fromCenterPoint(const QgsPoint &ptc, const QgsPoint &pt1)
Constructs an ellipse by a center point and a another point.
virtual QgsPointSequence points(unsigned int segments=36) const
Returns a list of points with segmentation from segments.
virtual QgsLineString * toLineString(unsigned int segments=36) const
Returns a segmented linestring.
virtual double focusDistance() const
The distance between the center and each foci.
virtual bool operator!=(const QgsEllipse &elp) const
double azimuth() const
Returns the azimuth.
virtual QVector< QgsPoint > quadrant() const
The four quadrants of the ellipse.
QgsEllipse()=default
Constructor for QgsEllipse.
virtual double perimeter() const
The circumference of the ellipse using first approximation of Ramanujan.
void setAzimuth(double azimuth)
Sets the azimuth (orientation).
virtual QgsPolygon * orientedBoundingBox() const
Returns the oriented minimal bounding box for the ellipse.
virtual void setSemiMinorAxis(double semiMinorAxis)
Sets the semi-minor axis.
static QgsEllipse fromExtent(const QgsPoint &pt1, const QgsPoint &pt2)
Constructs an ellipse by an extent (aka bounding box / QgsRectangle).
virtual double area() const
The area of the ellipse.
virtual bool isEmpty() const
An ellipse is empty if axes are equal to 0.
static QgsEllipse fromFoci(const QgsPoint &pt1, const QgsPoint &pt2, const QgsPoint &pt3)
Constructs an ellipse by foci (pt1 and pt2) and a point pt3.
virtual QgsRectangle boundingBox() const
Returns the minimal bounding box for the ellipse.
virtual void setSemiMajorAxis(double semiMajorAxis)
Sets the semi-major axis.
static QgsEllipse fromCenter2Points(const QgsPoint &ptc, const QgsPoint &pt1, const QgsPoint &pt2)
Constructs an ellipse by a central point and two other points.
static double lineAngle(double x1, double y1, double x2, double y2)
Calculates the direction of line joining two points in radians, clockwise from the north direction.
static double normalizedAngle(double angle)
Ensures that an angle is in the range 0 <= angle < 2 pi.
static QgsPoint projectPointOnSegment(const QgsPoint &p, const QgsPoint &s1, const QgsPoint &s2)
Project the point on a segment.
static QgsPoint midpoint(const QgsPoint &pt1, const QgsPoint &pt2)
Returns a middle point between points pt1 and pt2.
static bool transferFirstZOrMValueToPoint(Iterator verticesBegin, Iterator verticesEnd, QgsPoint &point)
A Z or M dimension is added to point if one of the points in the list points contains Z or M value.
Line string geometry type, with support for z-dimension and m-values.
void setPoints(size_t size, const double *x, const double *y, const double *z=nullptr, const double *m=nullptr)
Resets the line string to match the specified point data.
Point geometry type, with support for z-dimension and m-values.
double distance(double x, double y) const
Returns the Cartesian 2D distance between this point and a specified x, y coordinate.
A rectangle specified with double values.
QString qgsDoubleToString(double a, int precision=17)
Returns a string representation of a double.
bool qgsDoubleNear(double a, double b, double epsilon=4 *std::numeric_limits< double >::epsilon())
Compare two doubles (but allow some difference).
QVector< QgsPoint > QgsPointSequence