diff --git a/include/wx/gdicmn.h b/include/wx/gdicmn.h index c97ace3b46..bd466722d4 100644 --- a/include/wx/gdicmn.h +++ b/include/wx/gdicmn.h @@ -614,6 +614,16 @@ public: wxPoint(int xx, int yy) : x(xx), y(yy) { } wxPoint(const wxRealPoint& pt) : x(wxRound(pt.x)), y(wxRound(pt.y)) { } + static wxPoint Round(const wxRealPoint& pt) + { + return wxPoint(pt); + } + + static wxPoint Truncate(const wxRealPoint& pt) + { + return wxPoint(static_cast(pt.x), static_cast(pt.y)); + } + wxPoint(const wxPoint& pt) = default; wxPoint& operator=(const wxPoint& pt) = default; diff --git a/interface/wx/gdicmn.h b/interface/wx/gdicmn.h index 7c55f7db65..62aa8d0ddb 100644 --- a/interface/wx/gdicmn.h +++ b/interface/wx/gdicmn.h @@ -702,14 +702,32 @@ public: Notice that since v3.1.2 the behaviour of this constructor has changed to round the floating point values of @a pt components, if you want to - truncate them instead you need to do it manually, e.g. - @code - wxRealPoint rp = ...; - wxPoint p(static_cast(rp.x), static_cast(rp.y)); - @endcode + truncate them instead, please use wxPoint::Truncate(). */ wxPoint(const wxRealPoint& pt); + /** + Creates a wxPoint by rounding the coordinates of the given + wxRealPoint. + + This is equivalent to using the constructor taking wxRealPoint but is + more explicit about the fact that rounding is performed. + + @since 3.3.2 + */ + static wxPoint Round(const wxRealPoint& pt); + + /** + Creates a wxPoint by truncating the coordinates of the given + wxRealPoint. + + This is similar to Round() but instead of rounding the coordinates, + they are simply truncated to integers. + + @since 3.3.2 + */ + static wxPoint Truncate(const wxRealPoint& pt); + /** @name Miscellaneous operators diff --git a/tests/geometry/point.cpp b/tests/geometry/point.cpp index 2f0dea08ff..d47fcfcb80 100644 --- a/tests/geometry/point.cpp +++ b/tests/geometry/point.cpp @@ -19,6 +19,21 @@ #include "wx/math.h" +TEST_CASE("wxPoint::Create", "[point]") +{ + const wxRealPoint rp(1.7, 2.3); + + wxPoint p(rp); + CHECK( p.x == 2 ); + CHECK( p.y == 2 ); + + CHECK( wxPoint::Round(rp) == p ); + + p = wxPoint::Truncate(rp); + CHECK( p.x == 1 ); + CHECK( p.y == 2 ); +} + TEST_CASE("wxPoint::Operators", "[point]") { wxPoint p1(1,2);