mirror of
https://github.com/wxWidgets/wxWidgets.git
synced 2026-09-25 17:15:45 +08:00
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:
@@ -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
@@ -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
|
||||
|
||||
|
||||
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user