Add is_zero_approx methods to Vector{2,3}

This commit is contained in:
Haoyu Qiu 2024-04-10 19:02:42 +08:00 committed by Relintai
parent 1a231787b0
commit 441bb29fd3
8 changed files with 29 additions and 1 deletions

View File

@ -172,6 +172,10 @@ bool Vector2::is_equal_approx(const Vector2 &p_v) const {
return Math::is_equal_approx(x, p_v.x) && Math::is_equal_approx(y, p_v.y); return Math::is_equal_approx(x, p_v.x) && Math::is_equal_approx(y, p_v.y);
} }
bool Vector2::is_zero_approx() const {
return Math::is_zero_approx(x) && Math::is_zero_approx(y);
}
Vector2::operator String() const { Vector2::operator String() const {
return "(" + String::num_real(x) + ", " + String::num_real(y) + ")"; return "(" + String::num_real(x) + ", " + String::num_real(y) + ")";
} }

View File

@ -126,6 +126,7 @@ struct _NO_DISCARD_CLASS_ Vector2 {
Vector2 reflect(const Vector2 &p_normal) const; Vector2 reflect(const Vector2 &p_normal) const;
bool is_equal_approx(const Vector2 &p_v) const; bool is_equal_approx(const Vector2 &p_v) const;
bool is_zero_approx() const;
Vector2 operator+(const Vector2 &p_v) const; Vector2 operator+(const Vector2 &p_v) const;
void operator+=(const Vector2 &p_v); void operator+=(const Vector2 &p_v);

View File

@ -106,6 +106,10 @@ bool Vector3::is_equal_approx(const Vector3 &p_v) const {
return Math::is_equal_approx(x, p_v.x) && Math::is_equal_approx(y, p_v.y) && Math::is_equal_approx(z, p_v.z); return Math::is_equal_approx(x, p_v.x) && Math::is_equal_approx(y, p_v.y) && Math::is_equal_approx(z, p_v.z);
} }
bool Vector3::is_zero_approx() const {
return Math::is_zero_approx(x) && Math::is_zero_approx(y) && Math::is_zero_approx(z);
}
Vector3::operator String() const { Vector3::operator String() const {
return "(" + String::num_real(x) + ", " + String::num_real(y) + ", " + String::num_real(z) + ")"; return "(" + String::num_real(x) + ", " + String::num_real(y) + ", " + String::num_real(z) + ")";
} }

View File

@ -137,6 +137,7 @@ struct _NO_DISCARD_CLASS_ Vector3 {
bool is_equal_approx(const Vector3 &p_v) const; bool is_equal_approx(const Vector3 &p_v) const;
inline bool is_equal_approx(const Vector3 &p_v, real_t p_tolerance) const; inline bool is_equal_approx(const Vector3 &p_v, real_t p_tolerance) const;
inline bool is_equal_approxt(const Vector3 &p_v, real_t p_tolerance) const; inline bool is_equal_approxt(const Vector3 &p_v, real_t p_tolerance) const;
bool is_zero_approx() const;
/* Operators */ /* Operators */

View File

@ -547,6 +547,7 @@ struct _VariantCall {
VCALL_LOCALMEM1R(Vector2, bounce); VCALL_LOCALMEM1R(Vector2, bounce);
VCALL_LOCALMEM1R(Vector2, reflect); VCALL_LOCALMEM1R(Vector2, reflect);
VCALL_LOCALMEM1R(Vector2, is_equal_approx); VCALL_LOCALMEM1R(Vector2, is_equal_approx);
VCALL_LOCALMEM0R(Vector2, is_zero_approx);
VCALL_LOCALMEM0R(Vector2, angle); VCALL_LOCALMEM0R(Vector2, angle);
VCALL_LOCALMEM1(Vector2, set_rotation); VCALL_LOCALMEM1(Vector2, set_rotation);
VCALL_LOCALMEM0R(Vector2, abs); VCALL_LOCALMEM0R(Vector2, abs);
@ -682,6 +683,7 @@ struct _VariantCall {
VCALL_LOCALMEM1R(Vector3, reflect); VCALL_LOCALMEM1R(Vector3, reflect);
VCALL_LOCALMEM1R(Vector3, is_equal_approx); VCALL_LOCALMEM1R(Vector3, is_equal_approx);
VCALL_LOCALMEM2R(Vector3, is_equal_approxt); VCALL_LOCALMEM2R(Vector3, is_equal_approxt);
VCALL_LOCALMEM0R(Vector3, is_zero_approx);
VCALL_LOCALMEM2(Vector3i, set_axis); VCALL_LOCALMEM2(Vector3i, set_axis);
VCALL_LOCALMEM1R(Vector3i, get_axis); VCALL_LOCALMEM1R(Vector3i, get_axis);
@ -2657,6 +2659,7 @@ void register_variant_methods() {
ADDFUNC1R(VECTOR2, VECTOR2, Vector2, bounce, VECTOR2, "n", varray()); ADDFUNC1R(VECTOR2, VECTOR2, Vector2, bounce, VECTOR2, "n", varray());
ADDFUNC1R(VECTOR2, VECTOR2, Vector2, reflect, VECTOR2, "n", varray()); ADDFUNC1R(VECTOR2, VECTOR2, Vector2, reflect, VECTOR2, "n", varray());
ADDFUNC1R(VECTOR2, BOOL, Vector2, is_equal_approx, VECTOR2, "v", varray()); ADDFUNC1R(VECTOR2, BOOL, Vector2, is_equal_approx, VECTOR2, "v", varray());
ADDFUNC0R(VECTOR2, BOOL, Vector2, is_zero_approx, varray());
ADDFUNC0R(VECTOR2, REAL, Vector2, angle, varray()); ADDFUNC0R(VECTOR2, REAL, Vector2, angle, varray());
ADDFUNC1(VECTOR2, NIL, Vector2, set_rotation, REAL, "radians", varray()); ADDFUNC1(VECTOR2, NIL, Vector2, set_rotation, REAL, "radians", varray());
ADDFUNC0R(VECTOR2, VECTOR2, Vector2, abs, varray()); ADDFUNC0R(VECTOR2, VECTOR2, Vector2, abs, varray());
@ -2781,6 +2784,7 @@ void register_variant_methods() {
ADDFUNC1R(VECTOR3, VECTOR3, Vector3, reflect, VECTOR3, "n", varray()); ADDFUNC1R(VECTOR3, VECTOR3, Vector3, reflect, VECTOR3, "n", varray());
ADDFUNC1R(VECTOR3, BOOL, Vector3, is_equal_approx, VECTOR3, "v", varray()); ADDFUNC1R(VECTOR3, BOOL, Vector3, is_equal_approx, VECTOR3, "v", varray());
ADDFUNC2R(VECTOR3, BOOL, Vector3, is_equal_approxt, VECTOR3, "v", REAL, "tolerance", varray()); ADDFUNC2R(VECTOR3, BOOL, Vector3, is_equal_approxt, VECTOR3, "v", REAL, "tolerance", varray());
ADDFUNC0R(VECTOR3, BOOL, Vector3, is_zero_approx, varray());
ADDFUNC2(VECTOR3I, NIL, Vector3i, set_axis, INT, "axis", INT, "value", varray()); ADDFUNC2(VECTOR3I, NIL, Vector3i, set_axis, INT, "axis", INT, "value", varray());
ADDFUNC1R(VECTOR3I, INT, Vector3i, get_axis, INT, "axis", varray()); ADDFUNC1R(VECTOR3I, INT, Vector3i, get_axis, INT, "axis", varray());

View File

@ -159,6 +159,13 @@
Returns [code]true[/code] if the vector is normalized, [code]false[/code] otherwise. Returns [code]true[/code] if the vector is normalized, [code]false[/code] otherwise.
</description> </description>
</method> </method>
<method name="is_zero_approx">
<return type="bool" />
<description>
Returns [code]true[/code] if this vector's values are approximately zero, by running [method @GDScript.is_zero_approx] on each component.
This method is faster than using [method is_equal_approx] with one value as a zero vector.
</description>
</method>
<method name="length"> <method name="length">
<return type="float" /> <return type="float" />
<description> <description>

View File

@ -160,6 +160,13 @@
Returns [code]true[/code] if the vector is normalized, [code]false[/code] otherwise. Returns [code]true[/code] if the vector is normalized, [code]false[/code] otherwise.
</description> </description>
</method> </method>
<method name="is_zero_approx">
<return type="bool" />
<description>
Returns [code]true[/code] if this vector's values are approximately zero, by running [method @GDScript.is_zero_approx] on each component.
This method is faster than using [method is_equal_approx] with one value as a zero vector.
</description>
</method>
<method name="length"> <method name="length">
<return type="float" /> <return type="float" />
<description> <description>

View File

@ -685,7 +685,7 @@ void StyleBoxFlat::draw(RID p_canvas_item, const Rect2 &p_rect) const {
const bool rounded_corners = (corner_radius[0] > 0) || (corner_radius[1] > 0) || (corner_radius[2] > 0) || (corner_radius[3] > 0); const bool rounded_corners = (corner_radius[0] > 0) || (corner_radius[1] > 0) || (corner_radius[2] > 0) || (corner_radius[3] > 0);
// Only enable antialiasing if it is actually needed. This improve performances // Only enable antialiasing if it is actually needed. This improve performances
// and maximizes sharpness for non-skewed StyleBoxes with sharp corners. // and maximizes sharpness for non-skewed StyleBoxes with sharp corners.
const bool aa_on = (rounded_corners || !skew.is_equal_approx(Vector2())) && anti_aliased; const bool aa_on = (rounded_corners || !skew.is_zero_approx()) && anti_aliased;
const bool blend_on = blend_border && draw_border; const bool blend_on = blend_border && draw_border;