From c25bce109ef4a2a1fa1bfd7ecd6aba871546dbff Mon Sep 17 00:00:00 2001 From: Allan Sandfeld Jensen Date: Fri, 12 May 2017 16:50:24 +0200 Subject: Add QPolygon::intersects() methods Corresponds to the similar function QPainterPath::intersects() and is faster than calculating the intersection and then checking if it is empty. Change-Id: I694bb2206ed330a456a41d4118a952a68177b7a2 Reviewed-by: Eirik Aavitsland --- src/gui/painting/qpolygon.cpp | 43 +++++++++++++++++++++++++++++++++++++++++++ src/gui/painting/qpolygon.h | 4 ++++ 2 files changed, 47 insertions(+) (limited to 'src/gui') diff --git a/src/gui/painting/qpolygon.cpp b/src/gui/painting/qpolygon.cpp index 126b752811..3bf6004fcc 100644 --- a/src/gui/painting/qpolygon.cpp +++ b/src/gui/painting/qpolygon.cpp @@ -909,6 +909,8 @@ QPolygon QPolygon::united(const QPolygon &r) const Set operations on polygons will treat the polygons as areas. Non-closed polygons will be treated as implicitly closed. + + \sa intersects() */ QPolygon QPolygon::intersected(const QPolygon &r) const @@ -937,6 +939,26 @@ QPolygon QPolygon::subtracted(const QPolygon &r) const return subject.subtracted(clip).toFillPolygon().toPolygon(); } +/*! + \since 5.10 + + Returns \c true if the current polygon intersects at any point the given polygon \a p. + Also returns \c true if the current polygon contains or is contained by any part of \a p. + + Set operations on polygons will treat the polygons as + areas. Non-closed polygons will be treated as implicitly closed. + + \sa intersected() +*/ + +bool QPolygon::intersects(const QPolygon &p) const +{ + QPainterPath subject; subject.addPolygon(*this); + QPainterPath clip; clip.addPolygon(p); + + return subject.intersects(clip); +} + /*! \since 4.3 @@ -964,6 +986,7 @@ QPolygonF QPolygonF::united(const QPolygonF &r) const Set operations on polygons will treat the polygons as areas. Non-closed polygons will be treated as implicitly closed. + \sa intersects() */ QPolygonF QPolygonF::intersected(const QPolygonF &r) const @@ -991,6 +1014,26 @@ QPolygonF QPolygonF::subtracted(const QPolygonF &r) const return subject.subtracted(clip).toFillPolygon(); } +/*! + \since 5.10 + + Returns \c true if the current polygon intersects at any point the given polygon \a p. + Also returns \c true if the current polygon contains or is contained by any part of \a p. + + Set operations on polygons will treat the polygons as + areas. Non-closed polygons will be treated as implicitly closed. + + \sa intersected() +*/ + +bool QPolygonF::intersects(const QPolygonF &p) const +{ + QPainterPath subject; subject.addPolygon(*this); + QPainterPath clip; clip.addPolygon(p); + + return subject.intersects(clip); +} + /*! Returns the polygon as a QVariant. */ diff --git a/src/gui/painting/qpolygon.h b/src/gui/painting/qpolygon.h index f192e8d10b..8e74a499fd 100644 --- a/src/gui/painting/qpolygon.h +++ b/src/gui/painting/qpolygon.h @@ -98,6 +98,8 @@ public: Q_REQUIRED_RESULT QPolygon united(const QPolygon &r) const; Q_REQUIRED_RESULT QPolygon intersected(const QPolygon &r) const; Q_REQUIRED_RESULT QPolygon subtracted(const QPolygon &r) const; + + bool intersects(const QPolygon &r) const; }; Q_DECLARE_SHARED_NOT_MOVABLE_UNTIL_QT6(QPolygon) @@ -175,6 +177,8 @@ public: Q_REQUIRED_RESULT QPolygonF united(const QPolygonF &r) const; Q_REQUIRED_RESULT QPolygonF intersected(const QPolygonF &r) const; Q_REQUIRED_RESULT QPolygonF subtracted(const QPolygonF &r) const; + + bool intersects(const QPolygonF &r) const; }; Q_DECLARE_SHARED_NOT_MOVABLE_UNTIL_QT6(QPolygonF) -- cgit v1.2.3