Add wxPoint::Truncate() and Round() static function

Provide a simpler way of creating wxPoint from wxRealPoint by
truncating, instead of rounding, its coordinates.
This commit is contained in:
Vadim Zeitlin
2026-01-15 18:40:38 +01:00
parent 7bf202cb62
commit 69ad684156
3 changed files with 48 additions and 5 deletions
+10
View File
@@ -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<int>(pt.x), static_cast<int>(pt.y));
}
wxPoint(const wxPoint& pt) = default;
wxPoint& operator=(const wxPoint& pt) = default;
+23 -5
View File
@@ -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<int>(rp.x), static_cast<int>(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
+15
View File
@@ -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);