diff options
author | Aaron McCarthy <mccarthy.aaron@gmail.com> | 2014-07-28 09:57:04 +1000 |
---|---|---|
committer | Aaron McCarthy <mccarthy.aaron@gmail.com> | 2014-12-04 05:31:54 +0100 |
commit | b8681cdb51cc5d7040ecbe08b014bace2444c6f1 (patch) | |
tree | ebfe6a4e4cbbf178b1df2339abf91f7b4c84a2cf /src/positioning | |
parent | fddb7c51231e1649081de3a1d5f2ad191e9cd7dc (diff) | |
download | qtlocation-b8681cdb51cc5d7040ecbe08b014bace2444c6f1.tar.gz |
Add center() function to QGeoShape.
It is frequently useful to calculate the geometric center of a shape.
Both QGeoCircle and QGeoRectangle already define center() functions,
however, the application developer must cast each QGeoShape into either
a QGeoCirlce or QGeoRectangle before calling. Providing
QGeoShape::center() allows application code to be simplified.
Existing QGeoCircle::center() and QGeoRectangle::center() functions are
kept for compatibility.
Change-Id: I92b727ab5e713f70174588a27040446c992ae14e
Reviewed-by: Alex Blasche <alexander.blasche@theqtcompany.com>
Diffstat (limited to 'src/positioning')
-rw-r--r-- | src/positioning/qgeocircle.cpp | 29 | ||||
-rw-r--r-- | src/positioning/qgeocircle_p.h | 4 | ||||
-rw-r--r-- | src/positioning/qgeorectangle.cpp | 38 | ||||
-rw-r--r-- | src/positioning/qgeorectangle_p.h | 2 | ||||
-rw-r--r-- | src/positioning/qgeoshape.cpp | 15 | ||||
-rw-r--r-- | src/positioning/qgeoshape.h | 2 | ||||
-rw-r--r-- | src/positioning/qgeoshape_p.h | 2 |
7 files changed, 62 insertions, 30 deletions
diff --git a/src/positioning/qgeocircle.cpp b/src/positioning/qgeocircle.cpp index 459ce824..61336d1f 100644 --- a/src/positioning/qgeocircle.cpp +++ b/src/positioning/qgeocircle.cpp @@ -151,7 +151,7 @@ bool QGeoCircle::operator!=(const QGeoCircle &other) const bool QGeoCirclePrivate::isValid() const { - return center.isValid() && !qIsNaN(radius) && radius >= -1e-7; + return m_center.isValid() && !qIsNaN(radius) && radius >= -1e-7; } bool QGeoCirclePrivate::isEmpty() const @@ -166,17 +166,17 @@ void QGeoCircle::setCenter(const QGeoCoordinate ¢er) { Q_D(QGeoCircle); - d->center = center; + d->m_center = center; } /*! - Returns the center coordinate of this geo circle. + Returns the center coordinate of this geo circle. Equivalent to QGeoShape::center(). */ QGeoCoordinate QGeoCircle::center() const { Q_D(const QGeoCircle); - return d->center; + return d->center(); } /*! @@ -205,13 +205,18 @@ bool QGeoCirclePrivate::contains(const QGeoCoordinate &coordinate) const return false; // see QTBUG-41447 for details - qreal distance = center.distanceTo(coordinate); + qreal distance = m_center.distanceTo(coordinate); if (qFuzzyCompare(distance, radius) || distance <= radius) return true; return false; } +QGeoCoordinate QGeoCirclePrivate::center() const +{ + return m_center; +} + /*! Extends the circle to include \a coordinate */ @@ -220,7 +225,7 @@ void QGeoCirclePrivate::extendShape(const QGeoCoordinate &coordinate) if (!isValid() || !coordinate.isValid() || contains(coordinate)) return; - radius = center.distanceTo(coordinate); + radius = m_center.distanceTo(coordinate); } /*! @@ -235,8 +240,8 @@ void QGeoCircle::translate(double degreesLatitude, double degreesLongitude) Q_D(QGeoCircle); - double lat = d->center.latitude(); - double lon = d->center.longitude(); + double lat = d->m_center.latitude(); + double lon = d->m_center.longitude(); lat += degreesLatitude; lon += degreesLongitude; @@ -262,7 +267,7 @@ void QGeoCircle::translate(double degreesLatitude, double degreesLongitude) lon -= 180; } - d->center = QGeoCoordinate(lat, lon); + d->m_center = QGeoCoordinate(lat, lon); } /*! @@ -290,12 +295,12 @@ QGeoCirclePrivate::QGeoCirclePrivate() } QGeoCirclePrivate::QGeoCirclePrivate(const QGeoCoordinate ¢er, qreal radius) -: QGeoShapePrivate(QGeoShape::CircleType), center(center), radius(radius) +: QGeoShapePrivate(QGeoShape::CircleType), m_center(center), radius(radius) { } QGeoCirclePrivate::QGeoCirclePrivate(const QGeoCirclePrivate &other) -: QGeoShapePrivate(QGeoShape::CircleType), center(other.center), +: QGeoShapePrivate(QGeoShape::CircleType), m_center(other.m_center), radius(other.radius) { } @@ -314,7 +319,7 @@ bool QGeoCirclePrivate::operator==(const QGeoShapePrivate &other) const const QGeoCirclePrivate &otherCircle = static_cast<const QGeoCirclePrivate &>(other); - return radius == otherCircle.radius && center == otherCircle.center; + return radius == otherCircle.radius && m_center == otherCircle.m_center; } QT_END_NAMESPACE diff --git a/src/positioning/qgeocircle_p.h b/src/positioning/qgeocircle_p.h index d6fe37b5..635bd2ec 100644 --- a/src/positioning/qgeocircle_p.h +++ b/src/positioning/qgeocircle_p.h @@ -62,13 +62,15 @@ public: bool isEmpty() const; bool contains(const QGeoCoordinate &coordinate) const; + QGeoCoordinate center() const Q_DECL_OVERRIDE; + void extendShape(const QGeoCoordinate &coordinate); QGeoShapePrivate *clone() const; bool operator==(const QGeoShapePrivate &other) const; - QGeoCoordinate center; + QGeoCoordinate m_center; qreal radius; }; diff --git a/src/positioning/qgeorectangle.cpp b/src/positioning/qgeorectangle.cpp index d514edf9..808e091c 100644 --- a/src/positioning/qgeorectangle.cpp +++ b/src/positioning/qgeorectangle.cpp @@ -374,28 +374,13 @@ void QGeoRectangle::setCenter(const QGeoCoordinate ¢er) } /*! - Returns the center of this geo rectangle. + Returns the center of this geo rectangle. Equivalent to QGeoShape::center(). */ QGeoCoordinate QGeoRectangle::center() const { - if (!isValid()) - return QGeoCoordinate(); - Q_D(const QGeoRectangle); - double cLat = (d->topLeft.latitude() + d->bottomRight.latitude()) / 2.0; - - double cLon = (d->bottomRight.longitude() + d->topLeft.longitude()) / 2.0; - if (d->topLeft.longitude() > d->bottomRight.longitude()) { - cLon = cLon - 180.0; - } - - if (cLon < -180.0) - cLon += 360.0; - if (cLon > 180.0) - cLon -= 360.0; - - return QGeoCoordinate(cLat, cLon); + return d->center(); } /*! @@ -586,6 +571,25 @@ bool QGeoRectanglePrivate::contains(const QGeoCoordinate &coordinate) const return true; } +QGeoCoordinate QGeoRectanglePrivate::center() const +{ + if (!isValid()) + return QGeoCoordinate(); + + double cLat = (topLeft.latitude() + bottomRight.latitude()) / 2.0; + double cLon = (bottomRight.longitude() + topLeft.longitude()) / 2.0; + + if (topLeft.longitude() > bottomRight.longitude()) + cLon = cLon - 180.0; + + if (cLon < -180.0) + cLon += 360.0; + if (cLon > 180.0) + cLon -= 360.0; + + return QGeoCoordinate(cLat, cLon); +} + /*! Returns whether the geo rectangle \a rectangle is contained within this geo rectangle. diff --git a/src/positioning/qgeorectangle_p.h b/src/positioning/qgeorectangle_p.h index 1c5bc6f2..817fd360 100644 --- a/src/positioning/qgeorectangle_p.h +++ b/src/positioning/qgeorectangle_p.h @@ -62,6 +62,8 @@ public: bool isEmpty() const; bool contains(const QGeoCoordinate &coordinate) const; + QGeoCoordinate center() const Q_DECL_OVERRIDE; + void extendShape(const QGeoCoordinate &coordinate); QGeoShapePrivate *clone() const; diff --git a/src/positioning/qgeoshape.cpp b/src/positioning/qgeoshape.cpp index 932f7618..c331ef81 100644 --- a/src/positioning/qgeoshape.cpp +++ b/src/positioning/qgeoshape.cpp @@ -185,6 +185,21 @@ bool QGeoShape::contains(const QGeoCoordinate &coordinate) const } /*! + Returns the coordinate located at the geometric center of the geo shape. + + \since 5.5 +*/ +QGeoCoordinate QGeoShape::center() const +{ + Q_D(const QGeoShape); + + if (d) + return d->center(); + else + return QGeoCoordinate(); +} + +/*! Extends the geo shape to also cover the coordinate \a coordinate */ void QGeoShape::extendShape(const QGeoCoordinate &coordinate) diff --git a/src/positioning/qgeoshape.h b/src/positioning/qgeoshape.h index 3704ba6a..edaf263f 100644 --- a/src/positioning/qgeoshape.h +++ b/src/positioning/qgeoshape.h @@ -61,6 +61,8 @@ public: bool isEmpty() const; bool contains(const QGeoCoordinate &coordinate) const; + QGeoCoordinate center() const; + void extendShape(const QGeoCoordinate &coordinate); bool operator==(const QGeoShape &other) const; diff --git a/src/positioning/qgeoshape_p.h b/src/positioning/qgeoshape_p.h index 62ddd77c..154a4505 100644 --- a/src/positioning/qgeoshape_p.h +++ b/src/positioning/qgeoshape_p.h @@ -61,6 +61,8 @@ public: virtual bool isEmpty() const = 0; virtual bool contains(const QGeoCoordinate &coordinate) const = 0; + virtual QGeoCoordinate center() const = 0; + virtual void extendShape(const QGeoCoordinate &coordinate) = 0; virtual QGeoShapePrivate *clone() const = 0; |